use (schema);以test为例。

创建数据表

  • create table <表名>(字段 数据类型 [列级约束条件] [默认值] , … , [表级约束条件]);
    1. create table tb_emp1 (
    2. id int(11),
    3. name varchar(25),
    4. deptId int(11),
    5. salary float
    6. );

    查看数据表

show tables;

使用主键约束

单字段主键

  1. 字段名 数据类型 primary key [默认值]

定义时指定主键

create table tb_emp2 (
id int(11) primary key,
name varchar(25),
deptId int(11),
salary float
);
  1. constraint <约束名> primary key 字段名

定义之后指定

create table tb_emp3 (
id int(11),
name varchar(25),
deptId int(11),
salary float,
primary key (id)
);

多字段联合主键

  • primary key 字段1、字段2、….
    create table tb_emp4 (
    name varchar(25),
    deptId int(11),
    salary float,
    primary key (name,deptId)
    );
    

使用外键约束

  1. 外键用来在两个表的数据之间建立链接,它可以是一列或者多列。
  2. 必须是表中的一个字段。可以不是本表中的主键,但是必须是另一个表中的主键。
  3. 不允许删除在另一个表中具有关联关系的行。
  4. 外键可以为空值,若不为空值则必须等于另一个表中的某个主键。
  • constraint <外键名> foreign key [字段1、字段2、…] references {主表} 《主键列1、主键列2、…》

创建一个部门表

create table tb_dept1 (
id int(11) primary key,
name varchar(25) not null,
location varchar(50)
);

创建一个员工表并添加外键约束。

create table tb_emp5 (
id int(11) primary key,
name varchar(25),
deptId int(11),
salary float,
constraint fk_emp_dept1 foreign key(deptId) references tb_dept1(id)
);

使用非空约束

  • 字段 数据类型 not null
  • 非空约束是指字段不能为空。如果添加数据时没有指定值则报错。
    create table tb_emp6 (
    id int(11) primary key,
    name varchar(25) not null,
    deptId int(11),
    salary float
    );
    

    使用唯一性约束

  1. 字段 数据类型 unique
  • 要求该列值唯一,可以为空,但只能出现一个空值。
    create table tb_dept2 (
    id int(11) primary key,
    name varchar(25) unique,
    location varchar(50)
    );
    
  1. constraint <约束名> unique 字段名

定义后指定约束

create table tb_dept3 (
id int(11) primary key,
name varchar(25),
location varchar(50),
constraint sth unique(name)
);

使用默认约束

  • 字段 数据类型 default 默认值
  • 指定某列值的默认值。

    create table tb_emp7 (
    id int(11) primary key,
    name varchar(25) not null,
    deptId int(11) default 1,
    salary float
    );
    

    设置表的属性自动增加

  • 字段 数据类型 auto_increment ``` create table tb_emp8 ( id int(11) primary key auto_increment, name varchar(25) not null, deptId int(11), salary float );

测试插入

insert into tb_emp8 (name,salary) values (‘Lucy’,1000),(‘Lura’,1200),(‘Kevin’,1500);

查询

select * from tb_emp8;W


<a name="8e02707f"></a>
## 查看数据表结构

1. describe 表名
1. desc 表名
- 可以查看的信息有:字段名、字段数据类型、是否为主键、是否有默认值等。

describe tb_dept1;

desc tb_emp1;

<a name="c5b1f665"></a>
### 查看表的详细结构

1. show create table 表名
1. show create table 表名 \G;

show create table tb_emp1;

使用参数 \G

show create table tb_emp1\G;


<a name="dc15d5f9"></a>
## 修改数据表

<a name="9adf412d"></a>
### 修改表名
alter table [旧表名] rename <to> [新表名];  to为可选参数,一般没写。

alter table tb_dept3 rename tb_deptment3;

<a name="27c44360"></a>
### 修改字段的数据类型
alter table [表名] modify <字段名> {数据类型};

desc tb_dept1; alter table tb_dept1 modify name varchar(50); desc tb_dept1;

<a name="e86f1245"></a>
### 修改字段名
alter table [表名] chang <旧字段名> <新字段名> {数据类型};

- 也可以实现修改数据类型的效果,只需要将字段名保持一致就好了。

desc tb_dept1; alter table tb_dept1 change location loc varchar(50); desc tb_dept1;

<a name="4484fa04"></a>
### 添加字段
alter table [表名] add <新字段名> {数据类型} (可选约束条件 first、after) <已存在字段名>;
<a name="8ef741fb"></a>
#### 添加无完整性约束条件的字段

alter table tb_dept1 add managerId int(10);

<a name="e37a6c93"></a>
#### 添加有完整性约束条件的字段

alter table tb_dept1 add column1 varchar(12) not null;

<a name="647aee71"></a>
#### 在表的第一列添加一个字段

alter table tb_dept1 add column2 int(11) first;

<a name="016209c0"></a>
#### 在表的指定列添加一个字段

alter table tb_dept1 add column3 int(11) after name;

<a name="1cfd4c81"></a>
### 删除字段
alter table [表名] drop <字段名>;

alter table tb_dept1 drop column2;

<a name="bbef47b6"></a>
### 修改字段的排列位置
alter table [表名] modify <字段名1> {数据类型} (first | after) <字段名2>;
<a name="081b93e1"></a>
#### 修改字段表的第一个字段

alter table tb_dept1 modify column1 varchar(12) first;

<a name="a44d3288"></a>
#### 修改字段到表的指定列之后

alter table tb_dept1 modify column1 varchar(12) after location;

<a name="0498ba16"></a>
## 更改表的存储引擎
alter table [表名] engine=<要改为的引擎>;

show create table tb_deptment3\G; alter table tb_deptment3 engine=myisam;

<a name="b47f56e0"></a>
## 删除表的外键约束
alter table [表名] drop foreign key <外键约束名>;

创建tb_emp9,外键deptId关联tb_dept1表的主键id

create table tb_emp9( id int(11) primary key, name varchar(22), deptId int(11), salary float, constraint fk_emp_dept foreign key (deptId) references tb_dept1(id) );

查看表结构

show create table tb_emp9 \G;

删除外键约束

alter table tb_emp9 drop foreign key fk_emp_dept;

<a name="c2f224a3"></a>
## 删除数据表
<a name="638b0bfc"></a>
### 删除没有被关联的表

- drop table [if exists] <表名1、表名2、...>;

drop table if exists tb_dept2;

<a name="8b4e771e"></a>
### 删除被其他表关联的主表
在有外键关联的情况下,直接删除父表,会失败。原因是直接删除会破坏表的完整性,如果必须要删除,可以先删除与其关联的子表,再删除父表。保留子表则可以先删除关联外键约束,再删除父表。

创建部门表

create table tb_dept2 ( id int(11) primary key, name varchar(25) not null, location varchar(50) );

创建员工表

create table tb_emp ( id int(11) primary key, name varchar(25), deptId int(11), salary float, constraint fk_emp_dept foreign key (deptId) references tb_dept2(id) );

查看表结构

show create table tb_emp \G;

删除被数据表tb_emp关联的表tb_dept2

首先直接删除父表tb_dept2;

drop table tb_dept2;

可以看到报错,主表不能被直接删除。

解除关联的外键约束

alter table tb_emp drop foreign key fk_emp_dept;

再删除父表tb_dept2

drop table tb_dept2; ```

经过对上述操作的实操,更深刻认识和熟悉了MySQL数据表的基本操作。 光看是不会体会到其中的乐趣的。