MySql高级查询

来源:互联网 发布:淘宝店铺严重违规12分 编辑:程序博客网 时间:2024/05/17 21:53

1.修改表
修改表名:alter table oldname RENAME newname
添加字段:alter table 表名 add 字段名 数据类型…
修改字段:alter table 表名 CHANGE 原字段名 新字段名 数据类型…
删除字段:alter table 表名 drop 字段名
添加主外键:主键: alter table 表名 add [constraint 主键名] primary key (主键字段)
组合主键:alter table 表名 add [constraint 主键名] primary key (主键字段1,字段2,…)
外键: alter table 表名 add constraint 外键名 foreign key (外键字段) references 主键表名(主键字段)
删除主外键:主键: alter table 表名 drop primary key
外键: alter table 表名 drop foreign key 外键名
2.插入数据
insert into 表名 (字段名列表) values(值列表),(值列表)….
查询语句插入新表:
select 字段,字段… into 新表 from 原表 (要先创建表)
insert into 新表 (字段列表) select 字段,字段… from 原表
create table 新表 (select 字段,字段,… form 原表); 自动创建新表
3.更新数据
update 表名 set 字段=值,字段=值,字段=值,….. (where 条件)
4.删除数据
delete from 表名 (where条件)
删除表中所有行 truncate table 表名
5.LIMIT子句
放在句子最后
LIMIT [位置偏移量,][行数] 例:limit 4,4 从数据第5条开始读取5个数据
6.常用函数
AVG()平均值 count()行数 max()最大值 min()最小值 sum()字段的和
NOW()获取当前时间 YEAR()获取年
7.子查询
select 字段,字段… form 表1 where 字段 比较运算符 (子查询);
IN(NOT IN)
select 字段,字段… form 表 where (not) exists (子查询)

select (子查询),字段 from 表

原创粉丝点击