常见sql语句

来源:互联网 发布:手机淘宝试用在哪里 编辑:程序博客网 时间:2024/05/20 15:58
SQL分类: 
DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) 
DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT) 
DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)

 1、创建数据库的SQL语句:

复制代码
 1 create database stuDB  2 on  primary  -- 默认就属于primary文件组,可省略 3 ( 4 /*--数据文件的具体描述--*/ 5     name='stuDB_data',  -- 主数据文件的逻辑名称 6     filename='D:\stuDB_data.mdf', -- 主数据文件的物理名称 7     size=5mb, --主数据文件的初始大小 8     maxsize=100mb, -- 主数据文件增长的最大值 9     filegrowth=15%--主数据文件的增长率10 )11 log on12 (13 /*--日志文件的具体描述,各参数含义同上--*/14     name='stuDB_log',15     filename='D:\stuDB_log.ldf',16     size=2mb,17     filegrowth=1mb18 )

2、数据表的建立和删除: 
创建新表
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)           CREATE TABLE tab01 (name varchar (50), datetime default now ())   --时间的默认值
根据已有的表创建新表: 
A:create table tab_new like tab_old (使用旧表创建新表)                
B:create table tab_new as select col1,col2… from tab_old definition only
DROP TABLE 数据表名称(永久性删除一个数据表)
3、增加一个列:Alter table tabname add column col type
注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度
4、添加主键: Alter table tabname add primary key(col) 
说明:删除主键: Alter table tabname drop primary key(col) 
5、创建索引:create [unique] index idxname on tabname(col….)  
删除索引:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。
6、创建视图:create view viewname as select statement 
 删除视图:drop view viewname
7、创建游标:declare cursor_name cursor forselect_statement
打开游标:OPEN Cursor_name
遍历和获取游标:fetch next from cursor_name  into @id,@name  --将游标向下移1行,获取的数据放入之前定义的变量@id,@name中
使用游标:
关闭游标:CLOSE Cursor_name
释放游标:DEALLOCATE Cursor_name
8、创建存储过程:create proc proc_name 
  @gender nvarchar(10) [='男'],@age int [=30]
  as select _statement
9、创建触发器:create trigger trg_name on table_name for update as Transact-SQL
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 where field1 like ’%value1%’ ---like的语法很精妙,查资料!
排序:select * from table1 order by field1,field2 [desc]
总数:select count * as totalcount from table1
求和:select sum(field1) as sumvalue from table1
平均:select avg(field1) as avgvalue from table1
最大:select max(field1) as maxvalue from table1
最小:select min(field1) as minvalue from table1
11、几个高级查询运算词
A: UNION 运算符 
UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。
当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。 
B: EXCEPT 运算符 
EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当 ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。 
C: INTERSECT 运算符
INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。 
注:使用运算词的几个查询结果行必须是一致的。 
12、数据库中的连接:
内连接join:只连接匹配的行.select A.c1,B.c2 from A join B on A.c3 = B.c3;
左外连接(左连接)left join:结果集包括连接表的匹配行,也包括左连接表的所有行。SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c 
右外连接(右连接)right join:结果集既包括连接表的匹配连接行,也包括右连接表的所有行。 
全外连接full/cross join:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。
交叉连接:生成笛卡尔积——它不使用任何匹配或者选取条件,而是直接将一个数据源中的每个行与另一个数据源的每个行一一匹配。select A.c1,B.c2 from A,B;??
13、分组:Group by;使用group by 的目的就是要将数据分类汇总。
常用的聚合函数 :1  count    2   sum    3   avg    4   max    5   min 
  注意:select 后的字段,必须要么包含在group by中,要么包含在having 后的聚合函数里。
1. GROUP BY 是分组查询, 一般 GROUP BY 和聚合函数配合使用 group by 有一个原则,就是 select 后面的所有列中,没有使用聚合函数的列,必须出现在 group by 后面(重要)
2. GROUP BY 和 ORDER BY一起使用时,ORDER BY要在GROUP BY的后面,order by 的列,必须是出现在group by 子句里的列   
14、between说明:
between的用法,between限制查询数据范围时包括了边界值,
not between不包括 select * from table1 where time between time1 and time2 select a,b,c, from table1 where a not between 数值1 and 数值2
0 0
原创粉丝点击