1. 登录数据库
mysql -u root -p
-- 另起一行输入密码
  1. 查看共有多少数据库
show databases;
  1. 创建数据库db1
create database db1;
  1. 选择数据库db1
use db1;
  1. 查看当前数据库中的表
show tables;
  1. 创建表tb1
create table if not exists tb1 (
  id int not null auto_increment,
  title varchar(100) not null,
  author varcahr(40) not null,
  submission_date date,
  primary key(id));
  1. 查看某一个表中的列的信息,假设表名叫tb1
-- 一共有两种写法,均可以使用
1. desc tb1;
2. show columns from tb1;
  1. 删除表tb1
drop table tb1;
  1. 向表tb1中插入数据
-- 这里id的值不插入也可以,因为在定义表时将其设置为auto_increment,可以自动+1
insert into tb1
  (id, title, author, submission_date)
  values
  (1, "title1", "author1", now()),
  (2, "title2", "author2", now());
  1. 查询
select * from tb1 where title="title2";
/*
这里where子句是不检查大小写的,因此上述语句中将"title2"换为
"TITLE2"仍会得到一样的结果。若是强调大小写的话应该在
where后加上binary关键字,如下所示:
*/
select * from tb1 where binary title="TITLE2"
-- 在where后面还可以有limit和offset关键字,指明显示的行数以及查询时候的偏移量
-- 标准模板为:select [column] from [table] [where] [limit] [offset];
  1. 注释
1. -- 这是单行注释,注意要有一个空格
2. # 这是另一种单行注释
3. /*
   这是多行注释
   */
  1. 更新表中内容,where指明待更新表项需满足的条件
update tb1 set title="title3" where id=1;
  1. 删除表中内容,但表定义会保留,where指明删除条件,若没有where则会删除整个表中的数据
delete from tb1 where id=1; -- 删除id=1的那一行
delete from tb1; --删除整张表中的数据
  1. 查询表中的内容
-- where子句设置查询条件
-- limit指定显示的行数
-- offset指定偏移量,例如,offset 2 就是从第三行数据开始查询
select * from tb1 [where] [limit] [offset]
  1. 模糊匹配,在WHERE子句中使用SQL LIKE子句。%表示任意数量的任意字符,_表示任意单个字符。
-- 在表tb1中查询author属性以COM结尾的所有表项
select * from tb1 where author like "%COM";

有时候%_会混合使用,以下是常用的匹配模板:

'%a'     -- 以a结尾的数据
'a%'     -- 以a开头的数据
'%a%'    -- 含有a的数据
'_a_'    -- 三位且中间字母是a的
'_a'     -- 两位且结尾字母是a的
'a_'     -- 两位且开头字母是a的