一、
--1.建立学生表Student,其中以学号为主码,以系号为外码,姓名不可以为空值,性别取值为“男”或“女”
CREATE TABLE Student
(
Sno INT,
Name VARCHAR(20) NOT NULL,
Gender CHAR(2),
Class VARCHAR(20),
Major VARCHAR(20),
Dno CHAR(2)
CONSTRAINT pk_Student_Sno PRIMARY KEY(Sno),
CONSTRAINT fk_Student_Dno FOREIGN KEY(Dno) REFERENCES Dept(Dno),
CONSTRAINT chk_Student_Gender CHECK(Gender IN ('男', '女'))
)
--2. 检索与“王强”在同一个班级的学生的学号、姓名、性别
SELECT Sno, Name, Gender
FROM Student
WHERE Class = (SELECT Class FROM Student WHERE Name = '王强')
--3. 检索管理学院没有选修“电子商务”课程的学生的学号、姓名、班级、专业
SELECT S1.Sno, Name, Class, Major
FROM Student S1
JOIN Dept D
ON S1.Dno = D.Dno
WHERE Dname = '管理学院'
AND Sno NOT IN
( SELECT S2.Sno FROM Study S2
JOIN Course C
ON S2.Cno = C.Cno
WHERE Subject = '电子商务')
--4. 检索选修了“C语言”且C语言考试成绩高于这门课平均成绩的学生的学号、姓名
SELECT S1.Sno, Name
FROM Student S1
JOIN Study S2
ON S1.Sno = S2.Sno
JOIN Course C1
ON S2.Cno = C1.Cno
WHERE Subject = 'C语言'
AND Score > ( SELECT AVG(Score)
FROM Study S3
WHERE S3.Cno = S2.Cno )
--5. 在院系表(Dept)中插入一条记录,系号为06,系名为“物理系”,电话为8470660
INSERT INTO Demp VALUES('06', '物理系', '84706600')
--6. 将“数据库原理”课程的学时改为64,学分改为4
UPDATE Course
SET Time = 64,
Credit = 4
WHERE Subject = '数据库原理'
--7. 建立由班级、学号、姓名、课程号、课程名、学分、成绩七个数据项组成的视图
CREATE VIEW VW_SC
AS
SELECT Class, Sno, Name, Cno, Subject, Credit, Score
FROM Student S1
JOIN Study S2
ON S1.Sno = S2.Sno
JOIN Course C
ON S2.Cno = C.Cno
二、
2.
1.create table Student(sno varchar(12) primary key,
name varchar(12) not null,
Gender char(2) check(ssex='男' or ssex='女'),
Class varchar(12),
foreign key(Dno)references dept(dno),
)
2.select b.sno,b.name,b.gender from student a,student b where name='张强' and a.class=a.class
3.select sno,name from student where dno=(select dno from dept where dname='管理学院') and sno not in(select sno from study,course where study.cno=course.cno and subject='电子商务')
4.select sno,name from student,(select sno,avg(scort) from Study where cno=(select cno from course where subject='C语言') group by sno) t where t.sno = student.sno;
5.insert into dept(dno,dname,tele) values('06','物理系','84706600')
6.alter table course set credit=4 ,time=64 where course.subject='数据库原理'
就写这么多吧,可能问题不少只是给你个参考