createtable stu(
id intunsignednotnullauto_incrementprimarykey,
name varchar(16)notnullunique,
age tinyintunsignednotnulldefault20,
sex enum('w','m')notnulldefault'm',
classid char(8));
# 修改stu表中的数据 id为2、5、6的update stu set age=21, sex='w'where id in(2,5,6)# 修改一个不存在的数据并不会报错update stu set age=23, sex='m'where id=100;
数据的删除
# 删除不存在的数据不会报错,删除id为100和200的数据deletefrom stu where id in(100,200);# 删除 id>100的数据deletefrom stu where id>100;# 删除在100和200之间的数据,包括两端的数据deletefrom stu where id between100and200;
数据的查询(最重要)
# 查询所有数据select*from stu;# 查询多个字段,注意逗号(英文状态下)分隔select id, name from stu;# 查询字段中添加字段并做运算select id, name, age, age+5from stu;# 查询字段并使用别名, 通过as关键字select id, name as username, age, age+5as age5 from stu;# 查询字段并使用别名, as关键字省略不写select id, name username, age, age+5 age5 from stu;# 查询表中所有字段数据select*from stu;# 整理注意,数据在select输出的时候,和表结构不一定一致, 这里追加字段select*,'hangzhou'as city from stu;# 此时查到所有数据并且在最后一列加上值为hangzhou的city字段# 查询表中所有字段的部分数据select*from stu where id=2;select*from stu where id<5;select*from stu where id in(2,3,4);select*from stu where id between3and9;select*from stu where id notbetween8and9;select*from stu where id >=10and age <=100;select*from stu where id >=10or age <=100;# 查询某个库(demodb)下的某张表(demotab)select*from demodb.demotab where createtime isnull;# 模糊查询select*from stu where name like"%a%"# 包含式查询 中间含有a的