快速上手SQL语句基本操作

Mysql数据库 + Mysql Workbench图形化客户端

Tips : 执行语句 : select version(); 可以查看本机Mysql数据库版本

Tips : 行开头标记 "-- " 为注释该行 , 如需运行语句则需删除注释标记

– 查看有哪些数据库
– show databases;
– 使用数据库myblog
– use myblog;
– 展示该数据库下所有表
– show tables;


– 数据插入表
– insert into users (username, password, realname) values (‘zhangsan’, ‘456’, ‘张三’);
– insert into users (username, password, realname) values (‘lisi’, ‘123’, ‘李四’);


– 删除行
– delete from users where username=‘zhangsan’;
– 但实际开发中一般不会真正删除行,而是将自定义的state字段置为0来标记该行无效( 软删除 )
– update users set state=‘0’ where username=‘lisi’;
– 且采用形如下的查询方式查询有效数据
– select * from users where state=‘1’;
– 且可以恢复软删除的数据
– update users set state=‘1’ where username=‘lisi’;


– 更新
– SET SQL_SAFE_UPDATES = 0; // 如果更新报错safe update mode则执行该行代码
– update users set realname=‘李四’ where username=‘lisi’;


– 查询表中全部数据(实际开发中为了提高性能尽量避免使用*)
– select * from users;
– 查询表中某些列的数据
– select id, username from users;
– 条件查询
– select * from users where username=‘zhangsan’;
– 排除查询( 该句式查询users表中密码不为456的数据 )
– select * from users where password <> ‘456’;
– 多条件查询交集
– select * from users where username=‘zhangsan’ and password=‘123’;
– 多条件查询并集
– select * from users where username=‘zhangsan’ or password=‘123’;
– 模糊查询
– select * from users where username like ‘%san%’;
– 排序( 默认从上到下正序排列, 加上参数desc则为倒序排列 )
– select * from users where password like ‘%1%’ order by id desc;


发布了49 篇原创文章 · 获赞 29 · 访问量 1901

猜你喜欢

转载自blog.csdn.net/Brannua/article/details/104652438