CREATE TABLE table_name(
column_name column_type [not null],
column_name column_type [not null],
...
[constraint]
)
语法说明:
实例:
学生表 tb_S(Snum, Sname, sex, birthday, dept)
课程表 tb_C(Cnum, Cname, Credit, Tnum)
选修表 tb_SC(Snum, Cnum, grade)
教师表 tb_T(Tnum, Tname, sex, birthday, dept, title)
字段名 | 数据类型 | 是否为空 | 中文说明 |
---|---|---|---|
Snum | char(5) | not null | 学号 |
Sname | varchar(10) | not null | 学生姓名 |
sex | char(2) | not null | 学生性别 |
birthday | number(2) | not null | 学生年龄 |
dept | varchar(20) | not null | 系别 |
CREATE TABLE tb_S(
Snum char(5) not null,
Sname varchar(10) not null,
sex char(2) not null,
birthday number(2) not null,
dept varchar(20) not null
);
字段名 | 数据类型 | 是否为空 | 中文说明 |
---|---|---|---|
Cnum | char(2) | not null | 课程号 |
Cname | varchar(20) | not null | 课程名 |
Credit | number(1) | not null | 学分 |
Tnum | char(5) | not null | 教师号 |
CREATE TABLE tb_C(
Cnum char(2) not null,
Cname varchar(20) not null,
Credit number(1) not null,
Tnum char(5) not null
);
字段名 | 数据类型 | 是否为空 | 中文说明 |
---|---|---|---|
Snum | char(5) | not null | 学号 |
Cnum | char(2) | not null | 课程号 |
grade | varchar(3) | null | 成绩 |
CREATE TABLE tb_SC(
Snum char(5) not null,
Cnum char(2) not null,
grade varchar(3) null
);
ALTER TABLE tb_SC RENAME COLUMN Sname TO Snum;
字段名 | 数据类型 | 是否为空 | 中文说明 |
---|---|---|---|
Tnum | char(5) | not null | 教师号 |
Tname | varchar(10) | not null | 教师姓名 |
sex | char(2) | not null | 教师性别 |
birthday | number(2) | not null | 教师年龄 |
dept | varchar(20) | not null | 系别 |
title | varchar(20) | not null | 职位 |
CREATE TABLE tb_T(
Tnum char(5) not null,
Tname varchar(10) not null,
sex char(2),
birthday number(2),
dept varchar(20) not null,
title varchar(20) not null
);
创建以下学生信息:
--学生表
97301, 刘三, M, 21, 99101
97302, 王利, W, 20, 99101
97303, 刘磊, M, 22, 99102
97304, 张二, W, 22, 99101
97305, 陈四, M, 21, 99102
使用SQL
语句将信息依次插入到tb_S
表中:
INSERT INTO tb_S
VALUES(
'97301',
'刘三',
'M',
'21',
'99101'
);
创建以下课程信息:
--课程表
C1, 高等数学, 5, T1
C2, 计算机网络, 3, T2
C3, 线性代数, 4, T1
C4, 数据库原理, 3, T3
C5, 程序设计, 3, T4
使用SQL
语句将信息依次插入到tb_C
表中:
INSERT INTO tb_C
VALUES(
'C1',
'高等数学',
'5',
'T1'
);
创建以下选课信息:
97301, C1, 78
97301, C2, 82
97301, C3, 70
97301, C4, 58
97301, C5, 94
97302, C1, 66
97302, C2, 78
97302, C3, 60
97302, C4, NULL
97303, C1, 90
97303, C5, 88
97304, C1, 70
97304, C2, 74
97304, C4, 98
使用SQL
语句将信息依次插入到tb_SC
表中:
INSERT INTO tb_SC
VALUES(
'97301',
'C1',
'78'
);
创建以下教师表:
--教师表
T1, 李教, M, 50, 99101, 教授
T2, 陈四, W, 60, 99101, 教授
T3, 李华, M, 34, 99102, 讲师
T4, 刘丽, W, 44, 99102, 讲师
使用SQL
语句将信息依次插入到tb_T
表中:
INSERT INTO tb_T
VALUES(
'T1',
'李教',
'M',
'50',
'99101',
'教授'
);
注:添加完数据库信息后,要执行commit;
命令以提交修改。
SELECT Sname, birthday
FROM tb_S
ORDER BY birthday DESC;
SELECT * FROM tb_S
WHERE NOT Sname LIKE '王%';
SELECT DISTINCT Sname
FROM tb_S, tb_SC
WHERE tb_S.Snum=tb_SC.Snum;
SELECT tb_S.Snum, Sname, Cname
FROM tb_S, tb_SC, tb_C
WHERE tb_S.Snum=tb_SC.Snum
AND tb_SC.Cnum=tb_C.Cnum
AND tb_SC.grade>0;
SELECT Tname, Cname
FROM tb_T, tb_C
WHERE tb_T.Tnum=tb_C.Tnum
AND tb_T.sex='W';
SELECT COUNT(Tnum)
FROM tb_T
WHERE title='教授';
SELECT Sname
FROM tb_S
WHERE dept
IN (
SELECT dept
FROM tb_S
WHERE Sname='刘三'
);
SELECT DISTINCT Cnum, COUNT(Snum)
FROM tb_SC
GROUP BY Cnum
ORDER BY Cnum;
SELECT Sname, grade
FROM tb_S, tb_SC, tb_C
WHERE tb_S.Snum=tb_SC.Snum
AND tb_SC.Cnum=tb_C.Cnum
AND Cname='数据库原理';
SELECT Cname, AVG(grade)
FROM tb_T, tb_SC, tb_C
WHERE tb_T.Tnum = tb_C.Tnum
AND tb_C.Cnum=tb_SC.Cnum
AND Tname='李教'
GROUP BY Cname;
SELECT Sname, Cname, grade
FROM tb_S, tb_C, tb_SC
WHERE tb_S.Snum=tb_SC.Snum
AND tb_SC.Cnum=tb_C.Cnum;
SELECT Sname, AVG(grade)
FROM tb_S, tb_SC
WHERE tb_S.Snum=tb_SC.Snum
AND grade>=60
GROUP BY Sname
HAVING COUNT(Cnum)>=3
ORDER BY AVG(grade) DESC;
SELECT DISTINCT Sname
FROM tb_S, tb_SC
WHERE tb_S.Snum NOT IN (
SELECT tb_SC.Snum
FROM tb_SC
WHERE tb_S.Snum=tb_SC.Snum
AND grade<60)
AND dept='99101';
SELECT Sname, COUNT(Cnum), AVG(grade)
FROM tb_S, tb_SC
WHERE tb_S.Snum=tb_SC.Snum
GROUP BY Sname;
SELECT tb_SC.Cnum, Cname, COUNT(tb_SC.Snum), MAX(grade), MIN(grade), AVG(grade)
FROM tb_SC,tb_C
WHERE tb_C.Cnum=tb_SC.Cnum
GROUP BY tb_SC.Cnum, Cname
ORDER BY tb_SC.Cnum;
SELECT Sname, Cname, Credit
FROM tb_S, tb_C, tb_SC
WHERE tb_S.Snum=tb_SC.Snum
AND tb_SC.Cnum=tb_C.Cnum
AND grade IS NULL;
SELECT Snum, Sname
FROM tb_S
WHERE NOT EXISTS(
SELECT *
FROM tb_C
WHERE NOT EXISTS(
SELECT *
FROM tb_SC
WHERE tb_SC.Snum=tb_S.Snum
AND tb_SC.Cnum = tb_C.Cnum
)
);
INSERT INTO tb_SC
VALUES(
(SELECT Snum FROM tb_S WHERE Sname='刘磊'),
(SELECT Cnum FROM tb_C WHERE Cname='数据库原理'),
NULL
);
CREATE TABLE tb_Age(
dept VARCHAR(20) NOT NULL,
age NUMBER(2) NULL
);
SELECT dept, AVG(birthday)
INTO tb_Age
FROM tb_S
GROUP BY dept;
);
GROUP BY dept
UPDATE tb_SC
SET grade=grade+10
WHERE Snum IN (
SELECT Snum
FROM tb_S
WHERE Sname='王利'
)
AND Cnum='C3';
UPDATE tb_C
SET Credit=(
SELECT Credit
FROM tb_C
WHERE Cname='高等数学'
)
WHERE Cname='数据库原理';
DELETE FROM tb_S
WHERE Snum='97301';
DELETE FROM tb_SC
WHERE Snum IN (
SELECT Snum
FROM tb_S
WHERE Sname='王利'
)
AND Cnum IN (
SELECT Cnum
FROM tb_C
WHERE Cname='数据库原理'
);