数据表的基本操作:
能对表中的数据记录进行增加、删除、修改、查询操作:
1、数据表的创建:
基本语法:
bash
create table 表名 (字段1,字段2,...)
create table 表名 (字段1 数据类型(字符长度),字段2,...)
create table 表名 (字段1 数据类型(字符长度) 约束条件,字段2,...)
字段名称、约束条件什么的,都要提前规划好。
案例一:创建一个admin管理员表,拥有3个字段(编号、用户名称、用户密码)。
bash
use db_db3;
use在MySQL中的含义代表选择,use数据库名称相当于选择指定的数据库,**而且use比较特殊,其选择结束后,其尾部可以不加分号;**但是强烈建议所有的SQL语句都要加分号,养成一个好习惯。
bash
create table tb_admin(
id tinyint,
name varchar(20),
password char(32)
);
tinyint: 微整形;
char:固定长度的字段;
varchar:代表变化长度的字段;
bash
mysql> desc tb_admin;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | tinyint(4) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| password | char(32) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
案例二:创建一个article文章表,拥有4个字段(编号、标题、作者、内容)。
bash
create table tb_article (
id int,
title varchar(50),
author varchar(20),
content text
);
text: 一般情况下,varchar存储不了的字符串信息,都建议使用text文本进行处理。
varchar存储的最大长度,理论是65525个字符。但是实际上,有几个长度是用于存放内容长度的,所以真正可以使用的不足53335个字符,另外varchar类型存储的字符长度还和编码格式有关,1个 GBK格式的占用2个字节长度,1个UTF8格式的字符占用3个字节长度 GBK=65532~65533/2。UTF8=65532/3。超过这些范围,正常都使用text。
很多项目,超过255个,都使用text数据类型。
为数据库里的每个字段定义数据类型,可以大幅减少数据库里由于输入错误而产生的错误数据。字段定义是一种数据校验方式,控制了每个字段里可以输入的数据。
查询已创建的数据表:
bash
mysql> show tables;
+------------------+
| Tables_in_db_db3 |
+------------------+
| tb_admin |
| tb_article |
+------------------+
2 rows in set (0.00 sec)
显示所有数据表;
显示数据表的创建过程:(编码格式和字段信息):
bash
mysql> show create table tb_admin;
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tb_admin | CREATE TABLE `tb_admin` (
`id` tinyint(4) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`password` char(32) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
bash
mysql> show create table tb_article;
+------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tb_article | CREATE TABLE `tb_article` (
`id` int(11) DEFAULT NULL,
`title` varchar(50) DEFAULT NULL,
`author` varchar(20) DEFAULT NULL,
`content` text
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
不指定数据库的格式,默认是拉丁。数据库引擎InnoDB。
bash
mysql> desc tb_admin;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | tinyint(4) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| password | char(32) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
bash
mysql> create table tb_article (
-> id int,
-> title varchar(50),
-> author varchar(20),
-> content text
-> );
Query OK, 0 rows affected (0.00 sec)
那我们创建表的时候可以这样:
bash
create table tb_article (
id int,
title varchar(50),
author varchar(20),
content text
)engine=innodb default charset=utf8;
在创建表的时候指定数据引擎innodb和编码格式utf8。
我们可以重新再创建下tb_article表。
bash
mysql> create table tb_article (
-> id int,
-> title varchar(50),
-> author varchar(20),
-> content text
-> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.01 sec)
bash
mysql> show create table tb_article;
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tb_article | CREATE TABLE `tb_article` (
`id` int(11) DEFAULT NULL,
`title` varchar(50) DEFAULT NULL,
`author` varchar(20) DEFAULT NULL,
`content` text
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
可以看到默认的编码格式是utf8的。
bash
mysql> drop table tb_admin;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> create table tb_admin(
-> id tinyint,
-> name varchar(20),
-> password char(32)
-> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> show create table tb_admin;
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tb_admin | CREATE TABLE `tb_admin` (
`id` tinyint(4) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`password` char(32) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
更改表信息:
1)数据表字段的添加;
案例:在tb_article文章表中添加一个addtime字段,类型为date(年-月-日)。
bash
mysql> alter table tb_article add addtime date after content;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
mysql> desc tb_article;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| title | varchar(50) | YES | | NULL | |
| author | varchar(20) | YES | | NULL | |
| content | text | YES | | NULL | |
| addtime | date | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
first:把新添加的字段放在第一位;
after 字段名称:把新添加的字段放在指定字段的后面。
2)修改字段的名称或者字段类型:
修改字段名称与字段类型:
bash
mysql> alter table tb_admin change name username varchar(30);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
mysql> desc tb_admin;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | tinyint(4) | YES | | NULL | |
| username | varchar(30) | YES | | NULL | |
| password | char(32) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
修改字段类型:
bash
mysql> alter table tb_admin modify username varchar(20);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tb_admin;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | tinyint(4) | YES | | NULL | |
| username | varchar(20) | YES | | NULL | |
| password | char(32) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
修改字段名称用change,修改字段类型用modify。
3)删除某个字段:
bash
mysql> alter table tb_article drop addtime;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tb_article;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| title | varchar(50) | YES | | NULL | |
| author | varchar(20) | YES | | NULL | |
| content | text | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
4)修改数据表引MyISAM和InnoDB):
擅长数据的查询、InnoDB擅长事务的处理。
bash
mysql> alter table tb_article engine=myisam;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table tb_article;
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tb_article | CREATE TABLE `tb_article` (
`id` int(11) DEFAULT NULL,
`title` varchar(50) DEFAULT NULL,
`author` varchar(20) DEFAULT NULL,
`content` text
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
改字段都是可以通过desc进行查看。这个我们使用的show create table进行查看。
5)修改数据表的编码格式:
bash
mysql> show create table tb_admin;
+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tb_admin | CREATE TABLE `tb_admin` (
`id` tinyint(4) DEFAULT NULL,
`username` varchar(20) CHARACTER SET utf8 DEFAULT NULL,
`password` char(32) CHARACTER SET utf8 DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
移动表到另一个库里并重命名
rename table db01.t1 to db02.t11;
或者
alter table db01.t1 rename db02.t11;
只重命名表名不移动
rename table tt1 to tt2;
或者
alter table tt1 rename tt2;