MySQL一些简单的操作题

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);

猜你喜欢

转载自www.cnblogs.com/tw9751/p/11541267.html