断点

每天进步一点点!
posts - 174, comments - 56, trackbacks - 0, articles - 21

MySQL命令集

Posted on 2010-01-14 21:38 断点 阅读(217) 评论(0)  编辑  收藏 所属分类: MySQL

一、常用命令
列出数据库:show databases;
选择数据库:use databaseName;
列出表格:show tables;
建库create database 库名;
建表create table 表名;
显示数据表的结构:desc 表名;
删库 drop database 库名;
删表 drop table 表名;
将表中记录清空:delete from 表名;
显示表中的记录:select * from 表名;

二、连接MYSQL。
格式: mysql -h主机地址 -u用户名 -p用户密码
1、例1:连接到本机上的MYSQL。
首先在打开DOS窗口,键入命令mysql -uroot -p,回车后提示你输密码。
2、例2:连接到远程主机上的MYSQL。假设远程主机的IP为:192.168.1.5,用户名为root,密码为root。则键入以下命令: mysql -h192.168.1.5 -uroot -proot
3、退出MYSQL命令: exit (回车)

三、其它命令:
查询时间:select now();
查询数据库版本:select version();
查询当前用户:select user();
查询当前使用的数据库:select database();  
匹配字符:可以用通配符_代表任何一个字符,%代表任何字符串;
增加一个字段:alter table tabelName add column fieldName dateType;
增加多个字段:alter table tabelName add column fieldName1 dateType,add columns fieldName2 dateType;
修改密码:mysqladmin -u用户名 -p旧密码 password新密码

以下转载:

增加新用户:
格式:grant select on 数据库.* to 用户名@登录主机 identified by "密码"

例1、增加一个用户test1密码为abc,让他可以在任何主机上登录,并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入MYSQL,然后键入以下命令:
grant select,insert,update,delete on *.* to test1@"%" Identified by "abc";
但例1增加的用户是十分危险的,你想如某个人知道test1的密码,那么他就可以在internet上的任何一台电脑上登录你的mysql数据库并对你的数据可以为所欲为了,解决办法见例2。

例2、增加一个用户test2密码为abc,让他只可以在localhost上登录,并可以对数据库mydb进行查询、插入、修改、删除的操作(localhost指本地主机,即MYSQL数据库所在的那台主机),这样用户即使用知道test2的密码,他也无法从internet上直接访问数据库,只能通过MYSQL主机上的web页来访问了。
grant select,insert,update,delete on mydb.* to test2@localhost identified by "abc";

例3.如果你不想test2有密码,可以再打一个命令将密码消掉。
grant select,insert,update,delete on mydb.* to test2@localhost identified by "";

例4.增加一个管理员帐户:grant all on *.* to user@localhost identified by "password";

其它命令集:
从已经有的表中复制表的结构create table table2 select * from table1 where 1<>1; 
复制表create table table2 select * from table1; 
对表重新命名alter table table1 rename as table2;
修改列的类型:
alter table table1 modify id int unsigned;//修改列id的类型为int unsigned
alter table table1 change id sid int unsigned;//修改列id的名字为sid,而且把属性修改为int unsigned 
创建索引:
alter table table1 add index ind_id (id);
create index ind_id on table1 (id);
create unique index ind_id on table1 (id);//建立唯一性索引 
删除索引:
drop index idx_id on table1;
alter table table1 drop index ind_id; 
联合字符或者多个列(将列id与":"和列name和"="连接):
select concat(id,':',name,'=') from students。  
limit(选出10到20条)<第一个记录集的编号是0>:
select * from students order by id limit 9,10。  
MySQL不支持的功能:事务,视图,外键和引用完整性,存储过程和触发器。


posted @ 2009-03-26 10:02 断点 阅读(79) | 评论 (0)

只有注册用户登录后才能发表评论。


网站导航: