SQL总结

------SQL总结------

--1:建数据库

create database aaa

on

(

name=aaa_mdf,

filename='e:\aaa.mdf',

size=15,

maxsize=40,

filegrowth=5

)

log on

(

name=aaa_ldf,

filename='e:\aaa.ldf',

size=10,

maxsize=25,

filegrowth=5

)

use master

select name from sysdatabases          --查看有几个数据库

exec sp_dbremove database_name              --删除数据库

-------------------------------------------------------------------

--2:修改数据库属性

exec sp_dboption 'aaa','read only','true'

exec sp_dboption 'aaa','autostrink','true'

-------------------------------------------------------------------

--3:创建表

use aaa

create table student

(

stuid int identity(1,1) primary key,          ----主键约束

stuname nvarchar(20) unique not null,     ----唯一约束 非空约束

class_num int foreign key references class(class_num), ----外键约束

stuage int check(stuage>0 and stuage<100),     ----检查约束

sex char(5) default '男',     ----默认值

[primary key(stuid,stuname)]        --设组合键为主键

)

-------------------------------------------------------------------

--4:约束

exec sp_help             --查看默认约束名

exec sp_helpconstraint '表名'

exec sp_helptext

--(1)主键约束:

alter table student add constraint pk_stuid primary key(stuid)     --创建主键约束

alter table student drop constraint pk_stuid           --删除主键约束

alter table student add constraint pk_stuid_stuname primary key(stuid,stuname)    --设组合键为主键

--(2)外键约束

alter table student add constraint fk_class_num foreign key(class_num) references class(classnum)

alter table student drop constraint fk_class_num

/*特别注意:  外键必须是被引用表的主键*/

--(3)唯一约束

alter table student add constraint uq_stuname unique(stuname)

alter table student drop constraint uq_stuname

--(4)一次增加多个约束

alter table student add constraint pk_stuid primary key(stuid),constraint uq_stuname unique(stuname)

--(5)检查约束

alter table student add constraint check_age check(stuage>0 and stuage<100)

--(6)default 约束

alter table student add constraint df_sex default '男' for sex

-------------------------------------------------------------------

--5:数据的插入,删除,修改

--(1)插入

insert into student[(stuname,sex)] values('zhao','男')

select * into b /*(新表)*/ from a/*('源表')*/ where 1<>1

select * into test6 from test where 1<>1                --只复制了表结构没有数据

select * into test6 from test                --数据和表结构一起复制

insert into student

select /*字段列*/ from /*另一表*/

where 条件

--(2)删除

delete from student

where '条件'

delete from student                                    --删除全部数据

truncate table student

--注意: truncate 不能删除有外键约束的主表

--(3)修改

update student set name='',sex=''        --不加条件是全部修改

where '条件' 

--=================================================================

--6:全球唯一鉴别号

create table aa

(

id uniqueidentifier default newid(),

name char(10)

)

insert into aa(name) values('qian')

insert into aa values(newid(),'sun')

--=================================================================

--7:表结构修改

--(1)增加新列

alter table student add aa int not null check(aa>54)

--(2)删除列

alter table student drop column aa

--注意:删除列时必须保证基于该列的所有约束和索引(包括默认值约束)首先被删除掉

--(3)修改列

alter table student alter column stuname char(10) unique

/*  特别注意一:修改列时如果表里有数据就不能添加主键约束和唯一约束,

             因为表里有NULL值,必须是空表时才可以*/

/*  特别注意二:不能对有约束的列进行更改     

不对:   alter table student alter column stuname char(8) not null   */

--(4)表和表字段的重命名

exec sp_rename '旧表名','新表名'

exec sp_rename '旧字段名','新字段名','column'

--=====================================================================

--8:identity

set identity_insert 表名 on/off

--为 on 时可以自己输入数据

insert into student(num) values(58)    --58为自己设置的,此句有效

--为 off 时不可以

