MySQL数据表基本操作{记录2}{创建数据表}

1,创建数据表:使用CREATE TABLE语句

CREATE TABLE 表名 (
    列名 列类型 字段属性,
    列名 列类型 字段属性,
    PRIMARY KEY ( `定义为主键的列名` )
)ENGINE=设置储存引擎 DEFAULT CHARSET=设置编码;

 例1:在work中创建表offices,结构如下

字段名

数据类型

主键

外键

非空

唯一

officecode

int(10)

city

int(11)

address

varchar(50)

country

varchar(50)

postalcode

varchar(25)

 

1 CREATE TABLE offices(
2 officecode INT(10),
3 city INT(11) NOT NULL,
4 address VARCHAR(50),
5 country VARCHAR(50) NOT NULL,
6 postalcode VARCHAR(25) UNIQUE,
7 PRIMARY KEY(`officecode`)
8 )ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

从MySQL 8.0.17开始,上述代码会报错:

0 row(s) affected, 2 warning(s): 1681 Integer display width is deprecated and will be removed in a future release. 1681 Integer display width is deprecated and will be removed in a future release.

原因为:从MySQL 8.0.17开始,对于整数数据类型,不建议使用display width属性,即不用M显示宽度,并且在将来的MySQL版本中将删除对它的支持。

解决方法:不指定宽度  即INT 后面不要加宽度

代码:

CREATE TABLE offices(
officecode INT,
city INT NOT NULL,
address VARCHAR(50),
country VARCHAR(50) NOT NULL,
postalcode VARCHAR(25) UNIQUE,
PRIMARY KEY(`officecode`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

 例2:在work创建表employees,结构如下:

 

字段名

数据类型

主键

外键

非空

唯一

employeenumber

int(11)

lastname

varchar(50)

firstname

varchar(50)

mobile

varchar(25)

officecode

int(10)

jobtitle

varchar(50)

birth

Datetime

note

varchar(255)

sex

varchar(5)

 

代码:

 1 CREATE TABLE employees(
 2 employeenumber INT(11) primary key,
 3 lastname varchar(50) not null,
 4 firstname varchar(50) not null,
 5 mobile varchar(50) unique,
 6 officecode int(10) not null,
 7 jobtitle varchar(50) not null,
 8 birth datetime not null,
 9 note varchar(255),
10 sex varchar(5),
11 constraint fk_off foreign key (officecode) references offices(officecode)
12 )ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

 

posted @ 2022-03-30 10:09  ILEQ  阅读(279)  评论(0编辑  收藏  举报