create unique index stusno on student(sno); --創建索引
create clustered index stuname on student(sname);-- 創建聚簇索引
drop index student.stuname; -- 刪除索引
alter table student add constraint PK1_ID primary key(sno);--修改表的sno為主鍵
--創建表
create table student (
sno char(5) not null unique,
sname char(20) unique,
ssex? char(1),
sage int,
sdept char(15));
--刪除表
drop table student;
?
ALTER TABLE student DROP CONSTRAINT unique? --沒有解決
alter table student drop CONSTRAINT sname ;
--建立WoolAdminName表
create table WoolAdminName
(
ID int identity(1,1) not null,
AdminName varchar(30) not null,
pwd varchar(30) not null
)
--建立管理員WoolAdminName表約束
go
alter table WoolAdminName
add constraint PK_ID primary key(ID)
insert into WoolAdminName values('admin','admin')
create table course(
cno char(5) not null? primary key,
sname char(20) ,
cpno char(5) not null,
ccredit int );
create table sc(
cno char(5),
sno char(5),
grade int,
primary key(sno,cno)
)
select * from student;
insert into student values('1','ren','m',20,'信息工程');
insert into student values('2','xi','w',18,'公共管理');
insert into student values('3','hai','m',22,'管理科學');
insert into student values('4','hui','w','19','信息工程');
select sname 姓名, 40-sage 年齡 from student;
select sname 姓名, sage 年齡 from student where sage >=20;
select sname 姓名, sage 年齡 from student group by sage having? max(sage) ;
use test1;
alter table student drop unique(sname);
select sname,'Year of Birth:',2007-sage birthday,lower(sdept) from student;
select * from student;
select ssex from student;
select distinct ssex from student;
?
select * from student where sage =22;
select * from student where sage <>22;
select * from student where sage !=22;
?
select * from student where sage>21;
select * from student where sage !>21;
--下邊的形式不對
select * from student where sage not > 21;
--上邊的形式不對 應改成下邊的形式
select * from student where not sage? > 21;
?
select * from student where sage between 19 and 21;-- between and
select * from student where sage not between 19 and 21;--not between and
select * from student where sage in (19,20,21);-- in
select * from student where sage not in (19,20,21);-- not in
select * from student where sname like 'h\_i'escape '\';?? -- like
select * from student where sname not like 'h%'; -- not like
select * from student where sname is null;? -- is null
select * from student where sname is not null; -- is not null
select * from student where sname like 'h%' and sage>20;--and
select * from student where sname like '%i' or sage<20;-- or
select * from student where sage in (19,20,21)order by sage desc;-- order by
select * from student order by sdept desc,sage desc;--系按降序排列,同系中的按年齡降序排列
select count(*) from student; -- 統計元組個數
select count(*) from student where sage>20; -- 有條件統計元組個數
select? count(distinct ssex) from student;-- 統計一列中 不重復的值的個數
select? count(ssex) from student;--統計一列中值的個數
select sum(sage) from student;-- 計算一列值的總和(此列必為數值型)
select avg(sage) from student; -- 計算一列值的平均值
select max(sage) from student; -- 求一列值的最大值
select min(sage) from student; -- 求一列值的最小值
select sno from sc group by sno having count(*)>=2;
SELECT *
FROM dbo.course INNER JOIN
????? dbo.sc ON dbo.course.cno = dbo.sc.cno INNER JOIN
????? dbo.student ON dbo.sc.sno = dbo.student.sno
select student.*,sc.* from student,sc where student.sno=sc.sno;--等值連接
select student.*,sc.* from student,sc;--廣義笛卡爾積
--等值連接去掉重復的列就是自然連接
select student.sno,sname,ssex,sage,sdept,cno,grade from student,sc where student.sno=sc.sno;
--自連接
select first.cno,second.cpno from course first,course second where first.cpno=second.cno;
SELECT p1.pub_id,p2.pub_id,p1.pr_info
FROM pub_info AS p1 INNER? pub_info AS p2
ON DATALENGTH(p1.pr_info)=DATALENGTH(p2.pr_info)
--外連接
--join_type 指出連接類型,可分為三種:內連接、外連接和交叉連接。內連接(INNER JOIN)使用比
--較運算符進行表間某(些)列數據的比較操作,并列出這些表中與連接條件相匹配的數據行。根據所使用
--的比較方式不同,內連接又分為等值連接、自然連接和不等連接三種。
--外--連接分為左外連接(LEFT OUTER JOIN或LEFT JOIN)、右外連接(RIGHT OUTER JOIN或RIGHT JOIN)
--和全外連接(FULL OUTER JOIN或FULL JOIN)三種。與內連接不同的是,外連接不只列出與連接條件相匹
--配的行,而是列出左表(左外連接時)、右表(右外連接時)或兩個表(全外連接時)中所有符合搜索條件的
--數據行。
--交叉連接(CROSS JOIN)沒有WHERE 子句,它返回連接表中所有數據行的笛卡爾積,其結果集合中的
--數據行數等于第一個表中符合查詢條件的數據行數乘以第二個表中符合查詢條件的數據行數。
--連接操作中的ON (join_condition) 子句指出連接條件,它由被連接表中的列和比較運算符、邏輯
--運算符等構成。
--無論哪種連接都不能對text、ntext和image數據類型列進行直接連接,但可以對這三種列進行間接
--連接。-
?
select student.sno,sname,ssex,sage,sdept,cno,grade from student,sc where student.sno=sc.sno *;--錯誤的寫法
-- 右外連接
select student.sno,sname,ssex,sage,sdept,cno,grade from student right join sc on student.sno=sc.sno ;
--左外連接
SELECT a.*,b.* FROM luntan LEFT JOIN usertable as b
ON a.username=b.username
--嵌套查詢
use test1;
select sno, sname from student? where sno in (
???
?select sno from sc where cno in
??(select cno from course where sname='數據結構')
);
select student.sno,student.sname from student, sc, course
?where student.sno=sc.sno and sc.cno=course.cno and course.sname='數據結構';
select sname,sage,sdept from student where sage<any(select sage from student where sdept='信息工程');
select sname,sage,sdept from student where sage<all(select sage from student where sdept='信息工程');
--exists關鍵字
select sname from student where exists (select * from sc where sno=student.sno and cno='1');
select sname from student where not exists (select * from sc where sno=student.sno and cno='1');
select * from course;
insert into course values('1','語文','2','');
insert into course values('2','數學','2','');
insert into course values('3','英語','2','');
insert into course values('4','c語言','2','');
insert into course values('5','數據結構','2','');
select * from sc;
insert into sc values('1','1','');
insert into sc values('1','2','');
insert into sc values('2','2','');
insert into sc values('2','1','');
insert into sc values('3','3','');
insert into sc values('5','3','');
?