码迷,mamicode.com
首页 > 数据库 > 详细

MS SQL SERVER数据库简单回顾

时间:2015-01-16 20:36:21      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

MS SQL SERVER数据库

1.创建数据库
create database javateam;
 
2.使用数据库
use javateam;
 
3.创建表
create   table   表名 
(
    字段名    字段类型   主键   字段增长(从X开始  , 每次加X个),
    字段名  字段类型,
    字段名  字段类型
);
 
 
create table peopleNone

(

    pid int primary key identity(1,1),

    pname varchar(32),

    psex varchar(32)

);

 

 
 
4.查询表
--select * from peopleNone;
--select distinct pname,psex from peopleNone;
 
5.添加数据
insert into peopleNone(pname,psex) values(MR.MA,male);

insert into peopleNone(pname,psex) values(MR.LU,female);

 

 
 
6.删除表
(1)删除表,删除了表的结构
drop table peopleNone;
(2)清除表的数据,没有删除表结构
truncate table peopleNone;
(3)根据条件删除
delete from peopleNone where pname = ‘MR.MA‘;
 
7.修改表
update peopleNone set pname = ‘MR.LU‘,psex = ‘male‘ where pid = 2;
 
8.添加约束
(1)语法:
alter table 表名
add constraint 约束名 foreign(外键) key references 主键表(主键)
alter table stu_info
add constraint fk_s foreign key(sid) references stu_table(sid);
 
(2)添加数据
insert into stu_table(sname,sclass)values(土豪zhang,2);

insert into stu_info(sid,sphone)values(2,12111);

 

(3)更新,更新的SID号需要在主键中存在
update stu_info set sphone = 8955 where sid = 1;
(4)删除,当主外键形成约束时,删除主键会异常,需要先删除外键
delete from stu_info where sid = 1;
(5)联合查询
select sname,sclass,sphone from stu_table s,stu_info b where b.sid = s.sid;
 
9.复合查询(子查询)
1.关键字:and or not
select count(*) as 记录条数 from ablum;


select * from ablum where aid between 1 and 3;


select * from ablum where not aid = 2;

 

2.模糊查询
语法:like a% %a%  %a  a_ (类似正则表达式)
select * from ablum where aname like ‘%f%‘;
3.其他
语法:最大最小 ,max min
select min(aid) as ‘最小值‘ from ablum;
语法:复合查询,规避。。排除前三
select * from ablum where aid not in
(select top 3 aid from ablum);
4.分页
语法:
select top(页面长度) * from 表名 where 主键 not in
(select top(页面长度 * (页码-1))主键 from 表名);
select top(4) * from ablum where aid not in

(select top(4 * (2-1)) aid from ablum);

 

MS SQL SERVER数据库简单回顾

标签:

原文地址:http://www.cnblogs.com/pengjunwei/p/4229509.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!