This slide is based on Jeffrey D. Ullman's work, which can be download from his website.
References: 刘增杰、张少军 《MySQL 5.5 从零开始学》
C:\>mysql -uroot -p Enter password: 123456
登录成功后, 显示如下:
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 576 Server version: 5.6.17 MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
查看 MySQL 的版本.
mysql> select version(); +-----------+ | version() | +-----------+ | 5.6.17 | +-----------+ 1 row in set (0.04 sec)
使用
mysql> quit Bye
MySQL Shell 是 MySQL 新一代的高级客户端和代码编辑器,是 Oracle 公司提供的一个交互式命令行工具。对比自带的客户端工具 mysql ,MySQL Shell 不仅可以通过它执行传统的 SQL 语句,还可以使用包括 Python 和 JavaScript 在内的编程语言与服务器进行交互,为用户提供更多的选择和灵活性,而且为 MySQL 的不同产品(如 MySQL Server,MySQL Router,MySQL Innodb Cluster等)提供了一个统一接口。与此同时 MySQL Shell 还集成了很多功能,例如数据库查询和更新,数据库管理,集群管理,插件支持,备份恢复等。 MySQL Shell 8.0 可与 MySQL Server 8.0 和 5.7 一起使用。
首先使用
C:\Users\haife>mysqlsh MySQL Shell 8.0.21 Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type '\help' or '\?' for help; '\quit' to exit. MySQL JS >
使用
MySQL JS > \connect root@localhost:3306
根据提示输入
MySQL JS > \connect root@localhost:3306 Creating a session to 'root@localhost:3306' Fetching schema names for autocompletion... Press ^C to stop. Your MySQL connection id is 11 Server version: 5.7.23 MySQL Community Server (GPL) No default schema selected; type \useto set one.
使用
MySQL localhost:3306 JS > \sql Switching to SQL mode... Commands end with ;
然后就可以查询数据库了.
也可以直接登录数据库.
C:\Users\haife>mysqlsh -uroot -p -hlocalhost -P3306 MySQL Shell 8.0.21 Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type '\help' or '\?' for help; '\quit' to exit. Creating a session to 'root@localhost:3306' Fetching schema names for autocompletion... Press ^C to stop. Your MySQL connection id is 15 Server version: 5.7.23 MySQL Community Server (GPL) No default schema selected; type \useto set one. MySQL localhost:3306 JS >
重新登录后, 使用下面的命令看一下已经存在有多少数据库.
mysql> SHOW DATABASES;
+--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sakila | | test | | world | +--------------------+ 6 rows in set (0.00 sec)
mysql> CREATE DATABASE test_bar;
确认是否已创建了
mysql> SHOW DATABASES;
也可以使用下面的命令, 尝试使用
mysql> SHOW CREATE DATABASE test_bar\G;
删除刚才的数据库
mysql> DROP DATABASE test_bar;
使用
为了继续实验, 我们重新创建一下数据库
mysql> CREATE DATABASE test_bar;
mysql> use test_bar; Database changed mysql> select database(); +------------+ | database() | +------------+ | test_bar | +------------+
上面的
使用
MySQL localhost:3306 SQL > \use chinamath Default schema set to `chinamath`. Fetching table and column names from `chinamath` for auto-completion... Press ^C to stop.
MySQL localhost:3306 chinamath SQL > show tables; +---------------------+ | Tables_in_chinamath | +---------------------+ | students | +---------------------+ 1 row in set (0.0015 sec)
mysqldump -uroot -p dbName > dbName-2014-5-10.sql
mysql -uroot -p < dbName-2014-5-10.sql
在创建数据库时指定编码, 如果是在 Windows 下, 我们使用 GBK 编码.
mysql> CREATE DATABASE test_bar -> CHARACTER SET gbk -> COLLATE gbk_chinese_ci;
mysql> USE test_bar; mysql> SET NAMES 'gbk';
如果进入数据库发现数据有乱码, 首先查看该数据库或表的具体创建信息. 使用下面的命令
SHOW CREATE DATABASE test_bar; SHOW CREATE TABLE Sells\G;
SET NAMES 'gbk';
如果刚才输入
要查看错误信息, 可以使用下面的语句.
mysql> SHOW WARNINGS;
如果当输入
可以使用
mysql> slect -> database() -> \c mysql>
prompt | meaning |
---|---|
mysql> | Ready for new command. |
-> | Waiting for next line of multiple-line command. |
'> | Waiting for next line, waiting for completion of a string that began with a single quote (‘'’). |
"> | Waiting for next line, waiting for completion of a string that began with a double quote (‘"’). |
`> | Waiting for next line, waiting for completion of an identifier that began with a backtick (‘`’). |
/*> | Waiting for next line, waiting for completion of a comment that began with /*. |
在 Unix 和 Linux 系统上, mysql 将执行的语句记录到一个文件中. 默认的, 这个历史文件被命名为
在 Windows 下, 如果在MySQL的交互模式下需要记录所执行的命令. 则在启动 MySQL 时使用
假设我们要将 mysql 的历史命令记录存储到 D:\tmp\mysql_history.txt 文件中. 首先切换到目录 D:\tmp, 然后执行下面的命令.
mysql -uroot -p --tee mysql_history.txt
-- Beers(name, manf) -- Bars(name, addr, license) -- Drinkers(name, addr, phone) -- Likes(drinker, beer) -- Sells(bar, beer, price) -- Frequents(drinker, bar) CREATE TABLE Beers ( name VARCHAR(20) UNIQUE, manf VARCHAR(50) ); CREATE TABLE Bars ( name VARCHAR(20) PRIMARY KEY, addr VARCHAR(255), license VARCHAR(255) ); CREATE TABLE Drinkers ( name VARCHAR(20) PRIMARY KEY, addr VARCHAR(255), phone VARCHAR(15) ); CREATE TABLE Likes ( drinker VARCHAR(20), beer VARCHAR(255), PRIMARY KEY (drinker,beer) ); CREATE TABLE Sells ( bar VARCHAR(20), beer VARCHAR(20), price REAL, PRIMARY KEY (bar, beer) ); CREATE TABLE Frequents ( drinker CHAR(20), bar VARCHAR(255), PRIMARY KEY (drinker,bar) );
mysql> USE test_bar;
mysql> CREATE TABLE Beers ( -> name VARCHAR(20) UNIQUE, -> manf VARCHAR(50) -> );
mysql> CREATE TABLE Bars ( -> name VARCHAR(20) PRIMARY KEY, -> addr VARCHAR(255), -> license VARCHAR(255) -> );
mysql> CREATE TABLE Drinkers ( -> name VARCHAR(20) PRIMARY KEY, -> addr VARCHAR(255), -> phone VARCHAR(15) -> );
mysql> CREATE TABLE Likes ( -> drinker VARCHAR(20), -> beer VARCHAR(255), -> PRIMARY KEY (drinker,beer) -> );
mysql> CREATE TABLE Sells ( -> bar VARCHAR(20), -> beer VARCHAR(20), -> price REAL, -> PRIMARY KEY (bar, beer) -> );
mysql> CREATE TABLE Frequents ( -> drinker CHAR(20), -> bar VARCHAR(255), -> PRIMARY KEY (drinker,bar) -> );
mysql> SHOW TABLES; +--------------------+ | Tables_in_test_bar | +--------------------+ | Bars | | Beers | | Drinkers | | Frequents | | Likes | | Sells | +--------------------+ 6 rows in set (0.00 sec)
可以将上面的若干
mysql> source D:/tmp/schema.sql;
注意这里不能写成
mysql> INSERT INTO Beers (name, manf) -> VALUES ('百威', '百威英博啤酒集团');
不妨查看一下
mysql> SELECT * FROM Beers; +--------+--------------------------+ | name | manf | +--------+--------------------------+ | 百威 | 百威英博啤酒集团 | +--------+--------------------------+ 1 row in set (0.00 sec)
将下面的内容保存到文件:
INSERT INTO Beers (name, manf) VALUES ('贝克', '百威英博啤酒集团'); INSERT INTO Beers (name, manf) VALUES ('喜力', '喜利得(中国)有限公司'); INSERT INTO Beers (name, manf) VALUES ('嘉士伯', '嘉士伯啤酒集团'); INSERT INTO Beers (name, manf) VALUES ('安贝夫', '英博啤酒集团下巴西安贝夫公司'); INSERT INTO Beers (name, manf) VALUES ('南非啤酒', '南非啤酒集团'); INSERT INTO Beers (name, manf) VALUES ('米勒', 'SAB 公司'); INSERT INTO Beers (name, manf) VALUES ('科罗娜', '墨西哥摩洛哥啤酒公司'); INSERT INTO Beers (name, manf) VALUES ('Stella Artois', '百威英博啤酒集团'); INSERT INTO Beers (name, manf) VALUES ('朝日', '朝日啤酒株式会社'); INSERT INTO Beers (name, manf) VALUES ('麒麟', '麒麟(中国)投资有限公司'); INSERT INTO Beers (name, manf) VALUES ('生力', '香港生力啤酒廠有限公司'); INSERT INTO Beers (name, manf) VALUES ('健力士黑啤', '亚瑟健力士父子有限公司'); INSERT INTO Beers (name, manf) VALUES ('纯种苦啤酒', '马斯顿-汤普生-埃弗谢德公司'); INSERT INTO Bars (name, addr, license) VALUES ('3DArtBar', '上海虹梅路3338弄9-11号', 'BHXGZX'); INSERT INTO Bars (name, addr, license) VALUES ('HardRock', '上海政通路315号', 'XJ8QGF'); INSERT INTO Bars (name, addr, license) VALUES ('Westside', '上海衡山路237号', 'EDS9HF'); INSERT INTO Bars (name, addr, license) VALUES ('木板房啤酒吧', '上海子长路350号', 'YJVI9A'); INSERT INTO Bars (name, addr, license) VALUES ('宝莱纳餐厅', '上海徐汇区汾阳路150号', 'SKAPXJ'); INSERT INTO Bars (name, addr, license) VALUES ('苏荷酒吧', '扬州1912', 'LYMTD5'); INSERT INTO Bars (name, addr, license) VALUES ('扬州老啤酒厂酒吧', '广陵区南通东路128号', '8NZBSG');
mysql> source D:/tmp/insert_data.sql;
mysql> SELECT * FROM Beers; +-----------------+--------------------------------------------+ | name | manf | +-----------------+--------------------------------------------+ | 百威 | 百威英博啤酒集团 | | 贝克 | 百威英博啤酒集团 | | 喜力 | 喜利得(中国)有限公司 | | 嘉士伯 | 嘉士伯啤酒集团 | | 安贝夫 | 英博啤酒集团下巴西安贝夫公司 | | 南非啤酒 | 南非啤酒集团 | | 米勒 | SAB 公司 | | 科罗娜 | 墨西哥摩洛哥啤酒公司 | | Stella Artois | 百威英博啤酒集团 | | 朝日 | 朝日啤酒株式会社 | | 麒麟 | 麒麟(中国)投资有限公司 | | 生力 | 香港生力啤酒廠有限公司 | | 健力士黑啤 | 亚瑟健力士父子有限公司 | | 纯种苦啤酒 | 马斯顿-汤普生-埃弗谢德公司 | +-----------------+--------------------------------------------+ 14 rows in set (0.00 sec)
mysql> select * from Bars; +--------------------------+---------------------------------+---------+ | name | addr | license | +--------------------------+---------------------------------+---------+ | 3DArtBar | 上海虹梅路3338弄9-11号 | BHXGZX | | HardRock | 上海政通路315号 | XJ8QGF | | Westside | 上海衡山路237号 | EDS9HF | | 宝莱纳餐厅 | 上海徐汇区汾阳路150号 | SKAPXJ | | 扬州老啤酒厂酒吧 | 广陵区南通东路128号 | 8NZBSG | | 木板房啤酒吧 | 上海子长路350号 | YJVI9A | | 苏荷酒吧 | 扬州1912 | LYMTD5 | +--------------------------+---------------------------------+---------+ 7 rows in set (0.00 sec)
下载数据文件
INSERT INTO Beers (name, manf) VALUES ('百威', '百威英博啤酒集团'); INSERT INTO Beers (name, manf) VALUES ('贝克', '百威英博啤酒集团'); INSERT INTO Beers (name, manf) VALUES ('喜力', '喜利得(中国)有限公司'); INSERT INTO Beers (name, manf) VALUES ('嘉士伯', '嘉士伯啤酒集团'); INSERT INTO Beers (name, manf) VALUES ('安贝夫', '英博啤酒集团下巴西安贝夫公司'); INSERT INTO Beers (name, manf) VALUES ('南非啤酒', '南非啤酒集团'); INSERT INTO Beers (name, manf) VALUES ('米勒', 'SAB 公司'); INSERT INTO Beers (name, manf) VALUES ('科罗娜', '墨西哥摩洛哥啤酒公司'); INSERT INTO Beers (name, manf) VALUES ('Stella Artois', '百威英博啤酒集团'); INSERT INTO Beers (name, manf) VALUES ('朝日', '朝日啤酒株式会社'); INSERT INTO Beers (name, manf) VALUES ('麒麟', '麒麟(中国)投资有限公司'); INSERT INTO Beers (name, manf) VALUES ('生力', '香港生力啤酒廠有限公司'); INSERT INTO Beers (name, manf) VALUES ('健力士黑啤', '亚瑟健力士父子有限公司'); INSERT INTO Beers (name, manf) VALUES ('纯种苦啤酒', '马斯顿-汤普生-埃弗谢德公司'); INSERT INTO Bars (name, addr, license) VALUES ('3DArtBar', '上海虹梅路3338弄9-11号', 'BHXGZX'); INSERT INTO Bars (name, addr, license) VALUES ('HardRock', '上海政通路315号', 'XJ8QGF'); INSERT INTO Bars (name, addr, license) VALUES ('Westside', '上海衡山路237号', 'EDS9HF'); INSERT INTO Bars (name, addr, license) VALUES ('木板房啤酒吧', '上海子长路350号', 'YJVI9A'); INSERT INTO Bars (name, addr, license) VALUES ('宝莱纳餐厅', '上海徐汇区汾阳路150号', 'SKAPXJ'); INSERT INTO Bars (name, addr, license) VALUES ('苏荷酒吧', '扬州1912', 'LYMTD5'); INSERT INTO Bars (name, addr, license) VALUES ('扬州老啤酒厂酒吧', '广陵区南通东路128号', '8NZBSG'); INSERT INTO Sells (bar, beer, price) VALUES ('3DArtBar', '百威', 30); INSERT INTO Sells (bar, beer, price) VALUES ('3DArtBar', '贝克', 35); INSERT INTO Sells (bar, beer, price) VALUES ('3DArtBar', '喜力', 25); INSERT INTO Sells (bar, beer, price) VALUES ('3DArtBar', '嘉士伯', 32); INSERT INTO Sells (bar, beer, price) VALUES ('3DArtBar', '安贝夫', 40); INSERT INTO Sells (bar, beer, price) VALUES ('HardRock', '百威', 32); INSERT INTO Sells (bar, beer, price) VALUES ('HardRock', '贝克', 33); INSERT INTO Sells (bar, beer, price) VALUES ('HardRock', '喜力', 28); INSERT INTO Sells (bar, beer, price) VALUES ('HardRock', '嘉士伯', 30); INSERT INTO Sells (bar, beer, price) VALUES ('HardRock', '安贝夫', 39); INSERT INTO Sells (bar, beer, price) VALUES ('HardRock', '科罗娜', 40); INSERT INTO Sells (bar, beer, price) VALUES ('HardRock', '健力士黑啤', 39); INSERT INTO Sells (bar, beer, price) VALUES ('Westside', '百威', 32); INSERT INTO Sells (bar, beer, price) VALUES ('Westside', '纯种苦啤酒', 33); INSERT INTO Sells (bar, beer, price) VALUES ('Westside', '贝克', 28); INSERT INTO Sells (bar, beer, price) VALUES ('Westside', '南非啤酒', 36); INSERT INTO Sells (bar, beer, price) VALUES ('Westside', '朝日', 26); INSERT INTO Sells (bar, beer, price) VALUES ('Westside', '科罗娜', 40); INSERT INTO Sells (bar, beer, price) VALUES ('Westside', '健力士黑啤', 39); INSERT INTO Sells (bar, beer, price) VALUES ('木板房啤酒吧', '麒麟', 30); INSERT INTO Sells (bar, beer, price) VALUES ('木板房啤酒吧', '贝克', 35); INSERT INTO Sells (bar, beer, price) VALUES ('木板房啤酒吧', '生力', 25); INSERT INTO Sells (bar, beer, price) VALUES ('木板房啤酒吧', '米勒', 32); INSERT INTO Sells (bar, beer, price) VALUES ('木板房啤酒吧', '嘉士伯', 43); INSERT INTO Sells (bar, beer, price) VALUES ('木板房啤酒吧', '安贝夫', 40); INSERT INTO Sells (bar, beer, price) VALUES ('宝莱纳餐厅', '百威', 32); INSERT INTO Sells (bar, beer, price) VALUES ('宝莱纳餐厅', '纯种苦啤酒', 33); INSERT INTO Sells (bar, beer, price) VALUES ('宝莱纳餐厅', '喜力', 28); INSERT INTO Sells (bar, beer, price) VALUES ('宝莱纳餐厅', '南非啤酒', 36); INSERT INTO Sells (bar, beer, price) VALUES ('宝莱纳餐厅', '朝日', 26); INSERT INTO Sells (bar, beer, price) VALUES ('宝莱纳餐厅', '科罗娜', 40); INSERT INTO Sells (bar, beer, price) VALUES ('宝莱纳餐厅', '健力士黑啤', 39); INSERT INTO Sells (bar, beer, price) VALUES ('苏荷酒吧', '百威', 32); INSERT INTO Sells (bar, beer, price) VALUES ('苏荷酒吧', '纯种苦啤酒', 33); INSERT INTO Sells (bar, beer, price) VALUES ('苏荷酒吧', '喜力', 28); INSERT INTO Sells (bar, beer, price) VALUES ('苏荷酒吧', '南非啤酒', 36); INSERT INTO Sells (bar, beer, price) VALUES ('苏荷酒吧', '朝日', 26); INSERT INTO Sells (bar, beer, price) VALUES ('苏荷酒吧', '科罗娜', 40); INSERT INTO Sells (bar, beer, price) VALUES ('苏荷酒吧', '健力士黑啤', 39); INSERT INTO Sells (bar, beer, price) VALUES ('苏荷酒吧', '生力', 38); INSERT INTO Sells (bar, beer, price) VALUES ('扬州老啤酒厂酒吧', '百威', 32); INSERT INTO Sells (bar, beer, price) VALUES ('扬州老啤酒厂酒吧', '纯种苦啤酒', 33); INSERT INTO Sells (bar, beer, price) VALUES ('扬州老啤酒厂酒吧', '喜力', 35); INSERT INTO Sells (bar, beer, price) VALUES ('扬州老啤酒厂酒吧', 'Stella Artois', 36); INSERT INTO Sells (bar, beer, price) VALUES ('扬州老啤酒厂酒吧', '健力士黑啤', 26); INSERT INTO Sells (bar, beer, price) VALUES ('扬州老啤酒厂酒吧', '科罗娜', 40); INSERT INTO Sells (bar, beer, price) VALUES ('扬州老啤酒厂酒吧', '米勒', 50); INSERT INTO Sells (bar, beer, price) VALUES ('扬州老啤酒厂酒吧', '安贝夫', 40); INSERT INTO Sells (bar, beer, price) VALUES ('扬州老啤酒厂酒吧', '麒麟', 39); INSERT INTO Sells (bar, beer, price) VALUES ('扬州老啤酒厂酒吧', '贝克', 42);
首先清空某个数据表, 比如
TRUNCATE TABLE Bars;
将各个表的内容分别存储在文本文件中, 为方便, 下载 load_data.zip, 解压缩后有六个文件(它们是: Beers.txt, Bars.txt, Sells.txt, Drinkers.txt, Frequents.txt, Likes.txt) 其中文件中的每一行代表一个记录, 次序应与表中属性的次序一致. 注意: 这些文件的编码已经是
将它们保存在
mysql> LOAD DATA LOCAL INFILE 'D:/tmp/Bars.txt' -> INTO TABLE Bars -> CHARACTER SET GBK -> FIELDS TERMINATED BY ',' -> OPTIONALLY ENCLOSED BY '"' -> ESCAPED BY '\\' -> LINES STARTING BY '[' TERMINATED BY ']' -> ;
现在检查一下导入有没有成功, 其余类似.
如下面使用默认分隔符.
mysql> LOAD DATA LOCAL INFILE 'D:/tmp/Beers.txt' -> INTO TABLE Beers;
注意: 如果是在 Windows 中编辑的文本, 则要加上
mysql> LOAD DATA LOCAL INFILE 'D:/tmp/Beers.txt' -> INTO TABLE Beers -> LINES TERMINATED BY '\r\n';
这里提供了其他数据, 如
mysql> INSERT INTO Drinkers (name, addr, phone) -> VALUES ('Mike', '上海市徐汇区', '15912345678');
mysql> INSERT INTO Likes (drinker, beer) -> VALUES ('Mike', '贝克');
mysql> INSERT INTO Frequents (drinker, bar) -> VALUES ('Mike', 'Westside');
如: 对于关系
mysql> SELECT beer -> FROM Likes, Frequents -> WHERE Frequents.drinker=Likes.drinker -> AND Frequents.bar='Westside';
mysql> describe Beers; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | name | varchar(20) | YES | UNI | NULL | | | manf | varchar(50) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
其中各字段的含义分别为:
或者使用
mysql> desc Beers;
mysql> desc Beers\G; *************************** 1. row *************************** Field: name Type: varchar(20) Null: YES Key: UNI Default: NULL Extra: *************************** 2. row *************************** Field: manf Type: varchar(50) Null: YES Key: Default: NULL Extra: 2 rows in set (0.01 sec)
使用
mysql> show create table Beers\G; *************************** 1. row *************************** Table: Beers Create Table: CREATE TABLE `Beers` ( `name` varchar(20) DEFAULT NULL, `manf` varchar(50) DEFAULT NULL, UNIQUE KEY `name` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec)
mysql> ALTER TABLE Beers RENAME TO Beer;
mysql> SHOW TABLES; +--------------------+ | Tables_in_test_bar | +--------------------+ | Bars | | Beer | | Drinkers | | Frequents | | Likes | | Sells | +--------------------+
mysql> ALTER TABLE Beer RENAME Beers;
mysql> alter table Beers modify name char(20); Query OK, 14 rows affected (0.66 sec) Records: 14 Duplicates: 0 Warnings: 0
mysql> desc Beers; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | name | char(20) | YES | UNI | NULL | | | manf | varchar(50) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+
或者
mysql> alter table Beers change name beer char(20); Query OK, 14 rows affected (0.23 sec) Records: 14 Duplicates: 0 Warnings: 0
如果仅仅需要更改属性名, 数据类型也要加上, 不能为空. 即跟原来一致即可.
mysql> alter table Beers change beer name; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
添加无完整性约束条件的字段.
mysql> alter table Beers add Country varchar(20); Query OK, 14 rows affected (0.23 sec) Records: 14 Duplicates: 0 Warnings: 0
mysql> alter table Beers add name_en varchar(20) after name; Query OK, 14 rows affected (0.24 sec) Records: 14 Duplicates: 0 Warnings: 0
mysql> desc Beers; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | name | varchar(20) | YES | UNI | NULL | | | name_en | varchar(20) | YES | | NULL | | | manf | varchar(50) | YES | | NULL | | | Country | varchar(20) | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 4 rows in set (0.01 sec)
添加有完整性约束条件的字段.
mysql> alter table Beers add column1 varchar(12) not null; Query OK, 14 rows affected (0.30 sec) Records: 14 Duplicates: 0 Warnings: 0
mysql> desc Beers; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | name | varchar(20) | YES | UNI | NULL | | | name_en | varchar(20) | YES | | NULL | | | manf | varchar(50) | YES | | NULL | | | Country | varchar(20) | YES | | NULL | | | column1 | varchar(12) | NO | | NULL | | +---------+-------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)
注意不能将
删除刚才新加入的
mysql> alter table Beers drop column1; Query OK, 14 rows affected (0.56 sec) Records: 14 Duplicates: 0 Warnings: 0
mysql> desc Beers; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | name | varchar(20) | YES | UNI | NULL | | | name_en | varchar(20) | YES | | NULL | | | manf | varchar(50) | YES | | NULL | | | Country | varchar(20) | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
mysql> alter table Beers modify name_en varchar(20) after manf; Query OK, 14 rows affected (0.25 sec) Records: 14 Duplicates: 0 Warnings: 0
mysql> desc Beers; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | name | varchar(20) | YES | UNI | NULL | | | manf | varchar(50) | YES | | NULL | | | name_en | varchar(20) | YES | | NULL | | | Country | varchar(20) | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
mysql> show engines; +--------------------+---------+------------------------------------------------------------+--------------+------+------------+ | Engine | Support | Comment | Transactions | XA | Savepoints | +--------------------+---------+------------------------------------------------------------+--------------+------+------------+ | MyISAM | YES | MyISAM storage engine | NO | NO | NO | | InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES | | MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO | | PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO | | CSV | YES | CSV storage engine | NO | NO | NO | | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO | +--------------------+---------+------------------------------------------------------------+--------------+------+------------+
mysql> show engines\G; *************************** 1. row *************************** Engine: MyISAM Support: YES Comment: MyISAM storage engine Transactions: NO XA: NO Savepoints: NO *************************** 2. row *************************** Engine: InnoDB Support: DEFAULT Comment: Supports transactions, row-level locking, and foreign keys Transactions: YES XA: YES Savepoints: YES *************************** 3. row *************************** Engine: MRG_MYISAM Support: YES Comment: Collection of identical MyISAM tables Transactions: NO XA: NO Savepoints: NO *************************** 4. row *************************** Engine: PERFORMANCE_SCHEMA Support: YES Comment: Performance Schema Transactions: NO XA: NO Savepoints: NO *************************** 5. row *************************** Engine: CSV Support: YES Comment: CSV storage engine Transactions: NO XA: NO Savepoints: NO *************************** 6. row *************************** Engine: MEMORY Support: YES Comment: Hash based, stored in memory, useful for temporary tables Transactions: NO XA: NO Savepoints: NO 6 rows in set (0.00 sec)
mysql> show create table Beers\G; *************************** 1. row *************************** Table: Beers Create Table: CREATE TABLE `Beers` ( `name` varchar(20) DEFAULT NULL, `manf` varchar(50) DEFAULT NULL, `name_en` varchar(20) DEFAULT NULL, `Country` varchar(20) DEFAULT NULL, UNIQUE KEY `name` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec)
mysql> alter table Beers engine=myisam; Query OK, 14 rows affected (0.19 sec) Records: 14 Duplicates: 0 Warnings: 0
mysql> show create table Beers\G; *************************** 1. row *************************** Table: Beers Create Table: CREATE TABLE `Beers` ( `name` varchar(20) DEFAULT NULL, `manf` varchar(50) DEFAULT NULL, `name_en` varchar(20) DEFAULT NULL, `Country` varchar(20) DEFAULT NULL, UNIQUE KEY `name` (`name`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 1 row in set (0.00 sec)
改回
mysql> alter table Beers engine=innodb;
接下来, 按照讲义中的有关操作进行实验.