1、创建数据库语句(以部门表(department)和员工表(employee)位例)

  create database MyCompany

  on

  (

name='MyCompany_data',

filename='c:\database\MyCompany_data.mdf',

size=5mb,

filegrowth=10%,

maxsize=100mb

  )

  log on

  (

name='MyCompany_log',

filename='c:\database\MyCompany_log.ldf',

size=1mb,

filegrowth=1mb,

maxsize=10mb

  )

2、创建表语句

 

create table department

(

dId int identity(1,1) primary key,

dName nvarchar(10) not null

)

go


--员工表 employee


create table employee

(

eId int identity(1,1) primary key,

eName nvarchar(10),

eSex nchar(1),

eAge int,

eSalary money,

eDepId int,

eIntime datetime

)

go

 

3、创建主键约束 

  alter table [表名] add 

    constraint [键名] Primary key [主键名称]

4、创建唯一约束

  alter table [表名] add

     constraint [键名] unique(唯一键名称)

5、创建默认约束

  alter table [表名] add

     constraint [键名] default(默认值) for [设为默认值的字段]

6、创建检查约束

  alter table [表名] check(要检查的条件)

7、创建外键约束

  alter table [表名] add

  constraint [外键名称(自己起的)] foreign key(表中的外键名称) references [与外键相关联的表名][相应表中的主键]

8、级联删除

  alter table [表名] add

  constraint [外键名称(自己起的)] foreign key(表中的外键名称) references [与外键相关联的表名][相应表中的主键] on delete cascade

删除的时候先删除字表中的数据,再删除父表中的数据

9、在表中插入数据

  insert into [表名](字段名称)values(对应字段的值)

10、删除数据

  delete from table where(条件)

    truncate table [表名]

  delete与truncate的区别:truncate删除数据的时候把自增长列的值还原成种子

  delete删除了数据如果再向表中插入数据,有自增长列的值的种子还会自增

  删除表 drop table [表名]

  删除数据库 drop database [数据库名]

11、修改数据

  update table set [要修改的数据] where 条件

12、查询数据

  最简单的查询:selcet [字段] from [表名]

  sql中最难的要属数据查询了,增删改相对来说都比较简单。

  --修改列名

select sName as '姓名',sAge as '年龄',sSex as '性别' from Student

select sName '姓名',sAge '年龄',sSex '性别' from Student

select '姓名'=sName,'年龄'=sAge from Student


--带条件查询

select sName as '姓名',sAge as '年龄',sSex as '性别' from Student

where sAge > 20

select 1+1 as 'mm'

select GETDATE() 'time'


--top

select top 3 * from Student


--百分之几

select top 3 percent sName,sAge,sBirthday from Student

--distinct

select * from Student

select distinct * from Student

select distinct sName from Student

select distinct sName,sNo from Student

select distinct sName,sSex from Student


--聚合函数

select * from Score

select COUNT(*) from Student

select MAX(english) from Score

select Min(english) from Score

select SUM(english)/COUNT(*) from Score


--avg没有统计null的值

select AVG(english) from score

select MAX(sbirthday),MIN(sbirthday) from Student


--对列计算

select sName,sAge + 5 as 'sAge',sSex from Student

select english*2,studentid from Score

--带条件查询

select studentId,english from Score where english<60

select * from Student where sAge >=20 and sAge <= 30

--Between…and …在之间


--查询年龄在20-30岁之间的男学生

select * from Student where sAge between 20 and 22 and sSex = '男'


--查询成绩在80-90分之间的所有学生

select * from Score where english between 80 and 90

select * from Student where sBirthday between '1988-1-1' and '1989-12-31' 


--in

select * from Student where sClassId = 1 or sClassId=2 or sClassId=3 or sClassId = 4

select * from Student where sClassId in (1,2,3,4)


--模糊查询  like

select * from Student where LEFT(sName,1) = '张'

select * from Student where sName like '张%'

select * from Student where  sName like '%侯%'

select * from Student where sName like '张__'

select * from Student where sName like '%[飞羽]%'--[0-9]  [a-z]  [123]


--null

select 1 + null

select * from Score where english is null

select * from Score where english is not null

--order by


--出现在select语句的最后

select * from Student order by sAge asc

select * from Student order by sAge desc

select top 5 * from Student order by sAge desc

select * from Score order by english asc,math asc

select * from Score where english >= 60 and math >= 60

order by english asc,math asc


--分组  group by

--select之后的列,必须出现在聚合函数或者group by子句中

select sClassId,COUNT(sId),MAX(sAge) from Student group by sClassId

select sSex,COUNT(*) from Student group by sSex

 

--having 对分组过后的数据进行筛选

select sClassId,COUNT(*) from Student group by sClassId having COUNT(*) > 3

13、数据库中常用的函数

  getdate();count(),avg(),max(),min(),sum()

14、函数总结

    1、ISNULL(expression,value)     如果expression不为null返回expression表达式的值,否则返回value的值

2、字符串函数

LEN() :计算字符串长度

LOWER() 、UPPER () :转小写、大写

LTRIM():字符串左侧的空格去掉 

RTRIM () :字符串右侧的空格去掉 

LTRIM(RTRIM('         bb        '))

LEFT()、RIGHT()  截取取字符串

SUBSTRING(string,start_position,length)

参数string为主字符串,start_position为子字符串在主字符串中的起始位置(从1开始),length为子字符串的最大长度。SELECT  SUBSTRING('abcdef111',2,3) 

REPLACE(string,oldstr,newstr)

3、日期函数

GETDATE() :取得当前日期时间 

DATEADD (datepart , number, date ),计算增加以后的日期。参数date为待计算的日期;参数number为增量;参数datepart为计量单位,可选值见备注。DATEADD(DAY, 3,date)为计算日期date的3天后的日期,而DATEADD(MONTH ,-8,date)为计算日期date的8个月之前的日期 

DATEDIFF ( datepart , startdate , enddate ) :计算两个日期之间的差额。 datepart 为计量单位,可取值参考DateAdd。

 

DATEPART (datepart,date):返回一个日期的特定部分 整数

DATENAME(datepart,date):返回日期中指定部分 字符串

YEAR() MONTH() DAY()

4、类型转换函数

CAST ( expression AS data_type)

case(score as varchar(10))

CONVERT ( data_type, expression[,style])

取日期部分

2012-12-21

1、CONVERT(varchar(100),getdate(),23)

2、CONVERT(varchar(10),getdate(),21)


 

嵌套查询(子查询)

 

子查询返回的值不止一个。

 

--当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。

 

 

 

在子查询中,一搬我们会使用in 代替 =使用

 

select employee_id,employee_name

 

from employee

 

where department_id in

 

(select department_id 

 

from department where department_name='销售部')

 

连接查询

 

内连接  inner join...on...

 

查询满足on后面条件的数据

 

外连接

 

左连接

 

left join...on...

 

先查出左表中的所有数据

 

再使用on后面的条件对数据过滤

 

右连接

 

right join...on...

 

先查出右表中的所有数据

 

再使用on后面的条件对数据过滤

 

 

 

(*)交叉连接 

 

cross join 没有on

 

第一个表的每一行和后面表的每一行进行连接

 

没有条件。是其它连接的基础

 

 

  



 



  

posted on 2011-08-12 19:08  千里烟波226  阅读(1399)  评论(0编辑  收藏  举报