1. -- 查看所有数据库
  2. show databases;
  3. -- 创建数据库
  4. create database 数据库名 charset=utf-8;
  5. -- 使用数据库
  6. use 数据库名;
  7. -- 显示所有的表
  8. show tables;
  9. -- 删除数据库
  10. drop database 数据库名;
  11. -- 创建students数据表
  12. create table students(
  13. id int unsigned not null auto_increment primary key,
  14. name varchar(50) not null default "张三",
  15. age tinyint unsigned not null default 18,
  16. high decimal(5,2) not null,
  17. gender enum("男", "女", "保密")default "保密",
  18. cls_id int unsigned not null
  19. );

1.增

  1. use myblog;
  2. show tables;
  3. insert into users(username, `password`, realname) values ('zhangsan', '123', '张三');

2.查

  1. use myblogs;
  2. show tables;
  3. select * from users; --查询所有
  4. select id,username form users; --查询idusername
  5. select * from users where username='zhangsan'; --条件查询
  6. select * from users where username='zhangsan' and `password`='123'; --并且
  7. select * from users where username='zhangsan' or `password`='123'; --或者
  8. select * from users where username like '%zhang%'; --模糊查询
  9. select * from users where password like '%1%' order by id desc; --desc表示倒序,不加默认正序
  10. select * form users where state <> '0'; -- <>表示不等于0
  11. select count(id) as `count` from blogs; -- 查询总数
  12. select * from blogs order by id desc limit 2; -- 查询第一页的两条数据
  13. select * from blogs order by id desc limit 2 offset 2; -- 查询第二页的两条数据

3.改(更新)

如果遇到update users set realname…报错的话,先执行 SET SQL_SAFE_UPDATES = 0; 然后删掉再执行更新操作

  1. update users set realname='李四2' where username='lisi';

4.删

  1. delete from users where username='lisi';
  2. -- 日常开发中我们通常是采用软删除
  3. update users set state='0' where username='lisi';

5. 多表联查

  1. select blogs.*, users.username, users.nickname
  2. from blogs inner join users on users.id = blogs.userid
  3. where users.username = 'lisi'