数据库常用命令总结

来源:互联网 发布:淘宝对接什么意思 编辑:程序博客网 时间:2024/06/08 11:35

数据库常用命令总结

作者:罗正波 日期:2017/2/25 23:30:26 # 连接数据库a、mysql -u root -pb、mysql -u root -h主机地址 -p#退出数据库exit / quit / \q

数据库中修改密码

a、在cmd下操作    mysqladmin -uroot -p password 新密码;b、进入mysql后改密码的操作    1、set password for 用户名@主机地址 = password('新密码');    2、 use mysql 选中库之后执行。        update user set authentication_string = password('新密码') where user = "用户名";

创建一个用户与删除用户

a 、 创建用户(并给相应的权限)    grant 权限 on 库名.表名 to 用户名@主机地址 identified by'密码'  with grant option;    权限(all privileges 表示所有的权限 updaet、select、insert、delete);    写了之后刷新一下    flush privileges;b、 删除用户    delete from user where user="用户名"; 

数据库导入与导出

一、导出a、导出库    在cmd下操作    mysqldump  -u root -p 库名 > 路径和文件名    例子:mysqldump -u root -p > f:lamp.sql;b、导出数据库中的指定表    mysqldump -u 用户名 -p 库名 表名 > 路径和文件名;c、导出数据库中表结构(没有内容)    mysqldump -u 用户名 -p -d 库名 表名 > 路径和文件名;二、导入a、在cmd下操作    mysql -u 用户名 -p 库名 < 文件名;b、在进入mysql之后导入    source 路径文件名;(会导入在同名的表中)

索引的创建与删除

普通索引    a、再建表的时候就写在建表语句中    例子:index 索引名 (字段名)b、添加所普通索引    create index 索引名 on 表名(字段名);c、普通索引的删除    drop index 索引名 on 表名;唯一索引a、创建在建表时    例子 : unqiue 索引名 (索引字段);b、添加唯一索引    create unqiue 索引名 on 表名 (字段);c、删除唯一索引    drop index 索引名 on 表名(字段);主键索引:a、在建表时    直接加在通常加在ID上primary keyb、主键的追加    alter table 表名 add primary key(`字段`);c、主键的删除    alter table 表名 drop primary  key;(没有自增的主键删除)。先去掉自增    例子: alter   table 表名  modify ID int unsigned;主键的自增    alter table 表名 drop primary key;

设置字符集

a、服务器级别    在my.ini中的 加入 character_set_sercer = utf8;b、库级别    查看:show create database 库名;    建库是指定库的字符集:create database 库名 default charset = utf8;    修改库字符集:alter database 库名 default charset = utf8;c、表级别    查看:show create table 表名;    建表时指定:create table 表名(...)default charset = utf8;    修改表的字符集:alter table 表名 default charset = utf8;

表的修改

1、修改表名    alter table 旧表名 rename 新表名;2、修改表中的字段类型    alter table 表名 modify 字段名 类型 约束;(还可以改约束);3、修改字段名    alter table 表名 change 旧字段名 新字段名 数据类型 约束;4、新增字段    alter table 表名 add 字段名 类型 约束 【 after 字段 | first 】;5、删除字段    alter table 表名 drop 字段名;6、修改表的引擎    alter table 表名 engine = innodb; 

数据操作语言(DML语句)

数据添加

a、单条数据插入    insert into 表名(字段1,字段2,...)  value(值1,值2,...) ;b、多条数据的插入    insert into 表名(字段1,字段2,...)  values(值1,值2,...),(值1,值2,...),(值1,值2,...)...;c、指定字段插入    insert into 表名(指定字段1,指定字段2,...) value(值1,值2,...);(注意:也可以多指定条插入);

数据删除

a、有条件的删除    delete from 表名 where 字段 = 值;b、无条件的删除    delete  from 表名 ;(注意:没有条件的删除会删除整张表,但字段属性还在如:自增);c、清空表    truncate table 表名;(将彻底清空表的数据);

数据修改

a、有条件的修改    update 表名 set 字段1 = 值1, 字段2 = 值2,... where 字段 = 值;b、无条件的修改    update 表名 set 字段1 = 值1 ...; (无条件的修改将是符合 字段1 =值1的所有 );

数据查询语言(DQL语句)

a、查询整张表     select *  from 表名;b、查询指定字段    select * from 表名 where 字段 = 值;c、取别名、    select name as n, sex as s from 表名 where 字段 = 值;(as可以省略);d、将查询的结果去重    select distinct 字段 from 表名;e、MySQL中常用的函数    count();统计查询到的结果。 例子 select count(*) as total from 表名;    concat();将查询结果拼接起来显示。例子 select concat(id,name) IdName from 表名;    md5();md5加密显示结果。例子 select md5(id) md5 from 表名;(通常不会这样做);f、where 条件    and ,or        例子:select * from 表名 where name='123' and/or sex = '男';    between...and/not between ...and..        例子:select * from 表名 where  id between 10 and 20 ;        例子:select * from 表名 where  id not between 10 and 20;    like(模糊查询)配合(_ 下划线表示单个任意字符,% 表示任意个任意字符)/ not like        例子:select * from 表名     where 字段 like  "%o%";(查询字段值中含有o的所有值);        例子:select * from 表名 where 字段 like  "_a_";(查询字段值中以任意字符开始和结尾但是,中间的个值是a的,总的3个);        例子:select * from 表名 where 字段  not like "%w%"; (搜索不符合条件的);    in() / not in()        例子:select * from 表名 where 字段 in('值1','值2',...);    order by(排序操作)        例子:seelct * from 表名 where order by 字段 ;(默认asc升序);            seelct  * from 表名 where order by 字段 desc ;(降序);            select * from 表名 where order by 字段1 desc , 字段2 asc;    常用函数        count(); sum(); avg();max(); min();        例子:select avg(id) avg  from 表名;              select max(id) max from 表名;             select sum(id) sum from 表名;g、limit(限制查询结果)    例子:select * from 表名 limit 2;    例子: select * from 表名 limit 2,4;(跳过两条,显示4条);h、group by(分组操作)    例子:select * from 表名 group by 字段;    配合count()等使用:        例子:select count(id), max(id), min(id) from 表名 group by 字段;    配合having使用:        例子:select *  from  表名 group by sex  having 字段 = 值;i、多表查询    例子:seelct stu.* s , cj.php c ... from s , c where s.id = c.uid  and stu.id = 1;
0 0
原创粉丝点击