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)
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)
相关文章推荐
- case when then和insert select组合查询添加记录
- 暴感谢大山老师的乐理视频教学,我今天真是走狗屎运了
- thinkphp关于文章添加的时间筛选查询(今天,一周内,一个月内,三个月内,大于三个月)
- case when then和insert select组合查询添加记录
- eclipse插件Maven添加依赖查询无结果的解决方法(Select Dependency doesn't work)
- 这个程序我想添加过能否查询是哪个老师上?
- 今天我们小组终于把初赛文档搞定了
- 为SubSonic3.0的查询(SubSonic.Query.Select和存储过程)添加更多的执行功能
- 小白遇到老师了,终于搞明白, 当左边有一个田字格,怎么给右侧添加一个悬浮盒模型
- js 查询XML数据添加到HTML select控件
- mysql中select查询语句添加自增id
- eclipse插件Maven添加依赖查询无结果的解决方法(Select Dependency doesn't work)
- SELECT查询结果集中,如何能够动态添加一个标识列?
- MySQL 查询数据表里面时间字段为今天添加的计数
- SELECT查询结果集中,如何能够动态添加一个标识列?
- sql的select查询添加一个虚拟字段进去
- 今天南方学校的杨进老师叫我们写一下这些天来的心得体会
- eclipse插件Maven添加依赖查询无结果的解决方法(Select Dependency doesn't work)
- eclipse插件Maven添加依赖查询无结果的解决方法(Select Dependency doesn't work)
- 使用Oracle的instr函数与索引配合提高模糊查询的效率 一般来说,在Oracle数据库中,我们对tb表的name字段进行模糊查询会采用下面两种方式: 1.select * from tb wh