mysql -u root -p Enter password:输入密码展示数据库
show databases;创建数据库
create database database1 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
(数据库名)
进入数据库
use database1;
(数据库名)
创建数据表
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 --默认值为3
)default charset=utf8;
create table tb1(
id int primary key, --主键(不允许为空,不允许重复)
name varchar(16),
age int
)default charset=utf8;
主键一般用于表示当前的数据编号(类似于人的身份证)
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;
展示数据表
show tables;
desc tb1(表名称);删除表
drop table 表名称;
常见数据类型:
- tinyint
有符号,取值范围:-128 ~ 127 (有正有负)【默认】 无符号,取值范围:0 ~ 255 (只有正)
create table tb2(
id int not null auto_increment primary key,
age tinyint --有符号
)default charset=utf8;
create table tb3(
id int not null auto_increment primary key,
age tinyint unsigned --无符号
)default charset=utf8;
- int
int 表示有符号,取值范围:-2147483648 ~ 2147483647 int unsigned 表示无符号,取值范围:0 ~ 4294967295
- bigint
有符号,取值范围:-9223372036854775808 ~ 9223372036854775807 无符号,取值范围:0 ~ 1844674407309551615
练习题:
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,48);
#查看数据
select * from tb2;
#效果:
mysql> select * from tb2;
+----+--------+------+
| id | salary | age |
+----+--------+------+
| 1 | 10000 | 18 |
| 2 | 20000 | 28 |
| 3 | 30000 | 38 |
| 4 | 40000 | 48 |
+----+--------+------+
4 rows in set (0.00 sec)
-
float
-
double
-
decimal
#准确的小数值,m是数字的总个数(负号不算),d是小数点后的个数。m最大值为65,d的最大值为30。
create table tb3(
id bigint not null auto_increment primary key,
salary decimal(8,2) #decimal(m,d)
)default charset=utf8;
insert into tb3(salary) values(1.28);
insert into tb3(salary) values(1231.285); #会四舍五入
insert into tb3(salary) values(12343.282);
insert into tb3(salary) values(1234567.28); #会报错(超过8位) ERROR 1264 (22003): Out of range value for column 'salary' at row 1
#结果:
mysql> select * from tb3;
+----+----------+
| id | salary |
+----+----------+
| 1 | 1.28 |
| 2 | 1231.29 |
| 3 | 12343.28 |
+----+----------+
3 rows in set (0.01 sec)
-
char(m),速度快
定长字符串, m代表字符串长度,最多可容纳255个字符。 char(11),固定用11个字符串进行存储,即使没有11个字符,也按照11个字符进行存储。 create table tb4( id bigint not null auto_increment primary key, mobile char(11) )default charset=utf8; insert into tb3(mobile) values("123"); insert into tb3(mobile) values("123123456789"); #超出范围报错 -
varchar(m),节省空间
变长字符串。m代表字符长度。 最大65535字节 varchar(11),真实数据有多长就按照多长存储。 create table tb5( id bigint not null auto_increment primary key, mobile varchar(11) )default charset=utf8; insert into tb3(mobile) values("123"); insert into tb3(mobile) values("123123456789"); #超出范围报错 -
text
text数据类型用于保存大字符串,可以最多用到65535(2**16-1)个字符。 一般情况下,长文本会用text类型。例如:文章、新闻等。 create table tb6( id bigint not null auto_increment primary key, title varchar(128), content text )default charset=utf8; -
mediumtext
最多用到(2**24-1)个字符。
-
longtext
最多用到(2**32-1)个字符。
-
datetime(年月日 时分秒)
YYYY-MM-DD HH:MM:SS (2022-06-25 18:02:15)
-
date
YYYY-MM-DD (2022-06-25)
create table tb7(
id bigint not null auto_increment primary key,
name varchar(64),
password varchar(64),
email varchar(32),
age tinyint(5),
salary decimal(10,2),
ctime datetime
)default charset=utf8;
insert into tb7(name,password,email,age,salary,ctime) values("ordeal","123456","www.123456@163.com","18","11111.11","2022-6-25 18:18:18");
#结果:
mysql> select*from tb7;
+----+--------+----------+--------------------+------+----------+---------------------+
| id | name | password | email | age | salary | ctime |
+----+--------+----------+--------------------+------+----------+---------------------+
| 1 | ordeal | 123456 | www.123456@163.com | 18 | 11111.11 | 2022-06-25 18:18:18 |
+----+--------+----------+--------------------+------+----------+---------------------+
1 row in set (0.01 sec)
其他数据类型,官方文档:
https://dev.mysql.com/doc/refman/8.0/en/data-types.html



