您的位置:首页 > 数据库

SQL Server -数据库基本语法的总结

2011-03-22 00:03 429 查看
-------------创建数据库和标
create database 数据库名
on
(
name = 主要数据文件名,
filename = 主要数据文件存放路径(注; 保证存放目录事先存在,若不存在,得先手动创建某目录),
size = 初始大小,
maxsize = 最大大小,
filegrowth = 文件增长量(注:默认为兆字节的方式,可以指定为百分比增长)
)
log on
(
name = 日至文件名 (注: 不能和主要数据文件同名),
filename = 日至文件存放路径(注: 和主要数据文件存放路径相同),
size = 初始大小,
maxsize = 最大大小,
filegrowth = 文件增长量(注:默认为兆字节的方式增长,可以指定为百分比增长)
)
go
------------------------使用数据库
use 数据库名
-----------------------创建数据库表
create table 数据库表名
(
字段1 数据类型 约束,
字段2 数据类型 约束,
,

)
go
------------示例
-----创建数据库
create database Manager
on
(
name = Manage_data,
filename = 'd:/Manage_data.mdf',
size = 10mb,
maxsize = 20mb,
filegrowth = 10
)
log on
(
name = Manage_log,
filename = 'd:/Manage_log.ldf',
size = 5mb,
maxsize = 10mb,
filegrowth = 5%
)
---------使用数据库
use Manager
---------创建数据库表
create table Boss                  ----------创建boss表,包含有 bid , bname , bsex , bage四个字段
(
BID int primary key identity(1,1),
BName varchar(50),
BSex varchar(20),
BAge int not null
)
go
------删除表
drop table boss                     -----------删除表后,表结构被删除
delete from boss                    -----------一行一行记录删除,不能在delete后接字段名,可接where子句限制,速度慢,表结构存在
truncate table boss                 -----------一次删除表中的所有记录,表结构存在,速度快,但对于有外键的表只能用delete from 而不能用truncate table
--------------
go
create table Employee               ---------------创建Employee 表,包含有eid,ename,esex,eage四个字段
(
EID int primary key identity(1,1),
EName varchar(50) default('zhangsan'),
ESex varchar(20),
EAge int not null
)
go

---------------------向表中间插入数据
------单行插入
insert [into] 表名(字段名) values(值)
------多行插入
insert into 表1[(字段)] select 字段 from 表2                      ---------将表2中的所有记录插入到表1中,要求这两个表事先存在
select 字段1,字段2  into 新表 from 旧表                     ----------创建新表,同时将旧表中的所有记录插入到新表中,新表不含标识列
select 字段1,字段2 标识列字段(数据类型,标识起始值,标识增长量) [as 别名] into 新表 from 旧表            ---------创建新表,同时将旧表中的所有记录插入到新表中,含标识列字段
insert into 表(字段1,字段2)
select '值1','值2' union
select '值a','值b' union
select '值3','值4' union
select '值5','值6'

-----------------------------------------示例
----------------为Boss表插入数据
insert into Boss(BName,BSex,BAge) values('zcb','男',21)                        -----常规方式单行插入
insert Boss values('cxm','女',20)                                              -----插入时省略 into 和 字段名,注意:不能插入标识列
insert into Boss(BName,BSex,BAge)                                              -----通过union 联合关键词,向表中一次插入多行
select 'xrr','女',20 union
select 'xmm','女',19 union
select 'tangbi','男',26 union
select 'zhanliu','男',25
select BName , BSex , BAge into 新表1 from Boss                                  ----建立新表,同时将Empoyee表中的内容一次插入到新表中,新表不包含标识列
select BName, BSex,BAge , identity(int,1,1) as 老板编号 into 新表2 from Boss      ----------建立新表,同时将Empoyee表中的内容一次插入到新表中,新表包含标识列
go

-----------------为表Empoyee插入数据
insert into Employee(EName,ESex,EAge) values('aaa','男',21)                        -----常规方式单行插入
insert Employee values('bbb','女',20)                                              -----插入时省略 into 和 字段名,注意:不能插入标识列
insert into Employee(EName,ESex,EAge)                                              -----通过union 联合关键词,向表中一次插入多行
select 'ccc','女',20 union
select 'ddd','女',19 union
select 'eee','男',26 union
select 'fff','男',25

insert into Employee(ESex,EAge)                                              -----通过union 联合关键词,向表中一次插入多行。注意:采用默认值的时候,不为该列插入
select '女',20 union
select '女',19 union
select '男',26 union
select '男',25
insert into Employee(EAge) values(22)                                         ------为Empoyee表插入一行数据. 注意: 向Empoyee表中插入一行数据,对EName 采用默认值的方式,对ESex 采用为空的形式
select EName , ESex , EAge into 新表3 from Employee                                 ----建立新表,同时将Empoyee表中的内容一次插入到新表中,新表不包含标识列
select EName, ESex,EAge , identity(int,1,1) as 雇员编号 into 新表4 from Employee      ----------建立新表,同时将Empoyee表中的内容一次插入到新表中,新表包含标识列
go

