sql语句的复习

来源:互联网 发布:淘宝网怎么装修店铺 编辑:程序博客网 时间:2024/05/17 07:34

sql语句的复习

说来惭愧,写了这么多年代码,sql语句都忘记差不多了。

基础

  1. CREATE DATABASE database-name
  2. drop datebase dbname
  3. 备份数据库:mysqldump -u 用户名 -p 数据库名>导出的文件名
    导入数据库:
    导入数据库:
    先进入sql mysql -u 用户 -p
    mysql>use 目标数据库
    mysql>use news
    mysql>source news.sql
  4. 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
  5. drop table tabname
  6. alter table tabname add column col type
  7. alter table tabname add primary key
    alter table tabname drop primary key
  8. create [unique] index idxname on tabname
    drop index idxname
  9. create view viewname as select statement
    drop view viewname
  10. 几个简单的基本的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

  11. 几个高级查询运算词
    A:UNION运算符
    通过组合其它两个结果表并消去表中任何重复行而派生出一个结果表
    B:EXCEPT 运算符
    EXCEPT运算符通过包括所有在table1中但不在table2中的行并消除所有重复行而派生出一个结果表。
    C:INTERSECT运算符
    INTERSET 运算发通过只包括table1和table2中都有的行并消除所有重复而派生出一个结果表。

  12. 使用外连接
    A:left join:
    左外连接:结果集几包括连接表的匹配行,也包括左连接表的所有行
    B:rigth join:
    右外连接:结果集既包括连接表的匹配行,也包括右连接表的所有行
    C:full join
    全外连接:不仅包含符号连接表的匹配行,还包括两个连接表中的所有记录
  13. 分组:group by
    一张表,一旦分组完成后,查询后只能得到组相关的信息。
    组相关的信息:count、sum、max、min、avg
  14. 13.
原创粉丝点击