--=====================================================================

--9:连接

--(1)连接

select * from table1,table2

--等价于

select * from table1 cross join table2

select * from table1,table2

where table1.row=table2.row

--(2)自连接

--笛卡耳集    select * from emploly e1 ,emploly e2

select e1.name,e2.name from employ e1,employ e2

where e1.name=e2.name

--(3)内连接

select stuname as '姓名',classname as '班级' from student

inner join class on student.stuid=class.stuid

inner join '表名' on 条件             --连接多个表

--它等价于:

select stuname as '姓名',classname as '班级'

from student,class

where student.stuid=class.stuid

--------------操作过程----

1)从student 表中取出一条符合条件的记录

2)扫描 class 表,分别检查每条记录是否在连接属性上同表 student 取出的记录相等

3)相等就显示信息,继续从表 class 中取下一条记录,重复步骤2

--(4)外连接:(outer join)

允许限制一张表中的行,而不限制另外一张表中的行。

--注意:外连接不一定非要有外键约束

1: left outer join           --不能用left out join

左表中的记录全部会出现在结果集中,匹配不上的显示NULL

2: right outer join

右表中的记录全部会出现在结果集中,匹配不上的显示NULL

3: full outer join|full join      --不能用full out join

返回两个表中的匹配和不匹配的所有记录。

--=====================================================================

--10:查询

--(1)select

select * from student

where '条件'

--(2)order by

select * from student

order by stuname [asc|desc]     --asc 为升序

select * from student

order by stuid desc,stuname desc,classnum [asc]

--不能对数据类型为 text 或 image 的列使用 ORDER BY

--(3)group by

select * from student

group by classnum

having classnum>3

--(4)示例

use northwind

select * from customers

select city,count(*) from customers

where city in ('berlin','México D.F.')     --where 的位置不能放在 group by 之后,必须在其前边

group by city

having city in ('México D.F.','Berlin','Strasbourg')

order by count(*) desc

/*group by的特征:1.能按列分组 2.同聚合函数一起用,为每一组产生一个值。对数据分组时,也可以用条件

进行选择,这就是having子句。having子句与where子句相似,都是给出查询条件,所不同的是,where子句是

检查每条记录是否满足条件,而having子句是检查分组之后的个组是否满足条件。having子句是针对group by子

句的,没有group by 子句时不能使用having子句*/

/*1)where子句排除不满足条件的行

  2)group by子句收集满足where子句的搜索行,并将哪些行分组。

  3)having 子句排除不符合条件的组。*/

--group by all 能够显示所有组,即使是被where子句排除的组也将显示,不过不进行计算,计算值补null

---------------------------------------------------------------------------------------------USE Pubs

SELECT title_id +':'+ title + '->' + type

FROM titles

--(5)其他

--1:top子句

top n [percent]           --percent 为可选项

select top 5 *|'字段名' from student

/*如果一个select 语句既包含top又包含order by,那么返回的行将会从排序后的结果集中

选择。整个结果集按照指定的顺序建立并且返回排号序的结果集的前n行*/

--2:as子句

--表也可以用别名标识,尤其是自连接时

select * from student as s1,student as s2

--3:count函数

count(*) 返回组中项目的数量,这些项目包括 NULL 值。

COUNT(列名) 返回该列非空值的数量  --注意:是非空值

--4:模糊查询

--注意:模糊查询时,中文用两个%,即%%

%,_,[],[^]

--5:运算符

BETWEEN…AND… ,NOT BETWEEN…AND…

IN (项1,项2……), NOT IN (项1,项2……)

LIKE、NOT LIKE

IS NULL、IS NOT NULL

NOT、AND、OR

>、>=、=、<、<=、<>、!>、!<

 

--===========================================================================================

<script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/buttonLite.js#style=-1&uuid=&pophcol=3&lang=zh"></script> <script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/bshareC0.js"></script>
阅读(738) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值