SQL基本操作

来源:互联网 发布:秦夕颜捏脸数据 编辑:程序博客网 时间:2024/06/05 07:18
---创建表


create table student
(
id int identity(1,1) primary key,
stuName nvarchar(10),
stuSex bit,
stuAge int
)


---- 插入
 insert into student(stuName,stuSex,stuAge) values('张一',0,12);
 insert into student(stuName,stuSex,stuAge) values('张二',1,13);
 insert into student(stuName,stuSex,stuAge) values('张三',0,14);
 insert into student(stuName,stuSex,stuAge) values('张四',1,15);
 insert into student(stuName,stuSex,stuAge) values('张五',0,16);
 insert into student(stuName,stuSex,stuAge) values('张六',1,17);
 
 
 select * from student
 
 ----修该
 
 update student set stuName='修改' where stuAge=15
 
 ----删除
 
 delete from student where stuAge=16
 
 
 ----添加约束
 --alter table 表名 add constraint 约束名 约束表达式
 --主键约束
 
--- alter table 表名 add constraint
 ---PK_表名_字段名 primary key(字段名)
 
 create table PersonCon1
 (
 id int   not null,
 stuName nvarchar(10),
 stuSex nchar(1),
 inputtime datetime,
 )
 
 ---添加主键
 alter table PersonCon1
 add constraint PK_PersonCon1_id primary key(id);
 
 --添加唯一键
 
 alter table PErsonCon1
 add constraint UQ_PersonCon1_stuName unique(stuName);
 
 --检查约束
 
 alter table PersonCon1
 add constraint CK_PersonCon1_stuSex 
 check(stuSex='男' or stuSex='女');
 
 --默认约束
 
 alter table PersonCon1
 add constraint DF_PersonCon1_inputtime
 default (getdate()) for inputtime;
 
 
 --快捷方式
 /*
 alter table 表名
 add
 constraint 约束名1 约束表达式1 
 constraint 约束名1 约束表达式1 
 constraint 约束名1 约束表达式1 
 constraint 约束名1 约束表达式1 
 constraint 约束名1 约束表达式1 
 
 */
 
 
 
 --查询
  --select 字段1,字段2,字段3,字段4.... from 表名
  
  --按照sql执行流程进行介绍各个子句
  --首先查询需要数据源 :from 子句
  
  --语法:
-- from 数据源
--注意数据源可以是表,视图,内联表值函数,表连接,子查询等  
-- 查询数据时,不是所有的数据拿出来,而是需要在数据源上做一个筛选
---语法
----:where 条件1 and或者 0r 


 
 use TestDataBase
 select * from student
where
stuSex='f'
and
year(stuStudydate)=2012
and 
month (stuStudydate)=6
and
stuName like '张%';--查询所有姓张的
 
 
 --创建主外键关系
  ---alter table 表名 add constraint
 -- pk_外键表_主键表_字段
-- foreign key( 外键表中的字段) reference 主键表




--从StuId表中取31-40之间的数据


select top 10 * from Student where stuId  not in (select top 30 stuId from Student)
--使用ROW_NUMBER()取出表中第31-到40之间的数


select * from (select *, ROW_NUMBER() over(order by stuId) as num from Student ) as t where num between 31 and 40;


---求名字个数
----having 对于数据进行分组或者聚合后,在该基础上进行筛选使用having
use TestDataBase;
select 
stuName, 
COUNT(stuName) 
from 
Student 
group by 
stuName
having
count(stuName)>5






---select 子句
---将数据源中的结果按照select中列出的字段显示出来






------distinct//除去重复项
select distinct stuName from Student;


---排序
---order by 字段//按照字段排序


---select * from 表名 order by 字段
-------------结果集
--凡是没有order by的查询结果都是结果集
--insert into 表名(字段列表) from 结果集
------------表值构造函数
/*


(values
(值1,值2,值3,...值n),
(值1,值2,值3,...值n),
(值1,值2,值3,...值n),
...
) as 临时表的别名(字段1,字段2,字段3,...字段n)


*/
--表示构造函数(sql server 2008)
select 
 *
 from
(values
(1,'张三','boss'),
(2,'李四','学员'),
(3,'王五','经理')
)  as tblPerson(id,name,title);


------------------------------select into语法
---将查询到的数据插入到一个新表中


select 
*
into 
newStudent
from 
Student
where
stuSex='f'
and
YEAR(stuStudydate)=2012;


select * from newStudent;




----------------联合结果集
--表结构没有发生变化,仅仅只是将数据简单的合并(合并之后是无虚的)
--union语法    union会除去重复的数据
/*
select 
查询1
union
查询2


*/


select 1,2,3
union
select 5,6,7;




-----------------常用函数
--字符串函数
--时间函数
--转换函数
select ASCII('abc')
--输出第一个字符的ascii值
select ASCII('我')


select UNICODE('abc')


select UNICODE('我')


select  CHAR(97)


--求字符个数
select LEN('123456')


-------时间函数//yy年,QQ季度,hh小时
select DATEPART (YY,GETDATE());


select GETDATE(),DATEADD (HH,10,GETDATE())


--datediff
select DATEDIFF (SECOND,'2012-12-26 15:22:1','2012-12-26 15:22:1')


---求出2012年6月份入学的所有女生年龄在19到26之间
select 
*
from
Student
where
stuSex='f'
and
DATEDIFF (MONTH,stuStudydate,'2012-06-01')=0
and
DATEDIFF (YEAR,stuBirthdate,GETDATE())
between
19
and
26


--为了提高查询的效率,索引字段不应该添加方法处理




-----------------转换函数
--cast(数据 as 类型);
--CONVERT (类型,数据[,样式])


select '123a',CONVERT(char(3),456)
union 
select CAST(567 as CHAR(3))'abc'






select CONVERT (varchar(10),stuBirthdate,111),* from Student




-------case语句


--case相当于判断显示一个表达式
--if -else if模型


/*
select ...
case
when 字段表达式1 then 显示1
when 字段表达式2 then 显示2
...
else 默认显示
end  as 别名,
....
from ....
*/
--例子:
--如果是m显示男,如果是f显示女,否者显示未知


select 
case 
when stuSex='m' then '男'
when stuSex='f' then '女'
else '未知'
end as 性别
,*
from
Student