1.學校想做一個選課系統,其中涉及到課程表,學生表,請分別創建這兩個表,自己思考表中應有的列及數據類型。
create table course
( course_name varchar(10) primary key,
course_amount int,
course_class char(5)
)
create table student(
stuid varchar(10) stuname varchar(10),
stuphone char(11),
sex nume('男',’'女')
)
2.學校有一個選課系統,其中包括如下關系模式: 系(系編號: 主鍵, 系名稱: 唯一鍵, 系主任: 非空約束, 系所在校去:取值范圍只能在南湖校區和渾南校區) 班級(班級編號: 主鍵, 班級名稱: 唯一鍵, 所屬系: 外鍵)
create table system( sys_id int primary key,
sys_name varchar(10) unique,
sys_head varchar(10) not null,
sys_school char(2) check(sys_school in('南湖校區','渾南校區'))
)
create table class(
classid varchar(10) primary key,
classname varchar(10) unique,
classsys varchar(10)
)
alter table calss add constraint fk_classsys foreign key(classsys) references system(sys_name)
3.創建學生表,包含如下屬性: 學號 定長字符型 10位 主鍵 姓名 變長字符型 20位 非空 性別 定長字符型 2位 取值范圍只能為男或女 出生日期 日期型 所在班級
create table student(
stu_id char(10) primary key,
stu_name varchar(20) not null,
stu_sex cahr(2) check(sex in('男','女');
stu_date date,
stu_class varcahr(10)
constraint fk_classname foreign key(class_name) references class(class_name)
)
4.通過子查詢的方式創建一個表dept10,該表保存10號部門的員工數據。 create table dept10 like select * from dept where deptno=10 5 a.在員工表中添加一個性別列,列名為gender,類型為char(2),默認值為“男” b.修改員工表中性別列的數據類型為char(4) c.修改員工表中性別列的默認值為“女” d.刪除員工表中的性別列
a.alter table dept add gender char(2) default '男';
b.alter table dept modify gender char(4);
c.alter table dept alter column gender drop default; alter table dept add gender char(2) default '女';
d.alter table dept drop gender;
1.創建表date_test,包含列d,類型為date型。試向date_test表中插入兩條記錄,一條當前系統日期記錄,一條記錄為“1998-08-18”。
.create table date_test(
d date
)
insert into date_test values(sysdata)
insert into data_test values(to_date('1998-8-18')
2.創建與dept表相同表結構的表dtest,將dept表中部門編號在40之前的信息插入該表。 .
create table dtest as select * from dept where deptno<40;
3.創建與emp表結構相同的表empl,並將其部門編號為前30號的員工信息復制到empl表。
.create table empl as select *from emp where deptno<30;
4.試為學生表student增加一列學生性別gender 默認值 “女”。
.alter table student add gender char(2) default'女';
5.試修改學生姓名列數據類型為定長字符型10位。
.alter table student modify stu_name cahr(10);
1.簡述5種約束的含義。
2.創建學生關系sc,包括屬性名: 選課流水號 數值型 主鍵; 學生編號 非空 外鍵 課程編號 非空 外鍵; 成績 0-100之間;
3.創建copy_emp,要求格式同emp表完全一樣,不包含數據。
4.創建copy_dept,要求格式同dept表完全一樣,不包含數據。
5.設置copy_emp 表中外鍵deptno,參照copy_dept中deptno,語句能否成功,為什么?
6.追加copy_dept表中主鍵deptno
1.主鍵約束(Primay Key Coustraint) 唯一性,非空性
.唯一約束 (Unique Counstraint)唯一性,可以空,但只能有一個
.檢查約束 (Check Counstraint) 對該列數據的范圍、格式的限制(如:年齡、性別等)
.非空約束 (not null)指定某列的所有數據不能包含空值
.外鍵約束 (Foreign Key Counstraint) 需要建立兩表間的關系並引用主表的列
2.create table cs(
cs_id varshar(10) primary key,
student_id varchar(10) not null, course_id varchar(10) not null,
grad char(100) check(sorce between 0 and 100)
)
alter table cs
add constraint fk_student_id foreign key(student_id)references class(class_code);
add constraint fk_course_id foreign key(course_id)references xibu(xibu_part);
3.create table copy_emp like emp;
4.create table copy_dept like dept;
5.alter table copy_emp add consrtaint pk_deptno foreign key(deptno) references copy_dept(deptno); 不能
6.alter table copy_dept add constraint pk_depno primary key(deptno);