我是靠谱客的博主 魁梧花瓣,这篇文章主要介绍Mysql数据库练习-1创建数据库,创建表,增加列,修改列名,删除列,添加外键,修改表名,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#1.创建一个数据库db_stu; create database 库名称; create database db_stu; show databases; #2.在数据库db_stu中创建一张学生表, 表名:student,且包括以下列:stuno(学号),name(姓名),sex(性别),Birthday(出生日期), 并且设置学号stuno为主键,name(姓名)列不能为空,且stuno为自增列; #create table 表名 (列名,列的数据类型,primary key(stuno)->设置主键已经主键的列名); create table student ( stuno int not null auto_increment, name varchar(10), sex varchar(10), Birthday varchar(10), primary key (stuno) ); show tables; #3.给第二题中的表新增一列 telephone;alter table 表名 add column 列名 列的数据类型; alter table student add column telephone varchar(10); desc student; #4.修改第二题中表student,将列sex更名为gender; alter table 表名 change 旧列名 更改名 更改名数据类型; alter table student change sex grnder varchar(10); desc student; #5.修改第二题中的表student,更改列name(姓名)数据类型为varchar; # alter table 表名 modify column 更改列列名 更改列列名数据联系; alter table student modify column name varchar(20); desc student; #6.删除第二题中表student中的Birthday列;alter table 表名 drop 列名; # 通过使用 DROP 语句,可以轻松地删除索引、表和数据库。 alter table student drop Birthday; desc student; #7.创建一张学生成绩表,表名:sc,包括以下列:stuno(学号),cid课程号,grade(成绩) create table sc ( stuno int not null auto_increment, cid varchar(10), grade varchar(10), primary key (stuno) ); show tables; #8.给第7题中的sc表添加一个外键,外键列stuno关联student表中的stuno列。 # alter table 表名 add foreign key (外键列名) references 关联表名(关联表列名); alter table sc add foreign key (stuno) references student (stuno); show create table sc; #查当前表外键 show create table 表名; #9.重命名第一题中student表为stu; # 删除原来已经存在的stu表; drop table 表名; drop table stu; alter table student rename to stu; show tables; desc stu;

最后

以上就是魁梧花瓣最近收集整理的关于Mysql数据库练习-1创建数据库,创建表,增加列,修改列名,删除列,添加外键,修改表名的全部内容,更多相关Mysql数据库练习-1创建数据库内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部