1、增:
创建数据库:create database 库名;
创建表:create table 表名 (字段名 类型, 字段名 类型, 字段名 类型);
在表中添加字段:alter table 表名 add字段名 字段类型;
在表中第一列添加字段:alter table students add 字段名 字段类型 first;
在表中指定已有的字段后添加字段:alter table students add 字段名 字段类型 after 已有字段;
给表中添加字段值记录:insert into 库名.表名 values(记录1,记录2,记录3);
同时给表中插入多条记录:insert into 库名.表名 values(字段,字段...),(记录,记录...);
分开给表中插入记录:insert into 库名.表名 (字段,字段...)values(记录,记录...);
单精度浮点型: create table 表名(字段 float(3,1))3个数字,有小数点后。mysql自动给四舍五入。32位储存
双精度浮点型:create table 表名(字段 double(3,1))3个数字,有小数点后。若输入多位,则四舍五入;更精准64位储存
时间字段:create table 表名 (字段 date|time|year); date年月日 time时分秒 year年
非空约束不出现null:create table 表名 (字段 int not null)
字段数值自增长:create table 表名 ( 字段 int aut_increment);
枚举: create table 表名(字段名 enum('M','F')); 只能写入MF两种字符串; set最大包含64类项。在set中,相同的元素不能同时存在。
索引类型 index或key:
普通索引:create table 表名( 字段1 字段类型, key 索引名称(字段1) );
建表后添加普通索引:alter table 表名 add key(字段);
唯一性索引:create table 表名( 字段1 字段类型, unique key 索引名称 (字段1) );
建表后添加唯一索引:alter table 表名 add unique (字段);
主键索引:create table 表名( 字段 字段类型not null auto_increment primary key); 主键字段要求字段的所有内容必须唯一。主键索引是一种特殊的唯一索引, 并不允许有空值。
复合索引:create table 表名 ( 字段1 字段类型 not null ,字段2 字段类型 not null ,primary key (字段1,字段2));数值要唯一
2、删:
drop database 数据库名; 删除数据库
drop table 库名.表名; 删除表
alter table 表名 drop 字段名 ; 删除表中字段
delete from 库名.表名 where 字段名=字段值记录; 删除表中指定的记录
delete from 库名.表名 where 字段 is null; 删除表中指定为某一字段为空值的记录的行
3、改:
alter table 表名 rename 新表名; 修改表名
alter table 表名 modify 要修改的字段名 要修改的类型; 修改表中的字段类型
alter table 表名 change 原字段名 新字段名 新字段类型; 修改表中的字段与字段类型
update 库名.表名 set 某个字段=值 where 存在的字段=存在的值; 修改表中,指定某个字段为某个值的记录的别的字段记录值
update 库名.表名 set 字段=值; 修改表中所有这个字段的值
update 库名.表名 set 字段=值,字段=值 where 字段=值; 同时修改多个字段的值,用逗号隔开
4、查:
查看数据库信息:show databases;
查看自己当前在数据库哪个位置:select database();
查看数据库表相关信息:show tables;
查看表结构:desc 库名.表名;
查看表中所有的记录:select * from 表名称;
查看表中某个字段或某些字段内容:select 字段名 from 库名.表名;
去重查询,查看表记录时,将重复的记录去掉:select distinct 字段名,字段名 from 库名.表名;
多条件查询:select 字段,字段 where 字段>值 and 字段<值; (查询表中某个字段记录大于/等于/小于某个值,且匹配别一个值的记录)
多条件查询:select 字段,字段 from 库名.表名 where 字段>值 or 字段>值; (查询表中某个字段记录值大于某个值,或者匹配于另一个匹配字段的值,只要符合任意一个就将字段记录显示出来)
去重:select distinct 需查看字段 from 表名;
区分大小写:select * from 表名 where binary 字段=值;
升序:select 字段 from 表名 order by 字段名 asc;
降序:select 字段 from 表名 order by 字段名 desc;
字段类型默认选项:default在数据类型右边标注即可
多表联查:select a.字段1,字段2 from (总表 as a inner join 表1 as b on 1.字段=b.字段) inner join 表2 as c on a.字段=c.字段 where 条件;
like模糊搜索:select * from 表名 where 字段名 like '数值%’;
范围运算:select * from 表名 where 字段 between 3 and 6; 查找3到6之间数值 字段后加not便是查找不是3-6之间的数值;
子查询:select 字段 from 表1 where 字段=(select 字段 from 表2 where 匹配值);
限定显示的条目:select * from 表名 limit 1,5; 从1开始查询5行的记录 0表示表中的第一条记录 所以这是查询2-6行的记录;若要取前五个则是0,5
算数运算字符串函数:
Sum()求和:select sum(字段) from 表名; 若需设置别名则在字段后as设置即可
avg()平均值:select avg(字段) from 表名 where 条件;
max() 最大值:select 字段1 from 表名 where 字段2=(select max(字段2) from 表名);
count()统计记录数:select count(*) from 表名 where 字段>40; 统计大于40的数量
update 表名 set 字段=字段+5 where 字段<40; 凡是字段小于40的字段加5 (+-*/)
转为大写输出:select upper(字段) from 表名 where 条件;
转为小写输出:select lower(字段) from 表名 where 条件;