常见的sql语句

  1. 启动mysql服务器:net start mysql
  2. 停止mysql服务器:net stop mysql;
  3. 连接mysql服务器:mysql -u root -h127.0.0.1 -p
  4. 断开mysql服务器:quit、exit、\q
  5. 创建数据库:create database student; 或 create schema student;
  6. 查看数据库:show databases;
  7. 切换(使用,要操作的数据库):use 数据库名;
  8. 删除数据库:drop database student;
  9. 创建表:create table 表名(列名 类型,列名 类型,列名 类型);
  10. 查询数据库下所有的表:show tables;
  11. 查询表的结构:desc 表名;   show columns from 表名 from 数据库名;   show columns from数据库名.表名
  12. 创建指定字符集的数据库:create database 数据库名 character set=gbk;
  13. 创建数据库前判断数据库名是否存在:create database if not exists 数据库名;

  14. 筛选‘db_’开头的数据库:show databases like ‘db_%’;

  15. 添加字段:alter table 表名 add(字段 类型,字段 类型);

  16. 添加字段:alter table 表名 add(字段 类型,字段 类型);

  17. 修改字段类型:alter table 表名 modify 字段 类型;

  18. 修改字段名:alter table 表名 change 旧字段名 新字段名 新字段类型;

  19. 修改表名:alter table 旧表名 rename as 新表名;

  20. 删除数据表:drop table 表名;

  21. 向表中插入数据:insert into 表名 values(1001,’张三’,25,’男’);

                       insert into 表名 set id=1001,xm=’张三’,age=25,sex=’男’;

  22. 修改表中记录:update 表名 set age=24 where id=1001;
  23. 删除表中特定数据:delete from 表名 where id=1001;
  24. 删除表中所有数据:truncate table 表名;
  25. 数据库的备份:mysqldump -uroot -p123 数据库名>c:\123.sql
  26.    数据库的恢复:mysql -uroot -p123 数据库名<c:\123.sql

  27. 查询所有字段:select * from 表名;
  28. 查询指定字段:select 字段名(列名) from 表名;
  29. 查询指定数据:select * from 表名 where xm=’张三’;
  30. 在集合内查询(关键字in):select * from 表名 where id in(1001,1003,1005);
  31. 在范围内查询(between...and...):select * from 表名 where age between 20 and 30;
  32. 空值查询:select * from stu where li_lun is null;
  33. 带关键字and多条件查询:select * from stu where li_lun>90 and ji_neng>90;
  34. 带关键字or多条件查询:select * from stu where id=1001 or li_lun>90;
  35. 去重查询:select distinct xm from stu;
  36. 列运算:数值计算:select li_lun+ji_neng from stu;字符串连接:select concat(li_lun,’分’) from stu; 转换null值:select ifnull(li_lun,0)+ji_neng from stu;给列起别名:select ifnull(li_lun,0)+ji_neng as ‘总分’ from stu;(as可省略)
  37. 主键(primary key):修改表时指定主键:alter table stu add primary key(id);删除主键:alter table stu drop primary key;

  38. 主键自增长(primary key auto_increment):(此时主键必须设置为int)  修改表时设置主键自增长:alter table stu change sid sid int auto_increment;   修改表时删除主键自增长:alter table stu change sid sid int;
  39. 非空(not null):添加非空约束:alter table 表名 modify 列名 列类型 not null;    修改非空约束:alter table 表名 modify 列名 列类型 null;  删除非空约束:alter table 表名modify 列名 列类型;
  40. 唯一(unique):  添加唯一约束: alter table 列名 add unique(列名称) ;   修改唯一约束:alter table 表名 modify 列名称 列类型;   删除唯一约束:alter table 表名称 drop index 列名称;
  41. 默认值(default): 添加默认值约束:alter table 表名 modify 列名 列类型 not null default '默认值';
  42. 外键(foreign key):constrain fk_id foreign key(sid) references sheng_fen(id)                  添加外键约束:alter table student add constrain fk_id foreign key(sid) references sheng_fen(id);                                                                                                                        删除外键约束:alter table student drop foreign key fk_id;
  43. 升序:select * from student order by age asc;
  44. 倒序:select * from student order by age desc;
  45. 模糊查询:_  表示单个的任意字符   %  表示任意长度的任意字符

    例如:查询姓张的所有同学的记录:select * from student where sname like ‘张%’;             查询姓名中第二个字为‘亮’的记录:select * from student where sname like ‘_亮%’             查询姓名中包含‘明’字的记录:select * from student where sname like ‘%明%’;

  46. 显示内连接:select * from 左表,右表 where 关联条件;

  47. 隐式内连接:select * from 左表 inner join右表 on 关联条件; (inner可省略)

  48. 左外连接:select * from 左表 left outer join 右表 on 关联条件; (outer可省略)

  49. 右外连接:select * from 左表 right outer join 右表 on 关联条件;(outer可省略)

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值