-----------------------------查询表中的数据
-------简单的查询
select * from Boss                            -----------查询表中所有记录
select * from Employee
go
select BName,BSex from Boss                   ----------查询某些字段的记录
go
select * from Boss where BSex = '女' and BAge< 20        --------查询满足某些条件的所有记录
go
select BName ,BSex from Boss where Bsex = '男' and BID > 4    ----------查询满足某些条件的某些字段的记录
go
select * from Boss order by BAge desc  --asc                  ----------查询表中所有字段,并且按BAge进行排序,默认为升序排列,如果想按降序排列,则须指定BAge 为 desc
go
select * from Boss where BSex = '男' order by BAge desc       ----------查询表中满足条件的所有字段,并按BAge 进行排序。注意:order by 一定是写在where 条件的后面

------模糊查询(五种方式)
----1 . like 模糊查询
select * from Boss where BName like 'z_'
select * from Boss where BName like 'z%'
select * from Boss where BAge like '[0-9]'
select * from Boss where BAge like '[0-9][0-9]'
select * from Boss where BName like '%[^A-Z]%'
------查询字段时重命名字段(给字段命别名)
select * from Employee
go
select EID as '雇员编号', EName as 雇员姓名,ESex as '雇员性别', EAge as 雇员年龄 from Employee    -------给字段起别名的三种方式,注意对比.  别名可以用 '' 也可以不用
select EID '雇员编号', EName 雇员姓名,ESex '雇员性别', EAge 雇员年龄 from Employee
select '雇员编号' = EID, 雇员姓名 = EName,'雇员性别' = ESex, 雇员年龄 = EAge from Employee
-------查询是否为空的值
select * from Employee where ESex is NULL
select * from Employee where ESex is not null
-------查询常量值
select EID as '雇员编号', EName as 雇员姓名,ESex as '雇员性别', EAge as 雇员年龄 , '雇员地址' = '北京海淀' ,'空值' = ''from Employee      --------只是查询时存在,并没有改变表的结构
-------查询限定行数的值
select top 6 * from Employee
select top 4 EName,EAge from Employee
select top 50 percent * from Employee
select top 25 percent EID , ESex from Employee                       -------如果百分比其结果为小数,取整
------查询后排序  ( order by 字段 asc/desc )
select * from Boss order by BAge desc  --asc                  ----------查询表中所有字段,并且按BAge进行排序,默认为升序排列,如果想按降序排列,则须指定BAge 为 desc
go
select * from Boss where BSex = '男' order by BAge desc       ----------查询表中满足条件的所有字段,并按BAge 进行排序。注意:order by 一定是写在where 条件的后面

---------------------字符串函数,日期函数,数学函数,系统函数--------------------------------------

----------------更新表中的数据
update 表名 set 字段名 = 新值 where 条件
-------------示例
select * from Boss
update Boss set BName = 'zhaoliu' where BID = 6

-----------------删除表中数据
drop table 表名               --------删除整个表 ,表结构被删除
delete from 表名 [where 条件]  --------删除表中的数据(与truncate table 的区别见前面)
truncate table 表名           ---------删除表中的数据
----------------示例
delete from Boss where BID = 6
delete from Boss
truncate table Boss
----------------------------------------------聚合函数--------------------------------

----------------分组查询
select * from 表名 group by 字段
select * from 表名 where 条件 group by 字段         -----错误
select 字段1,字段2 from 表名 where 条件 group by 字段1,字段2
select 聚合函数,字段1,字段2 from 表名 where 条件 group by 字段1,字段2
----------------示例
select * from Boss
select * from Boss group by BName                ----错误
select BName , BAge from Boss group by BName,BAge   -------一定要对所查询的所有字段都进行group by
select avg(BAge),max(BName),count(*) from Boss

select avg(BAge),max(BName),count(*) , BName from Boss group by BName
select avg(BAge),max(BName),count(*) ,BName from Boss where BAge < 28 group by BName               -----注意:group by一般在where后面
-----------------摔选 ( having )    注意having 一定是在group by的后面
select avg(BAge),max(BName),count(*) ,BName from Boss where BAge < 28 group by BName having BSex <> '女'   ------错误
select avg(BAge),max(BName),count(*) ,BName from Boss where BAge < 28 group by BName having BName <>'xmm'

------------------------------查询语句的一般形式
select 字段名/ from 表名
[where 条件]
[group by 字段]
[having 字段]
[order by 字段]

------------------修改表结构(添加一列、删除一列、 添加约束等)    二期内容,大家有一定的了解就行,到二期学的时候再具体理解
alter tabel 表名
add 字段 数据类型 约束
----------示例
create table a
(
name varchar(20),
sex varchar(20)
)
alter table a
add id int primary key identity(1,1)
----删除一列
alter table 表名
drop column 列名

----------增加约束
----主键约束
alter table 表名
add constraint 约束名 primary key 表名(主键字段名)
----检查约束
alter table 表名
add constraint 约束名 check('表达式')
----外键约束
alter table 表名
add constraint 约束名 foreign key 外键表(外键字段) references 主表(主表主键字段名)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: