Mysql实验二 数据库和表的建立

实验二 数据库和表的建立

1、在mysql中建立一个数据库testdb,所有的SQL操作均在此数据库上进行。

create database testdb;
use testdb;

2、用命令的方式在testdb数据库中建立5张基本表,其类型为innodb表,字符集为utf8。(注意创建表的顺序)

①创建学生表Student,由以下属性组成:学号 sno(int型,主码,其值自动填充,初值为2011001,每次增值为1),学生姓名 sname(char型,长度为8,非空),性别 sex(enum(男,女)),所在系别 deptno(tinyint型),邮箱email(varchar(30))。

 create table student(sno int primary key
     auto_increment comment '学号',
    sname char(8) not null,
   sex enum('男','女'),
 deptno tinyint not null,
 email varchar(30))auto_increment=2011001;

②创建系表dept,由以下属性组成:系号 deptno(tinyint型),系名 dname(char型,长度为20,非空),电话号码(phone char(8),唯一性)。

 create table dept(deptno tinyint,
    dname char(8) not null,
   phone char(8) unique);

创建教师表teacher,由以下属性组成:教师编号 tno(char型,长度为10,主码),教师姓名 tname(varchar型,长度为5,非空),性别tsex((enum(男,女)),系 deptno(tinyint型),邮箱email(varchar(30))。

 create table teacher(tno char(10) primary key,
    tname varchar(5) not null,sex enum('男','女'),
   deptno tinyint(6),email varchar(30));

④创建课程表course,由以下属性组成:课程号 cno(int型,主码),课程名 cname(varchar型,长度为20,非空),授课教师编号 tno(char型,长度为10,外码),授课老师姓名 tname(varchar型,长度为8,非空),学分 credit(tinyint型)。

 create table course( cno int primary key,cname varchar(20)
 not null,tno char(10) comment '老师号',
    tname varchar(8) not null comment '老师名字',
  credit tinyint comment '学分',
 foreign key(tno) references teacher(tno));

⑤创建学生选课表student_course,由以下属性组成:学号 sno(int型,外码),课程号 cno(int型,外码),fenshu(decimal(5,2)型),(cno,sno)为主码。

create table student_course (sno int,
  cno int,
     fenshu decimal(5,2),primary key(cno,sno),foreign key(sno) references student(sno),
   foreign key(cno) references course(cno));

4、修改表结构

①显示所有的表
show tables;

②查看student表结构,并将student表的姓名字段改为varchar类型,长度为

4desc student
alter table student modify sname varchar(4);

在这里插入图片描述

③查看dept表结构,并将dept表的deptno设置为主码

desc dept;
alter table dept add primary key(deptno);

④将student表的deptno设置为外键,外键名为aaa

⑤为dept表的dname设置为唯一索引

alter table dept add unique(dname);

⑥为teacher表的deptno设置为外码,外键名为bbb

⑦在student表中加入属性出生日期 birth(datetime型),放在姓名字段的后面
alter table student add birth datetime after sname;
⑧删除course表中授课教师姓名列。

alter table course drop column tname;

⑨为course表中的credit增加一个默认值为4。

 alter table course modify credit tinyint default 4;

⑩将student_course表名改名为sc。

rename table student_course to sc;

将sc表中的fenshu字段改为grade。

alter table sc change fenshu grade decimal(5,2) ;

5sql语句插入系统的当前时间

insert into users(user_regtime)values(sysdate());
insert into users(user_regtime)values(now());


4、输入5张表的数据。

student表的记录如下:
sno sname birth sex deptno email
2011001 张天 2009/3/5 男 10 12344@qq.com
2011002 李兰 2009/10/12 女 10 345611123@qq.com
2011003 陈铭朝 2010/3/9 男 10 lanlan@163.com
2011004 刘茜 2008/6/23 女 20 liuxi@163.com
2011005 马朝阳 2010/7/1 男 20 45234321@qq.com

insert into student values(2011004,'刘茜','2008/6/23','女',20,'liuxi@qq163.com'),
     (2011005,'马朝阳','2010/7/1','男',20,'43234321qq.com');

4、因为学校机房有还原系统,所以下课之前应将所有的数据进行备份,下次上机课还原即可。

5、思考与练习:

6. 一次性插入或者更新多条语句

insert into tablename(),(),()
update tablename set filed ="条件" in(, , , , )
  • 8
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有时间指导毕业设计

觉得写的好的话可以给我打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值