怎么创建MySQL数据库表——MySQL 5.7数据库


一、 InnoDB存储引擎的表空间

1. 查看数据库的表空间

利用如下命令可以查看数据库的表空间。

	mysql> show variables like 'InnoDB_data%'; 

       (1)表空间有四个文件组成:ibdata1、ibdata2、ibdata3、ibdata4,每个文件的大小为10M,当每个文件都满了的时候,ibdata4会自动扩展;

       (2)如果用 autoextend 选项描述最后一个数据文件,当 InnoDB 用尽所有表自由空间后将会自动扩充最后一个数据文件,每次增量为 8 MB。

       (3)不管是共享表空间和独立表空间,都会存在InnoDB_data_file文件,因为这些文件不仅仅要存放数据,而且还要存储事务回滚(undo)信息。

2. 共享表空间和独立表空间之间的转换

(1)查看当前数据库的表空间管理类型。可以通过如下命令查看。

mysql> show variables like "InnoDB_file_per_table";

(2)修改数据库的表空间管理方式。
       修改InnoDB_file_per_table的参数值(InnoDB_file_per_table=1 为使用独占表空间,InnoDB_file_per_table=0 为使用共享表空间)即可,但是修改不能影响之前已经使用过的共享表空间和独立表空间;

(3)共享表空间转化为独立表空间的方法(参数InnoDB_file _per_table=1需要设置)。

(4)单个表的转换操作可以用如下命令实现:

alter table  table_name  engine = innodb;

二、 创建数据库表

1. 创建表的语法结构

       表决定了数据库的结构, 表是存放数据的地方,一个库需要什么表,各数据库表中有什么样的列,都是要合理设计的。
创建表的语法结构如下。

扫描二维码关注公众号,回复: 10748400 查看本文章

create [temporary]table[if not exists]table_name
[([column_definition], …|[index_definition])]
[table_option][select_statement];

       其中,column_definition:字段的定义。包括指定字段名、数据类型、是否允许空值,指定默认值、主键约束、唯一性约束、注释字段名、是否为外键,以及字段类型的属性等。字段的定义具体格式描述如下:

col_name type [not null | null] [default default_value]
[auto_increment] [unique [key] | [primary] key] [comment ‘string’]
[reference_definition]

2. 利用SQL语句创建数据表(创建的数据表全部是引用的例子,方便观察)附:说明

(1)打开教务管理数据库teaching,按照如下所示的学生信息表结构创建student表。

列序号 字段名 类型 取值说明 列含义
1 studentno char(11) 主键 学生学号
2 sname char(8) 学生姓名
3 sex enum (2) 性别
4 birthdate date 出生日期
5 entrance int(3) 入学成绩
6 phone varchar(12) 电话
7 Email varchar(20) 电子信箱

添加代码:注意不能忘记括号和分号,可以写成一行,也可以写成多行,每一句用逗号隔开。分号为整个代码结束

mysql> create table if not exists student 
(
studentno  char(11) not null comment'学号', 
sname char(8) not null comment'姓名', 
sex enum('男', '女') default '男' comment'性别', 
birthdate date not null comment'出生日期', 
entrance int(3)  null comment'入学成绩',		 
phone varchar(12) not null comment'电话', 
Email varchar(20) not null comment'电子信箱',
primary key (studentno)
);

运行结果如图所示:
在这里插入图片描述
(2)利用create table命令建立建立课程信息表course.

列序号 字段名 类型 取值说明 列含义
1 courseno char(6) 主键 课程编号
2 cname char(20) 课程名称
3 type char(8) 类别
4 period int(2) 总学时
5 exp int(2) 实验学时
6 term int(2) 开课学期

添加代码:

mysql> create table if not exists course 
(
courseno  char(6) not null, 
cname  char(6) not null, 
type char(8) not null,  
period int(2) not null, 
exp int(2) not null,
term int(2) not null,
primary key (courseno) ); 

运行结果如图所示:
在这里插入图片描述
(3)利用create table命令建立学生分数表score。该表中主键由两个列构成

列序号 字段名 类型 取值说明 列含义
1 studentno char(11) 主键 学号
2 courseno char(6) 主键 课程编号
3 daily float(3,1) 平时成绩
4 final float(3,1) 期末成绩

添加代码:

mysql> create table if not exists score
(studentno  char(11) not null, 
courseno  char(6) not null, 
daily float(3,1) default 0, 
final float(3,1) default 0,
primary key (studentno , courseno) 
); 

运行结果如图所示:
在这里插入图片描述
(4)利用create table命令建立教师信息表teacher.

列序号 字段名 类型 取值说明 列含义
1 teacherno char(6) 主键 教师编号
2 tname char(8) 教师姓名
3 major char(10) 专业
4 prof char(10) 职称
5 department char(16) 院系部门

添加代码:

mysql> create table if not exists teacher 
(teacherno  char(6) not null comment '教师编号', 
tname  char(8) not null comment'教师姓名', 
major  char(10) not null comment '专业', 
prof char(10) not null comment '职称',
department char(16) not null comment '部门',
primary key (teacherno)
); 

运行结果如图所示:
在这里插入图片描述
(5)为了完善teaching数据库的表间联系,创建纽带表teach_course。

列序号 字段名 类型 取值说明 列含义
1 teacherno nchar(6) 主键 教师编号
2 courseno nchar(6) 主键 课程编号

添加代码:

mysql> create table if not exists teach_course 
(teacherno char(6) not null, 
courseno  char(6) not null, 
primary key (teacherno,courseno) );

运行结果如图所示:
在这里插入图片描述

  • 说明:
    (1)主键设置。primary key表示设置该字段为主键。
    (2)添加注释。comment’学号’表示对studentno字段增加注释为“学号”
    (3)字段类型的选择。sex enum(‘男’,‘女’)表示sex字段的字段类型是enum,取值范围为’男’和’女’。对于取值固定的字段可以设置数据类型为enum。例如,在course表的type字段表示的是课程的类型,一般是固定的几种类型。因此,也可以把该字段的定义写成:type enum (‘必修课’,’ 选修课’) default ‘必修课’。
    (4)默认值的设置。default’男’表示默认值为“男”。
    (5)设置精度。Score表中的daily float(3,1)表示精度为4 ,小数位1位。
    (6)如果没有指定是null或是not null,则列在创建时假定为null。

3.设置表的属性值自动增加

       (1)在MySQL数据表中,一个整数列可以拥有一个附加属性auto_increment。其主要用于为表中插入的新记录自动生成唯一的序列编码

       (2)默认的情况下,该字段值是从1开始自增,也可自定义开始值。一个数据表只能有一个字段使用auto_increment约束,且该字段必须为主键的一部分。可以是任何整数类型(tinyint、samllint、int、bigint等)。

设置属性值字段增加的基本语法规则如下:

属性名 数据类型 auto_increment

如:在teaching库中,创建选课表sc,选课号sc_no是自动增量,选课时间默认为当前时间,其他字段分别是学号、课程号和教师号。
代码:

mysql>create table sc
(sc_no int(6) not null auto_increment, 
studentno  char(11) not null, 
courseno  char(6) not null, 
teacherno char(6) not null,  
sc_time timestamp not null default now(), 
primary key (sc_no) );

运行结果如图所示:
在这里插入图片描述


发布了38 篇原创文章 · 获赞 15 · 访问量 1690

猜你喜欢

转载自blog.csdn.net/hyh17808770899/article/details/105048477