您的位置:首页 > 数据库

SQL的一些基本操作语法

2013-03-11 12:01 681 查看
创建数据库: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: