sql语句的复习
说来惭愧,写了这么多年代码,sql语句都忘记差不多了。
基础
- CREATE DATABASE database-name
- drop datebase dbname
- 备份数据库:mysqldump -u 用户名 -p 数据库名>导出的文件名
导入数据库:
导入数据库:
先进入sql mysql -u 用户 -p
mysql>use 目标数据库
mysql>use news
mysql>source news.sql - create table tablename(col1 type1 [not null][primary key],col2 type2 [not null],…)
根据已有的表创建新表:
create table tab_new like tab_old.
create table tab_new as select col1,col2..from tab_old definition only - drop table tabname
- alter table tabname add column col type
- alter table tabname add primary key
alter table tabname drop primary key - create [unique] index idxname on tabname
drop index idxname - create view viewname as select statement
drop view viewname 几个简单的基本的sql语句
选择:select * from table1 where 范围
插入:insert into table1(field1,field2)values(value1,value2)
删除:delete from table1 where 范围
更新:update table1 set field1=value1 where 范围
查找:select * from table1 order by field1,field2[desc]
排序:select * from table1 group by filed1,filed2[desc]
总数:select count as totalcount from table1
求和:select sum(filed1) as sumvlaue from table1
平均:select avg(filed1)as avgvalue from table1
最大:select max(field1) as maxvalue from table1
最小:select min(field1) as minvalue from table1几个高级查询运算词
A:UNION运算符
通过组合其它两个结果表并消去表中任何重复行而派生出一个结果表
B:EXCEPT 运算符
EXCEPT运算符通过包括所有在table1中但不在table2中的行并消除所有重复行而派生出一个结果表。
C:INTERSECT运算符
INTERSET 运算发通过只包括table1和table2中都有的行并消除所有重复而派生出一个结果表。- 使用外连接
A:left join:
左外连接:结果集几包括连接表的匹配行,也包括左连接表的所有行
B:rigth join:
右外连接:结果集既包括连接表的匹配行,也包括右连接表的所有行
C:full join
全外连接:不仅包含符号连接表的匹配行,还包括两个连接表中的所有记录 - 分组:group by
一张表,一旦分组完成后,查询后只能得到组相关的信息。
组相关的信息:count、sum、max、min、avg - 13.