您的位置:首页 > 其它

alter添加/select查询【今天真是个奇迹啊,老师提前10分钟讲完了,我们终于能按点下课了!!!!】

2012-10-24 13:28 786 查看
1.查询表Employee

select*from Employee

2.添加字段

insert into Employee(EmpName,EmpGender,EmpAge,EmpDpmId)--部门EmpDpmId

valuse(N'张兰','女','20',3)

3.

删除表--drop table Employee

——————————————————————————————————

修改字段名:

4.添加列--手动增加一列EmpDpmId

alter table Employee and EmpDpmId int

alter table Employee and EmpAddress nvarchar

5.删除列--手动删除一列EmpAddress

alter table Employee drop column EmpAddress

—————————————————————————

6.修改数据类型--手动修改EmpEmail的数据类型为varchar(200)

alter table Employee alter column EmpEmail varchar(200)

—————————————————————————

手动添加约束

alter table 表名 add constraint 约束名

unique(字段名)------唯一约束

check(约束表达式)-------检查约束

default(值)---------默认约束

primary key (字段名)-----------主键约束

foreign key (字段名) reference 表名(字段)---------外键约束

on Delete cascade/on action-----------添加级联

删除约束---------删除约束--又单击--删除

alter table 表名 drop constraint 约束名

7.前提是自增长,但不是主键

-1-主键(primary key)约束[constraint约束]

alter table Employee add constraint PK_Employee_EmpId primary key(EmpId)

-2-为EmpName增加唯一(unique)约束

alter table Employee add constraint UQ_Employee_EmpNmae unique(EmpNmae)

为EmpName删除唯一(unique)约束

alter table Employee drop constraint UQ_Employee_EmpNmae

-3-为EmpName增加非空约束

alter table Employee alter column EmpName varchar(50) not null

-4-为性别增加默认(default)约束,使默认值为"男"

alter table Employee add constraint DF_Employee_EmpGender default('男') for EmpGender

-5-为年龄增加检查(check)约束 0-120含0和120

alter table Employee add constraint CK_Employee_EmpAge check(EmpAge>=0 and EmpAge<=120)

为性别增加检查约束 非男即女

alter table Employee add constraint CK_Employee_EmpGender check(EmpGender='男' and EmpGender='女')

-6-为员工表增加约束

首先设置部门表中的DepId为主键,并且外键不能为空

alter table Department add constraint PK_Department_DepId primary key(DepId)

增加外键(foreign key)约束

右击设计----设置不能为空

alter table Employee add constraint PK_Employee_EmpDepId foreign key(EmpDepId) references Department(DepId) on delete cascade

添加级联on delete cascade

删除外键

alter table Employee drop constraint PK_Employee_EmpDepId

--1条语句删除多个约束,约束名用逗号隔开

alter table Employee drop constraint PK_Employee_EmpDepId,CK_Employee_EmpAge,UQ_Employee_EmpNmae

--1条语句为表增加多个约束

alter table Employee add constraint UQ_Employee_EmpNmae unique(EmpNmae),constraint CKSSS check(EmpAge>=0 and EmpAge<=150)

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

select查询

----修改字段名

select

FName as 姓名,

FAge 年龄,

性别=FGender

---select后可以直接跟一个表达式,而不是from......

select 学生总人数=40,学生总分数=320

select '哈哈','呵呵','嘻嘻','嘿嘿'

---获取MyStudents表中年龄最大的前5个人

select top 5 *form MyStudents order by FAge DESC

---获取MyStudents表中年龄最大的20% 降序排序

select top 20 percent *form MyStudents order by FAge DESC

--distinct去掉重复数据

create table Test2

(

autoId int identity(1,1),

uname nvarchar(20),

uage int,

ugender nchar(2)

)

insert into Test2

select '姚洪波',20,'男' union all

select '李伟',26,'男' union all

select '陈红军',28,'男' union all

select '辛兴涛',16,'男' union all

select '姚洪波',21,'男' union all

select '李伟',27,'男' union all

select '陈红军',28,'男' union all

select '辛兴涛',16,'男' union all

select '姚洪波',22,'男' union all

select '李伟',28,'男' union all

select '陈红军',28,'男' union all

select '辛兴涛',16,'男'

select * from Test2

--因为id不重复所以记录不变

select distinct * from Test2

--去掉姓名,年龄,性别重复的记录

select distinct uname,uage,ugender from Test2

--去掉姓名,性别重复的记录

select distinct uname,ugender from Test2

-----聚合函数

--聚合函数对null不进行计算

select max(FMath) as 数学最高分 from MyStudents

select min(FMath) as 数学最低分 from MyStudents

select sum(FMath) as 总分 from MyStudents

select avg(FMath) as 平均分 from MyStudents

select count(*) as 总人数 from MyStudents --count(*)包含对null的计算

-----子查询

select

最高分=(select max(FMath) from MyStudents),

最低分=(select min(FMath) from MyStudents),

总分=(select sum(FMath) from MyStudents)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