MySQL数据表的管理(文件)
1、进入数据库(进入文件夹)
- use 数据库名字;
use gx_day14;
2、查看当前数据库下的所有 表(文件)
show tables;
3、创建表(文件文件)
格式:
create table 表名称(
列名称 类型,
列名称 类型,
列名称 类型
)default charset=utf8;
create table tb1(
id int,
name varchar(16),
age int
) default charset=utf8;
每行后面还可以加更多的信息:
create table tb1(
id int,
name varchar(16) not null, -- 不允许为空
age int null, -- 允许为空(默认)
) default charset=utf8;
create table tb1(
id int,
name varchar(16),
age int default 3 -- 插入数据时,age列的值默认3
) default charset=utf8;
create table tb1(
id int primary key, -- 主键(不允许为空,不允许重复)
name varchar(16),
age int
) default charset=utf8;
主键一般用于表示当前行的数据的编号(类似于人的身份证)。
自增:id这列,从1开始,依次自动加一,系统自动添加,id这一列我们不用管,自动传数据。
create table tb1(
id int auto_increment primary key, -- 内部维护,自增
name varchar(16),
age int
) default charset=utf8;
一般情况下,我们再创建表时都会这样来写:【标准】
create table tb1(
id int not null auto_increment primary key,//非空,自增,主键
name varchar(16),
age int
) default charset=utf8;
4、查看表属性 desc 表名;
mysql> desc tb1;
5、删除表
drop table 表名称;
6、常用数据类型:
6.1、整型:tinyint,int,bigint
- tinyint
有符号,取值范围:-128 ~ 127 (有正有负)【默认】
无符号,取值范围:0 ~ 255(只有正)
create table tb2(
id int not null auto_increment primary key,
age tinyint -- 有符号:取值范围:-128 ~ 127
) default charset=utf8;
create table tb3(
id int not null auto_increment primary key,
age tinyint unsigned -- 无符号:取值范围:0 ~ 255
) default charset=utf8;
-
int
int 表示有符号,取值范围:-2147483648 ~ 2147483647
int unsigned 表示无符号,取值范围:0 ~ 4294967295 -
bigint
有符号,取值范围:-9223372036854775808 ~ 9223372036854775807
无符号,取值范围:0 ~ 18446744073709551615
6.1.1、小练习:
创建表
create table tb2(
id bigint not null auto_increment primary key,
salary int,
age tinyint
) default charset=utf8;
插入数据
insert into tb2(salary,age) values(10000,18);
insert into tb2(salary,age) values(20000,28); //单行插入
insert into tb2(salary,age) values(30000,38),(40000,40); //批量插入
查看表中的数据
select * from tb2;
mysql> show tables;
+--------------------+
| Tables_in_gx_day14 |
+--------------------+
| tb1 |
+--------------------+
1 row in set (0.00 sec)
mysql> create table tb2(
-> id bigint not null auto_increment primary key,
-> salary int,
-> age tinyint
-> ) default charset=utf8;
Query OK, 0 rows affected (0.03 sec)
mysql> show tables;
+--------------------+
| Tables_in_gx_day14 |
+--------------------+
| tb1 |
| tb2 |
+--------------------+
2 rows in set (0.00 sec)
mysql> insert into tb2(salary,age) values(10000,18);
Query OK, 1 row affected (0.00 sec)
mysql> insert into tb2(salary,age) values(20000,28);
Query OK, 1 row affected (0.00 sec)
mysql> insert into tb2(salary,age) values(30000,38),(40000,40);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from tb2;
+----+--------+------+
| id | salary | age |
+----+--------+------+
| 1 | 10000 | 18 |
| 2 | 20000 | 28 |
| 3 | 30000 | 38 |
| 4 | 40000 | 40 |
+----+--------+------+
4 rows in set (0.00 sec)
6.2、小数:float double
decimal:准确的小数值,decimal(m,d)
m是数字总个数(负号不算),d是小数点后个数。
m最大值为65,d最大值为30。
create table tb3(
id int not null primary key auto_increment,
salary decimal(8,2) //decimal(m,d)
)default charset=utf8;
insert into tb3(salary) values(1.28);
insert into tb3(salary) values(5.289);
insert into tb3(salary) values(5.282);
insert into tb3(salary) values(122115.11);
select * from tb3;
6.3、字符串:放文本,日期等
6.3.1、char(m),速度快。
定长字符串,m代表字符串的长度,最多可容纳255个字符。
char(11),固定用11个字符串进行存储,哪怕真是没有11个字符,也会按照11存储。
create table tb4(
id int not null primary key auto_increment,
mobile char(11) //手机号 固定11位
)default charset=utf8;
insert into tb4(mobile) values("151");
insert into tb4(mobile) values("15131255555");
6.3.2、varchar(m),节省空间。动态变化
变长字符串,m代表字符的长度。 最大65535字节/3 = 最大的m (一个中文占三个字节)
varchar(11),真实数据有多少长久按照多长存储。
create table tb5(
id int not null primary key auto_increment,
name varchar(11) //用户名 长度不固定
)default charset=utf8;
insert into tb5(name) values("151");
insert into tb5(name) values("15131255555");
6.3.3、text
text数据类型用于保存 变长的大字符串,可以最多到65535 (2**16 − 1)个字符。
一般情况下,长文本会用text类型。例如:文章、新闻等。
create table tb6(
id int not null primary key auto_increment,
title varchar(128),
content text
)default charset=utf8;
6.3.4、不常用:
- mediumtext
A TEXT column with a maximum length of 16,777,215 (2**24 − 1) characters.
- longtext
A TEXT column with a maximum length of 4,294,967,295 or 4GB (2**32 − 1)
6.3.5、时间字符串
- datetime
YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59)
- date
YYYY-MM-DD(1000-01-01/9999-12-31)
6.4、练习题:用户表
create table tb7(
id int not null primary key auto_increment,
name varchar(64) not null,
password char(64) not null,
email varchar(64) not null,
age tinyint,
salary decimal(10,2),
ctime datetime
)default charset=utf8;
insert into tb7(name,password,email,age,salary,ctime) values("张三","123","xx@live.com",19,1000.20,"2011-11-11 11:11:10");
insert into tb7(name,password,email,age,salary,ctime) values("张电摩","123","xx@live.com",19,1000.20,"2011-11-11 11:11:10");
insert into tb7(name,password,email,age,salary,ctime) values("庞小青","123","xx@live.com",19,1000.20,"2011-11-11 11:11:10");
insert into tb7(name,password,email,age,salary,ctime) values("谢涛","123","xx@live.com",19,1000.20,"2011-11-11 11:11:10");
insert into tb7(name,password,email,age,salary,ctime) values("谢鹏","123","xx@live.com",19,1000.20,"2011-11-11 11:11:10");
select * from tb7;
+----+-----------+----------+-------------+------+---------+---------------------+
| id | name | password | email | age | salary | ctime |
+----+-----------+----------+-------------+------+---------+---------------------+
| 1 | 张三 | 123 | xx@live.com | 19 | 10000.20 | 2011-11-11 11:11:10 |
+----+-----------+----------+-------------+------+---------+---------------------+
1 row in set (0.00 sec)
MySQL还有很多其他的数据类型,例如:set、enum、TinyBlob、Blob、MediumBlob、LongBlob 等,详细见官方文档:https://dev.mysql.com/doc/refman/5.7/en/data-types.html
开发系统时,
- 创建数据库
- 创建表结构
都需要通过上述命令创建。