mysql sql语句

1、说明:创建数据库

CREATE DATABASE database-name

2、说明:删除数据库

drop database dbname(数据库名)

3、说明:备份sql server

----创建备份数据的device

USE master

EXEC sq_addumpdevice 'disk' , 'testBack' , 'd\mysql.dat' //、尾巴后面数据库名字

4、说明:创建新表

create  table 表名 (col1 type1 [not null] [primary key],col2 type2 [not null])

根据已有的表创建新表

A:create table tab_new like tab_old(使用旧表创建新表)

B:create table tab_new as select col1,col2 from table_old definition only

5、说明:删除新表

drop table 表名

6、说明:增加一个列

Alter table 表名 add colunmn col type 

注:列增加后将不能删除。DB2中列加上后数据类也不能该表,唯一能改变的是增加varchar的长度

7、说明:添加主键:Alter table 表名 add primary key(col)

说明:添加主键:Alter table 表名 drop primary key(col)

8、说明:创建索引:create(unique)index idxname on 表名(col)

删除索引:drop index idxname

注:索引是不可能更改的,想更改必须删除重新新建

9、说明:创建视图:create view viewname as select statement

删除视图:drop view viewname

10、说明:几个简单的基本的sql语句

查看:select * from 表名 where 范围

增加:insert into 表名(name1,name2) values(zss,lss)

删除:delete from 表名 where 范围

更新:update 表名 set  id=1 where 范围

查询:select * from 表名 field1 like ‘%value1%’ 

排序:select * from 表名 order by field1,field2[desc]

总数:select count as totalcount from 表名

求和:select sun(field) as sumvalue from 表名

平均:select avg(field) as avgvalue from 表名

最大:select max(field) as maxvalue from 表名

最小:select min(field) as minvalue from 表名

11、说明:几个高级查询运算词

A:UNION运算符

UNION 运算符通过组合其他两个结果表(例如table1和table2)并消去表中任何重复行而派生出一个结果表。当ALL随union 一起使用时(既union all),不消除重复行。两种情况下,派生表的每一行不是来自table1就是来自table2。

B:EXCEPT运算符

EXCEPT运算符通过包括所有在table中但不再table2中行并消除所有重复行派生出一个结果表。当all

随EXCEPT一起使用时(EXCEPT ALL),不消除重复行。

C:INTERSECT运算符

INTERSECT运算符通过只包括table1和table2中都有的行并消除所有重复行而派生出一个结果表。当ALL随EXCEPT一起使用时(INTERSECT ALL),不消除重复行。

注:使用运算词的几个查询结果行必须是一致的。

12、说明:使用外连接

A、left(outer) join:

左外连接(左连接):

待更新

posted @ 2017-12-28 15:28  丶趁年轻  阅读(156)  评论(0)    收藏  举报