MySQL学习笔记7

数据表的基本操作:

能对表中的数据记录进行增加、删除、修改、查询操作:

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;

相关推荐
瓯雅爱分享1 分钟前
Java+Vue构建的采购招投标一体化管理系统,集成招标计划、投标审核、在线竞价、中标公示及合同跟踪功能,附完整源码,助力企业实现采购全流程自动化与规范化
java·mysql·vue·软件工程·源代码管理
咋吃都不胖lyh3 小时前
SQL-多对多关系
android·mysql·数据分析
哲Zheᗜe༘5 小时前
了解学习MySQL数据库基础
数据库·学习·mysql
咋吃都不胖lyh5 小时前
MySQL 多表查询中,联合查询(UNION) 和子查询
mysql·数据分析
先鱼鲨生6 小时前
【MySQL】认识数据库以及MySQL安装
数据库·mysql
周杰伦_Jay7 小时前
【终端使用MySQL】MySQL 数据库核心操作全解析:从入门到性能优化
数据库·mysql·性能优化
-雷阵雨-9 小时前
MySQL——数据库入门指南
数据库·mysql
就叫飞六吧9 小时前
DataX适合全量同步和简单的增量场景
mysql
xhbh66611 小时前
【实战避坑】MySQL自增主键(AUTO_INCREMENT)全解:从锁机制、间隙问题到分库分表替代方案
android·数据库·mysql·mysql自增主键
程序员三明治15 小时前
【MyBatis从入门到入土】告别JDBC原始时代:零基础MyBatis极速上手指南
数据库·mysql·mybatis·jdbc·数据持久化·数据