数据库

架构的作用常常用作一些权限,安全限制,默认的架构是dbo

如果自定义名义与关键字冲突,或中间需要有空格等,
则使用方括号将其括起来
create table [int]
(
id int
)
---------------------
写SQL语句,检查一下两张表的数据

use MyFirstDaBase
go

select * from dbo.tblDepartment
select * from dbo.tblEmployee

修改数据的EmpepId为5
update tblEmployee set EmpPeId=5

将所有人年龄改为-1岁
update tblEmployee set EmpAge=-100

添加约束 为了保证数据的完整性

删除数据 drop table 表名 ----是删除表
delete from 表名 清除表中数据
truncate table 表名(将清楚所有的数据,包括所有动作不计入日志) 清除表中数据
--------------
添加约束 主键约束primary key constraint唯一且不为空
外键约束foreign key constraint 表关系

限定长度 len(长度)=11

datetime getdate()获得当前时间
--------------------------------
主键约束 paimary key 命名PK
唯一约束 unique 命名UQ
检查约束 check() 命名CK
默认约束 default() 命名DF
1,创建表的时候就创建约束
create table tblCleateConstraint
(
id int primary key, 设置主键约束
name nvarchar(10) unique, 设置唯一约束
age int check(age>0 and age<100), 设置check约束
joindate datetime default(getdate()) 设置默认,getdate()是获得当前时间的函数
)

insert into dbo.tblCreateConstraint(id,name,age,joindate) values(2,'张三',18,'2012-02-02');
--------------------------
2,创建表以后,修改创建约束
create table tblCleateConstraint2
(
id int ,
name nvarchar(10) ,
age int ,
joindate datetime
)
凡是修改表语法为alter table 表名 动作
alter table dbo.tblCreateConstraint2
add constraint CK_tblCreateConstraint2_age
check(
age>0
and
age<100
)

alter table dbo.tblCreateConstraint2 drop constraint CK_tblCreateConstraint2_age //删除约束

添加主键约束
alter table dbo.tblCreateConstraint
add constraint
PK_tblCreateConstraint2_id
primary key(id)
唯一约束
alter table dbo.tblCreateConstraint2
add constraint
UQ_tbcCreateConstraint2_name
unique(name)

默认约束
alter table dbo.tblCreateConstraint2
add constraint
DF_tblCreateConstraint2_joinDate
default(getDate())for joinDate
-------------
查看所有的约束
selecct * from sys.objects

------------------------------
外键
1,用设计器
2,用代码
2.1创建表的时候,创建约束create table MainTable 先创建主键表
(
Fid int primary key,
name nvarchar(10)
)

create table ForTable
(
id int primary key,
Fid int foreign key references MainTable(Fid)
)

2.2
创建表以后修改添加外键约束
create table MainTable 先创建主键表
(
id int primary key,
name nvarchar(10)
)

create table ForTable
(
id int primary key,
Fid int
)
alter table ForTable
add constraint
FK_ForTable_MainTable_Fid
foreign key(Fid) references MainTable(FId)


create table Mytable
(
id int identity(1,1) primary key,
name nvarchar(10)
)

------------------下午------------------
查询语句

先插入一些数据
insert into dbo.tblEmployee
(

)values()
查询
select * from 表名
需要对数据进行检索
select * from 表名 where 条件
----------
筛选检索的字段
select 需要查找的 from 表名 where 条件
添加列的别名
1,列as 别名
2,列 空格 别名
3,别名=列
select EmpId as 员工编号, EmpName 员工姓名,员工年龄=age from 表名 where 条件

select '我是另加的' as 另加的数据,EmpId as 员工编号, EmpName 员工姓名,员工年龄=age from 表名 where 条件
select 有一个特殊能,就是显示数据 print也可以显示数据
seleect '我是select'
print '我是print'

---------------------
添加一个年龄排序
使用order by 字段名
在后面使用asc和desc表示升序和降序排
select * from 表名 order by 字段名

select * from 表名 order by 字段名 desc

select top(3) * from 表名 order by 字段名 //表示查询前3个

select top(3)percent * from 表名 order by 字段名 //表示查询前3%

------------------
去除重复的数据 distinct
select distinct * from 表名 (注意id是唯一约束,实际使用时别用*)
------------------------------- 聚合函数
常见的有求和(SUM),求平均(AVG),求最大小(MAX,MIN),求总(COUNT)

COUNT()求符合条件的数据条数
select COUNT(要求总数的符合条件的字段) from 表名 where 条件

MAX求最大
select MAX(字段) from 表名
聚合函数常常与分组一起使用
group by 字段
group by中有的字段,才能出现在select语句中
group by对数据进行分组,但是应该注意分组的数据才允许select检索
group by 应写在from后面,但写在order by前面

聚合函数的一个注意事项
create table school
(
id int identity(1,1) primary key,
math int,
english int ,
chinese int
)
insert into school(math,english,chinese)values(10,30,64);
select * from school
select sum(math) from school
select SUM(math)/COUNT(id) from school 在处理聚合数据的时候,不计入空值数据
select COUNT(math) from school

SQL中null表示不知道
------------------------------
范围条件和集合条件 年龄在18到23岁之间的所有员工
使用between and表示条件范围
字段between 数值1 and 数值2
select * from 表名 where 字段 between 18 and 23

求出年龄在17到18岁和21岁的人
字段 in(数值)
select * from 表名 where 字段 in(17,18,21)
------------------
模糊查询 数据库的通配符
_ 单个字符
% 多个字符包含0个
使用like select * from 表名 where 字段 like '__虎'; 我要匹配***_log.ldf
...like'___[_]log.ldf'
在方括号中[^]也表示否定,就是表示不出现这个元素的
...name like'[^赵]%'
...name not like '赵'

简单总结一个条件一些内容
在SQL中,使用三值逻辑(true,false,unknow)
> = < >= <= <> != !< !>
由于null的存在
select * from tblScore
select isNUll(math,0)from 表名 //把math为Null的改为0--求出数学成绩为null的人
select * from 表名 where math is null

insert into tblScore(math,english,chinese) values(111,65.60)
--------------------- 如果有select中的数据进行筛选,需要放在having

聚合函数不能放在where中,因为聚合函数是对结果的聚合
where是在查询前进行筛选
having是在查询后进行筛选
having是group by 的条件对分组后的数据进行筛选,having用在group by 后
注意having中不能使用未参与分组的列,having不能替代where。作用不一样,having是对组进行过滤


group by 是分组
order by 是根据条件排序






posted @ 2012-09-18 00:31  美国如来不如中国上帝  阅读(141)  评论(0编辑  收藏  举报