SQL的一些基本操作语法

创建数据库:create DataBase   数据库的名字

创建表:

create table 表名
(
字段 类型 [约束]
字段 类型 [约束]
)

创建约束:

->建约束
alter table test add constraint PK_text_column primary key(_column) --主键约束
alter table test add constraint DK_text_column default(_value) for _column --默认约束
alter table test add constraint CK_test_column check(sex='男'or sex='女') --检查约束
alter table test add constraint UQ_test_column unique(_column) --唯一约束
alter table test1 add constraint FK_test1_test2_column foreign key (_column1) references


insert into 表名(字段名) values ('1','2','3','4')
select _column into _table_new from _table_old --要求表是存在的
insert into _table_new select * from _table_old --要求表是不存在的


delete from 表名 where 条件
truncate table 表名;--所以有数据归零
drop table _table
drop database _database


update 表名 set _column = _values,_column = _values where condition


->from 字句
->where字句
->group by 子句
->having 子句
->select top distinct
->order by

表与表的连接
->交叉连接
select * from _table1 cross join _table2;
->内连接
select * from _table1 inner join _table2 on ID=ID
->外联接
select * from _table1 left join _table2 on ID=ID
->自连接、多表连接
表join表on。。。join表on。。。

创建分页的存储过程

create proc 存储过程的名字

@pageIndex int=1, --页码(这里设置了默认值,也可以不用设置)
@pageSize int =5 ---页容量
as
select * from
(select ROW_NUMBER() over (order by stuid) as num,* from Student ) as temp
where num between (@pageIndex-1)*@pageSize+1 and @pageIndex*@pageSize

 

 

 

 

posted @ 2013-03-11 12:01  Suner  阅读(120)  评论(0编辑  收藏  举报