sqlserver操作练习笔记知识点

sqlserver操作练习笔记知识点

create database bless_remind
use bless_remind
1.check
表约束:
drop table student
create table student(id int identity(1,1) not null primary key,name varchar(20),check((id<5)and(name<>'a')))
insert into student(name) values('a')
insert into student(name) values('d')
select * from student
列约束:
create table student(id int identity(1,1) not null primary key check(id<5),name varchar(20) check (name<>'a'))
insert into student(name) values('a')
insert into student(name) values('d')
select * from student
当输入条件不满足check约束的数据时,会报"消息 547,级别 16,状态 0,第 1 行
INSERT 语句与 CHECK 约束"CK__student__0AD2A005"冲突。该冲突发生于数据库"bless_remind",表"dbo.student"。
语句已终止。"错误。
2.指定外键约束
create table Dept(DepID int primary key,DepName varchar(20)unique,ManagerID int)
insert into Dept values(1,'a',1)
insert into Dept values(1,'a',1)
错误"消息 2627,级别 14,状态 1,第 1 行
违反了 PRIMARY KEY 约束 'PK__Dept__108B795B'。不能在对象 'dbo.Dept' 中插入重复键。
语句已终止。"
insert into Dept values(2,'b',2)
insert into Dept values(3,'c',3)
select * from Dept
按照列级约束创建外键表:
create table users(usersID int,name varchar(10),DepID int references Dept(DepID))
insert into users values(1,'a',1)
update users set ManagerID=4 where ManagerID=1
报错"消息 207,级别 16,状态 1,第 1 行
列名 'ManagerID' 无效",因为Dep的DepID并没有DepID=4这项。references指的是我这列的属性值都是来自指定列的值,在这些值之外,就会报错。
按照表级约束创建外键表
create table users(usersID int,name varchar(10),DepID int,foreign key(DepID) references Dept(DepID))
insert into users values(1,'a',4)
报错“消息 547,级别 16,状态 0,第 1 行
INSERT 语句与 FOREIGN KEY 约束"FK__users__DepID__1DE57479"冲突。该冲突发生于数据库"bless_remind",表"dbo.Dept", column 'DepID'。
语句已终止。”
insert into users values(1,'c',1)
select * from users
3.指定标识列
create table student(id int not null identity(1,1),name varchar(20))
insert into student values(1,'a')
错误!“消息 8101,级别 16,状态 1,第 1 行
仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'student'中的标识列指定显式值。”
insert into student values('a')
成功!
一个表只能有一个标识列,且标识列的值只能是整数类
create table student(id int not null identity(1,1),age int identity(1,2))
错误!“消息 2744,级别 16,状态 2,第 1 行
为表 'student' 指定了多个标识列。只允许为每个表指定一个标识列。

4.指定默认属性
create table student(id int not null primary key,name varchar(10) default('not input!'),sex int default(1))
insert into student(id) values(1)
select * from student
5.计算列
create table users(price float,num int,amount as price*num)
insert into users(price,num) values (2,3)
select * from users
6.group by语句
create table dept(deptID int,name varchar(10))
insert into dept(deptID) values(1)
insert into dept(deptID) values(1)
insert into dept(deptID) values(1)
insert into dept(deptID) values(2)
insert into dept(deptID) values(2)
select deptID,count(*)as total from dept group by deptID
结果如下:
depID total
1 3
2 2
having子句:
select deptID,count(*)as total from dept group by deptID having deptID<>2
结果如下:
depID total
1 3
7.内联结(inner可以省略),它主要用来取两个表的公共部分
select * from students a inner join stu_course b on a.id=b.id
8.外联结,主要用来取两个表的公共部分以及原表的总和
select * from students a left join stu_course b on a.id=b.id
select * from students a right join stu_course b on a.id=b.id
9.go语句的一些小心得

create table table1(id int,name varchar(10))
go
insert into table1 vlaues(1,'a')
insert into table1 vlaues(1,'a')
insert into table1 vlaues(1,'a')
insert into table1 vlaues(1,'a')
insert into table1 vlaues(1,'a')
insert into table1 vlaues(1,'a')
go
在SQLSERVER2000里,如果go语句不写的话,就会报错,但在SQOSERVER2005里不写go语句的话也可以执行,只不过如果前边语句出错的话,后边的语句就不会得到执行


create table student(
studentid int primary key,
studentname varchar(20)
);

create table subject(
subjectid char(3) primary key,
subjectname varchar(20)
);


create table grade(
studentid int references student(studentid),
subjectid char(3) references subject(subjectid),
mark int,
primary key (studentid,subjectid)
)

insert into student values (101,'张三');
insert into student values (102,'李云');
insert into student values (103,'未');

insert into subject values ('A01','C++');
insert into subject values ('A02','ASP');
insert into subject values ('A03','JAVA');


insert into grade values (101,'A01',59);
insert into grade values (101,'A02',72);
insert into grade values (101,'A03',90);

insert into grade values (102,'A01',75);
insert into grade values (102,'A02',91);

insert into grade values (103,'A01',71);

select * from student
select * from subject
select * from grade

一.查询出以下信息

学号 学生姓名 课程名称 成绩 (要全部学生信息)

select a.studentid as"学生编号" ,a.studentname as"学生姓名" ,b.subjectid as"课程编号" ,b.subjectname as"课程名字",c.mark as"分数" from student a,subject b,grade c where a.studentid=c.studentid and b.subjectid=c.subjectid

第二问:查询出以下信息

学号 学生姓名 课程名称 成绩(只显示每科最高分)

select a.studentid "学 号",studentname "学生姓名",
subjectname "课程名称",mark "成 绩"
from student a,subject b,grade c
where a.studentid = c.studentid
and b.subjectid = c.subjectid
and (select count(*) from grade
where subjectid = b.subjectid and
mark > c.mark) = 0
或者
select a.studentid "学 号",studentname "学生姓名",
subjectname "课程名称",mark "成 绩"
from student a,subject b,grade c
where a.studentid = c.studentid
and b.subjectid = c.subjectid
and c.mark in(select max(mark)
from grade group by subjectid )

第三问:查询出以下信息

学号 学生姓名 (查询出课程超过1门以上学生的信息)

select a.studentid "学 号",studentname "学生姓名",
count(subjectname)as"课程数目"
from student a , subject b , grade c
where a.studentid = c.studentid
and b.subjectid = c.subjectid
group by a.studentid,studentname
having count(subjectname) >= 2


问题:有六支球队,每个球队都要和其他队打一场比赛,请列表
create table test(teamID varchar(20))
insert into test values('大连')
insert into test values('上海')
insert into test values('北京')
insert into test values('河北')
insert into test values('四川')
insert into test values('海南')
select a.teamID as"球队", b.teamID as"球队" from test a,test b where a.teamID<>b.teamID