一、数据类型
MySQL数据类型 | 含义 |
---|---|
float(m,d) | 4字节,单精度浮点型,m总个数,d小数位 |
double(m,d) | 8字节,双精度浮点型,m总个数,d小数位 |
decimal(m,d) | decimal是存储为字符串的浮点数 |
我在MySQL中建立了一个表,有一列为float(5,3);做了以下试验:
1.插入123.45678 ,最后查询得到的结果为99.999;
2.插入123.456,最后的查询结果为99.999;
3.插入12.34567,最后查询结果为12.346;
所以在使用浮点型的时候,还是要注意陷阱的,还是要以插入数据库中的实际结果为准
decimal(2,1):小数会四舍五入,整数会补充小数点
MySQL数据类型 | 含义 |
---|---|
char(n) | 固定长度:最多255个字符 |
varchar(n) | 固定长度:最多65535个字符 |
tinytext | 固定长度:最多255个字符 |
text | 可变长度:最多2 的65535个字符 |
mediumtext | 可变长度:最多2 的24次方-1 个字符 |
longtext | 可变长度:最多2 的32次方-1 个字符 |
【说明】
1、char(n)和varchar(n)中括号中n代表字符的个数,并不代表字节个数,所以当使用了中文的时候(UTF8)意味着可以插入m个中文,但是实际会占用M*3个字节;
2、同时char和varchar最大的区别就在于char不管实际value都会占用n个字符的空间,而varchar只会占用实际字符应该占用的空间+1,并且实际会占用+1<=n;
3、超过char和varchar的n设置后,字符串会被截断;
4、char的上限为255字节,varchar的上限为65535字节,text的上限为65535;
5、char会在存储的时候,截断尾部的空格,varchar和text不会;
6、varchar会使用1-3个字节来存储长度,text不会;
create table test01(
id int(5) ZEROFILL -- 设置id字段宽度为5,并且配合0填充
);
insert into test01 values(10); -- 插入一个值10
SELECT * from test01; -- 查看结果可以看到为00010,会使用0来填充满5个宽度
【表的管理-增-删-改-查】
二、练习
基本概念(随便了解就行了)
查看所有数据库:show databases
使用指定数据库:use 数据库名
查看所有表名(前提指定数据库):show tables
查看表结构:desc 表名
关于UTF8编码,MySQL中常用的支持utf-8的字符集有两种
-
mb3:是MySQL5.7及以前的默认UTF-8默认字符集,最多支持3个字节
-
mb4:是MySQL8.0及以后的默认UTF-8默认字符集,最多支持4个字节
1)和数据库相关的
创建数据库
create database 数据库名;
查看所有的数据库
show databases; #有一个s,代表多个数据库
查看某个数据库的详细定义语句
show create database 数据库名;
show create database 数据库名\G
修改数据库编码
#修改数据库字符集和校对规则
ALTER DATABASE 数据库名称 CHARACTER SET 字符集名称 COLLATE 字符集对应校对规则;
ALTER DATABASE atguigu_chapter3_two CHARACTER SET utf8 COLLATE utf8_general_ci;
==注意,==修改数据库编码只会影响之后创建的表的默认编码,之前创建的表不会受影响。
删除数据库
drop database 数据库名;
使用数据库
use 数据库名;
SQL示例演示
#演示和数据库相关的DDL语句
#查看当前登录用户(有权限)查看的所有的数据库
show databases;
#如何创建数据库
create database 数据库名称;
#例如,创建数据库bookstore
create database bookstore;
#查看数据库的定义
show create database 数据库名;
#例如,查看bookstore数据库的定义信息
show create database bookstore;
show create database bookstore\G
*************************** 1. row ***************************
Database: bookstore
Create Database: CREATE DATABASE `bookstore` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */
1 row in set (0.00 sec)
#修改数据库的编码字符集和校对规则
alter database 数据库名称 CHARACTER SET 字符集名称 COLLATE 校对规则
#修改bookstore数据库的字符集和校对规则
alter database bookstore CHARACTER SET Latin1 COLLATE Latin1_general_ci;
#Latin1字符集不支持中文
mysql> show create database bookstore;
+-----------+--------------------------------------------+
| Database | Create Database |
+-----------+----------------------------------------------------+
| bookstore | CREATE DATABASE `bookstore` /*!40100 DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci */ /*!80016 DEFAULT ENCRYPTION='N' */ |
+-----------+---------------------------------+
1 row in set (0.00 sec)
#使用数据库
use 数据库名;
#例如:使用bookstore数据库
use bookstore;
#创建表格
create table temp(
s char
);
#添加数据
insert into temp values('a');
#查询数据
mysql> select * from temp;
+------+
| s |
+------+
| a |
+------+
1 row in set (0.00 sec)
#添加数据
insert into temp values('尚');
mysql> insert into temp values('尚');
ERROR 1366 (HY000): Incorrect string value: '\xC9\xD0' for column 's' at row 1
#删除数据库
drop database 数据库名称;
#例如:删除bookstore数据库
drop database bookstore;
2)和数据表相关的
查看某个数据库的所有表格
use 数据库名;
show tables;
show tables from 数据库名;
创建表格
create table 数据表名称(
字段名 数据类型,
字段名 数据类型
);
create table teacher(
tid int,
tname varchar(5),
salary double,
weight double(5,2),
birthday date,
gender enum('男','女'),
blood enum('A','B','AB','O'),
tel char(11)
);
查看表的详细定义信息
show create table 表名称;
show create table 表名称\G
mysql> show create table teacher\G
*************************** 1. row ***************************
Table: teacher
Create Table: CREATE TABLE `teacher` (
`tid` int(11) DEFAULT NULL,
`tname` varchar(5) DEFAULT NULL,
`salary` double DEFAULT NULL,
`weight` double(5,2) DEFAULT NULL,
`birthday` date DEFAULT NULL,
`gender` enum('男','女') DEFAULT NULL,
`blood` enum('A','B','AB','O') DEFAULT NULL,
`tel` char(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
查看表结构
desc 表名称;
mysql> desc teacher;
+----------+------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------------+------+-----+---------+-------+
| tid | int(11) | YES | | NULL | |
| tname | varchar(5) | YES | | NULL | |
| salary | double | YES | | NULL | |
| weight | double(5,2) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| gender | enum('男','女') | YES | | NULL | |
| blood | enum('A','B','AB','O') | YES | | NULL | |
| tel | char(11) | YES | | NULL | |
+----------+------------------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
删除表格,包括表结构和里面的数据
drop table 表名称;
修改表结构:删除字段
alter table 表名称 drop 【column】 字段名称;
alter table teacher drop column weight;
修改表结构:增加字段
alter table 表名称 add 【column】 字段名称 数据类型;
alter table 表名称 add 【column】 字段名称 数据类型 first;
alter table 表名称 add 【column】 字段名称 数据类型 after 另一个字段;
alter table teacher add weight double(5,2);
alter table teacher drop column weight;
alter table teacher add weight double(5,2) first;
alter table teacher drop column weight;
alter table teacher add weight double(5,2) after salary;
alter table teacher drop column weight;
修改表结构:修改字段的数据类型
alter table 表名称 modify 【column】 字段名称 新的数据类型;
mysql> desc teacher;
+----------+------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------------+------+-----+---------+-------+
| tid | int(11) | YES | | NULL | |
| tname | varchar(5) | YES | | NULL | |
| salary | double | YES | | NULL | |
| weight | double(5,2) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| gender | enum('男','女') | YES | | NULL | |
| blood | enum('A','B','AB','O') | YES | | NULL | |
| tel | char(11) | YES | | NULL | |
+----------+------------------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
mysql> alter table teacher modify column weight double;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc teacher;
+----------+------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------------+------+-----+---------+-------+
| tid | int(11) | YES | | NULL | |
| tname | varchar(5) | YES | | NULL | |
| salary | double | YES | | NULL | |
| weight | double | YES | | NULL | |
| birthday | date | YES | | NULL | |
| gender | enum('男','女') | YES | | NULL | |
| blood | enum('A','B','AB','O') | YES | | NULL | |
| tel | char(11) | YES | | NULL | |
+----------+------------------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
修改表结构:修改字段的名称
alter table 表名称 change 【column】 旧字段名称 新的字段名称 新的数据类型;
mysql> desc teacher;
+----------+------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------------+------+-----+---------+-------+
| tid | int(11) | YES | | NULL | |
| tname | varchar(5) | YES | | NULL | |
| salary | double | YES | | NULL | |
| weight | double | YES | | NULL | |
| birthday | date | YES | | NULL | |
| gender | enum('男','女') | YES | | NULL | |
| blood | enum('A','B','AB','O') | YES | | NULL | |
| tel | char(11) | YES | | NULL | |
+----------+------------------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
mysql> alter table teacher change tel phone char(11);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc teacher;
+----------+------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------------+------+-----+---------+-------+
| tid | int(11) | YES | | NULL | |
| tname | varchar(5) | YES | | NULL | |
| salary | double | YES | | NULL | |
| weight | double | YES | | NULL | |
| birthday | date | YES | | NULL | |
| gender | enum('男','女') | YES | | NULL | |
| blood | enum('A','B','AB','O') | YES | | NULL | |
| phone | char(11) | YES | | NULL | |
+----------+------------------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
修改表结构:修改字段位置
alter table 表名称 modify 【column】 字段名称 数据类型 first;
alter table 表名称 modify 【column】 字段名称 数据类型 after 另一个字段;
修改表名称(重命名表)
alter table 旧表名 rename 【to】 新表名;
rename table 旧表名称 to 新表名称;
例如:
alter table teacher rename to t_tea;
rename table t_tea to teacher;
上课SQL示例演示
#演示和数据表相关的DDL语句
#为了方便接下来的演示,最好在前面确定针对哪个数据库的表格演示
#使用数据库
use atguigu;
#查看当前登录用户在本库下能够看到的所有表格
show tables;
#如果前面没有use语句,或者在当前use语句下,要查看另一个数据库的表格。
show tables from 数据库名;
#例如:查看当前数据库的表格
show tables;
#例如:在当前use atguigu;下面,查看mysql库的表格
show tables from mysql;
#创建表格
create table 表名称(
字段名1 数据类型1,
字段名2 数据类型2 #如果后面没有其他字段或约束的定义,后面就不用加,
);
#例如:创建一个teacher表
/*
包含编号、姓名、性别、出生日期、薪资、电话号码
*/
create table teacher(
id int,
name varchar(20),
gender enum('男','女'),
birthday date,
salary double,
tel varchar(11)
);
#查看表结构
desc 表名称;
describe 表名称;
#例如:查看teacher表的结构
desc teacher;
describe teacher;
mysql> describe teacher;
+----------+-----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| gender | enum('男','女') | YES | | NULL | |
| birthday | date | YES | | NULL | |
| salary | double | YES | | NULL | |
| tel | varchar(11) | YES | | NULL | |
+----------+-----------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
#查看表格的详细定义
show create table 表名称;
#例如:查看teacher表的定义语句
show create table teacher;
mysql> show create table teacher\G
*************************** 1. row ***************************
Table: teacher
Create Table: CREATE TABLE `teacher` (
`id` int DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`gender` enum('男','女') DEFAULT NULL,
`birthday` date DEFAULT NULL,
`salary` double DEFAULT NULL,
`tel` varchar(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
#修改表结构
#增加一个字段
alter table 表名称 add column 字段名 数据类型;
#column表示列,字段,可以省略
#例如:给teacher表增加一个address varchar(100)字段
alter table teacher add column address varchar(100);
mysql> desc teacher;
+----------+-----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| gender | enum('男','女') | YES | | NULL | |
| birthday | date | YES | | NULL | |
| salary | double | YES | | NULL | |
| tel | varchar(11) | YES | | NULL | |
| address | varchar(100) | YES | | NULL | |
+----------+-----------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
#在某个字段后面增加一个字段
alter table 表名称 add column 字段名 数据类型 after 另一个字段;
#column表示列,字段,可以省略
#例如:给teacher表增加一个cardid char(18)字段,增加到name后面
alter table teacher add column cardid char(18) after name;
mysql> desc teacher;
+----------+-----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| cardid | char(18) | YES | | NULL | |
| gender | enum('男','女') | YES | | NULL | |
| birthday | date | YES | | NULL | |
| salary | double | YES | | NULL | |
| tel | varchar(11) | YES | | NULL | |
| address | varchar(100) | YES | | NULL | |
+----------+-----------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
#增加一个字段,称为第一个字段
alter table 表名称 add column 字段名 数据类型 first;
#column表示列,字段,可以省略
#例如:给teacher表增加一个age int字段,增加到id前面
alter table teacher add column age int first;
mysql> desc teacher;
+----------+-----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------------+------+-----+---------+-------+
| age | int | YES | | NULL | |
| id | int | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| cardid | char(18) | YES | | NULL | |
| gender | enum('男','女') | YES | | NULL | |
| birthday | date | YES | | NULL | |
| salary | double | YES | | NULL | |
| tel | varchar(11) | YES | | NULL | |
| address | varchar(100) | YES | | NULL | |
+----------+-----------------+------+-----+---------+-------+
9 rows in set (0.01 sec)
#删除字段
alter table 表名称 drop column 字段名;
#column可以省略
#例如:删除teacher表的age字段
alter table teacher drop column age;
mysql> desc teacher;
+----------+-----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| cardid | char(18) | YES | | NULL | |
| gender | enum('男','女') | YES | | NULL | |
| birthday | date | YES | | NULL | |
| salary | double | YES | | NULL | |
| tel | varchar(11) | YES | | NULL | |
| address | varchar(100) | YES | | NULL | |
+----------+-----------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
#修改字段的数据类型
alter table 表名称 modify column 字段名 新的数据类型;
#例如:修改teacher表的salary字段,数据类型修改为double(10,2)
alter table teacher modify column salary double(10,2);
mysql> desc teacher;
+----------+-----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| cardid | char(18) | YES | | NULL | |
| gender | enum('男','女') | YES | | NULL | |
| birthday | date | YES | | NULL | |
| salary | double(10,2) | YES | | NULL | |
| tel | varchar(11) | YES | | NULL | |
| address | varchar(100) | YES | | NULL | |
+----------+-----------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
#修改字段的名称
alter table 表名称 change column 旧字段名 新的字段名 数据类型;
#例如:修改teacher表的tel字段,字段名修改为telphone
alter table teacher change column tel telphone char(18);
mysql> desc teacher;
+----------+-----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| cardid | char(18) | YES | | NULL | |
| gender | enum('男','女') | YES | | NULL | |
| birthday | date | YES | | NULL | |
| salary | double(10,2) | YES | | NULL | |
| telphone | char(18) | YES | | NULL | |
| address | varchar(100) | YES | | NULL | |
+----------+-----------------+------+-----+---------+-------+
8 rows in set (0.01 sec)
#修改字段的顺序
alter table 表名称 modify column 字段名 数据类型 after 另一个字段;
alter table 表名称 modify column 字段名 数据类型 first;
#例如,把teacher表的salary调整到telphone后面
alter table teacher modify column salary double(10,2) after telphone;
mysql> desc teacher;
+----------+-----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| cardid | char(18) | YES | | NULL | |
| gender | enum('男','女') | YES | | NULL | |
| birthday | date | YES | | NULL | |
| telphone | char(18) | YES | | NULL | |
| salary | double(10,2) | YES | | NULL | |
| address | varchar(100) | YES | | NULL | |
+----------+-----------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
#修改表名称
rename table 旧表名称 to 新表名称;
alter table 表名称 rename 新表名称;
#把teacher表重命名为jiaoshi
rename table teacher to jiaoshi;
#把jiaoshi表重命名为teacher
alter table jiaoshi rename teacher;
#删除表结构(数据一并删除)
drop table 表名称;
#删除teacher表格
drop table teacher;
三、总结
MySQL指令基本分为三类:DDL(数据定义语言)、DML(数据操纵语言)、DCL(数据控制语言)。
DDL:
DDL指令的功能就是定义数据库DATabase、表table、索引index、视图view、列column等。
DDL与DML的区别就在与DDL是对表进行定义、对结构进行修改,DML只能处理数据库中的数据,不能对表结构进行更改。
关键字有:insert、delete、update、SELECT、drop等,常用DDL语言包括以下:
以下主要总结MySQL的数据库操作(DDL),表操作方面的常用DDL(Data Definition Language)语句。
1)数据库操作
-- 创建数据库
create database dbname;
-- 查看数据库;
show databases;
-- 使用指定数据库
use dbname;
-- 删除数据库
drop database dbname;
2)表操作
1-创建表
-- 创建表
create table emp (
ename varchar(10),
hiredate date,
sal decimal(10,2),
deptno int(2)
);
-- 快速创建一个表结构相同的表
CREATE TABLE new_tablename LIKE old_tablename;
-- 创建表时增加索引
CREATE TABLE actor (
actor_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id),
KEY idx_actor_last_name (last_name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- 创建表时增加外键
CREATE TABLE city (
city_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
city VARCHAR(50) NOT NULL,
country_id SMALLINT UNSIGNED NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (city_id),
KEY idx_fk_country_id (country_id),
CONSTRAINT `fk_city_country` FOREIGN KEY (country_id) REFERENCES country (country_id) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
在创建索引时, 可以指定在删除、更新父表时,对子表进行的相应操作,包括 RESTRICT、CASCADE、SET NULL和 NO ACTION。
RESTRICT和NO ACTION 是指限制在子表有关联记录的情况下, 父表不能更新,RESTRICT为默认行为;
CASCADE表示父表在更新或者删除时,更新或者删除子表对应的记录;
SET NULL 则表示父表在更新或者删除的时候,子表的对应字段被SET NULL 。
2-查看表
-- 查看表
mysql> desc emp;
+----------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| ename | varchar(10) | YES | | NULL | |
| hiredate | date | YES | | NULL | |
| sal | decimal(10,2) | YES | | NULL | |
| deptno | int(2) | YES | | NULL | |
+----------+---------------+------+-----+---------+-------+
mysql> show create table emp \G;
*************************** 1. row ***************************
Table: emp
Create Table: CREATE TABLE `emp` (
`ename` varchar(10) DEFAULT NULL,
`hiredate` date DEFAULT NULL,
`sal` decimal(10,2) DEFAULT NULL,
`deptno` int(2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
-- 删除表
DROP TABLE tablename;
drop table emp;
3-修改表
-- 修改表名
ALTER TABLE tablename RENAME[TO] new_tablename;
-- 将表名emp改为emp1
ALTER TABLE emp rename emp1;
-- 修改字符集
ALTER TABLE tablename character set 字符集;
ALTER TABLE student character set utf8;
-- 修改表类型
ALTER TABLE tablename MODIFY[COLUMN] column_definition[FIRST|AFTER col_name];
-- 修改emp的ename字段定义,将varchar(10)改为varchar(20)
alter table emp modify ename varchar(20);
-- 增加表字段
ALTER TABLE tablename add[COLUMN]column_definition[FIRST|AFTER col_name];
-- 向emp中新增字段age,类型为int(3)
alter table emp add column age int(3);
-- 删除表字段
ALTER TABLE tablename DROP[COLUMN]col_name;
-- 将字段age删除
alter table emp drop age;
-- 字段改名
ALTER TABLE tablename CHANGE[COLUMN] old_col_name new_col_name column_definition;
-- 将age改为age1 同时类型由int(3) 改为int(4)
alter table emp change age age1 int(4);
-- 修改字段排列顺序
-- 将新增字段birth date 加在ename之后
mysql> desc emp;
+----------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| ename | varchar(20) | YES | | NULL | |
| hiredate | date | YES | | NULL | |
| sal | decimal(10,2) | YES | | NULL | |
| deptno | int(2) | YES | | NULL | |
| age | int(4) | YES | | NULL | |
+----------+---------------+------+-----+---------+-------+
mysql> alter table emp add birth date after ename;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc emp;
+----------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| ename | varchar(20) | YES | | NULL | |
| birth | date | YES | | NULL | |
| hiredate | date | YES | | NULL | |
| sal | decimal(10,2) | YES | | NULL | |
| deptno | int(2) | YES | | NULL | |
| age | int(4) | YES | | NULL | |
+----------+---------------+------+-----+---------+-------+
-- 将age移到ename后面
alter table emp modify age int(3) after ename ;
-- 修改外键
-- 或者修改
alter table city_innodb
drop foreign key fk_city_country;
alter table city_innodb
add CONSTRAINT `fk_city_country` foreign key (`country_id`)
references country_innodb (country_id) on delete restrict on update cascade;
如果觉得我笔记写的好的,可以点赞加关注,也可以加微信一起学习交流