上节课复习:
1、数据库相关概念
记录
=》表中一行内容
字段
=》表格的标题
表
=》文件
库
=》文件夹
数据库管理软件
=》mysql
数据库服务器
=》运行有数据库管理软件服务端的计算机
2、基本管理
3、sql语句
库
create database db1 charset utf8mb4
;创建库
show databases
;查看库
+--------------------+
| Database
|
+--------------------+
| information_schema
|
| db1
|
| mysql
|
| performance_schema
|
| test
|
+--------------------+
show create database db1
;查看某个库
+----------+----------------------------------------------------------------+
| Database
| Create Database
|
+----------+----------------------------------------------------------------+
| db1
| CREATE DATABASE `db1`
/*!
40100 DEFAULT CHARACTER SET latin1
*/ |
+----------+----------------------------------------------------------------+
select database
();
+------------+
| database
() |
+------------+
| NULL
|
+------------+
alter database db1 charset gbk
;修改库编码
drop database db1
;删除库
表
use db1选择库
create table t1
(id int,name char
);创建表
show tables
;查看库中的表
show create table t1
;
+-------+-----------------------------------------------------------------------------------------------------------------------+
| Table
| Create Table
|
+-------+-----------------------------------------------------------------------------------------------------------------------+
| t2
| CREATE TABLE `t2`
(
`
id`
int(11) DEFAULT NULL
,
`name` char
(1) DEFAULT NULL
) ENGINE
=InnoDB DEFAULT CHARSET
=latin1
|
+-------+-----------------------------------------------------------------------------------------------------------------------+
desc t1
;查看库
+-------+---------+------+-----+---------+-------+
| Field
| Type
| Null
| Key
| Default
| Extra
|
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES
| | NULL
| |
| name
| char
(1) | YES
| | NULL
| |
+-------+---------+------+-----+---------+-------+
alter table t1 rename t2
;改变表名
alter table t1 modify
id tinyint
;改变表中表头输入数据格式
drop table t1
;删除表
记录
insert t1
(id,name
) values 插入表中的数据
(1,"aaa"),
(2,"bbb"),
(3,"ccc");
select
id from t1 where
id>1;查看表中的数据
update t1
set name
="AAA" where
id>=2;更新表中的数据
delete
from t1 where
id=1;清空表中某个字段的数据
truncate t1
;清空表中数据
存储引擎
innodb存储引擎负责处理innodb类型的表
今日内容:表详细操作
1、数据类型
2、约束条件
foreign key
多对一
多对多
一对一
1.修改表
create table t1
(id int,name char
);创建表(括号内创建字段名和类型)
alter table t1 rename tt1
;修改表名
alter table t1 modify
id tinyint
;修改表某个字段数据类型
alter table t1 change
id ID tinyint
;修改表某个字段名字和数据类型
alter table t1 change
id ID tinyint
,change name NAME char
(4);修改表多个字段名字和数据类型
alter table t1 add gender char
(4);增加表某个字段名称和属性
alter table t1 add gender char
(4) first
;增加表某个字段名称和属性,位置
alter table t1 add level
int after ID
;增加表某个字段名称和属性,位置
alter table t1 drop gender
;删除表中某个字段的值
create table t2 select user
,host
,password
from mysql
.user
;复制硬盘中某个表中数据到新创建表中
create table t3 select user
,host
,password
from mysql
.user where
1!=1;复制硬盘中某个表结构到新表中
2.表字段的类型
强调:整型的宽度是显示宽度,无需设置,存储宽度是固定死的!!!!
mysql
> create table t5
(id tinyint
)
mysql
> desc t4
;下面括号中的
4是显示宽度
+-------+------------+------+-----+---------+-------+
| Field
| Type
| Null
| Key
| Default
| Extra
|
+-------+------------+------+-----+---------+-------+
| id | tinyint
(4) | YES
| | NULL
| |
+-------+------------+------+-----+---------+-------+
1 row
in set (0.01 sec
)
mysql
> insert t4 values
(128);
ERROR
1264 (22003): Out of
range value
for column
'id' at row
1
mysql
>
mysql
>
mysql
> insert t4 values
(127);
Query OK
, 1 row affected
(0.05 sec
)
mysql
> select
* from t4
;
+------+
| id |
+------+
| 127 |
+------+
1 row
in set (0.00 sec
)
mysql
>
create table t7
(x
float(255,30),y double
(255,30),z decimal
(65,30));第一个数字是整数显示宽度,第二个数字为小数显示宽度
insert t7 values
(1.111111111111111111111111111111,1.111111111111111111111111111111,1.111111111111111111111111111111);
year(
1901/2155)
time 时
:分
:秒 (
'-838:59:59'/'838:59:59')
date 年
:月
:日 (
1000-01-01/9999-12-31)
datetime 年
:月
:日 时
:分
:秒
1000-01-01 00:00:00/9999-12-31 23:59:59
timestamp 年
:月
:日 时
:分
:秒
1970-01-01 00:00:00/2037
create table t8
(y year
,t time
,d date
,dt datetime
,ts timestamp
);
insert t8 values
(now
(),now
(),now
(),now
(),now
());
create table student
(
id int,
name char
(10),
born_year year
,
bitrh date
,
reg_time datetime
);
insert student values
(1,"wangjing","1911","1911-11-11","1911-11-11 11:11:11"),
(2,"lxx","1988","1988-11-11","1988-11-11 11:11:11");
insert student values
(3,"wangjing","1911","19111111","19111111111111");
create table t9
(
id int,
name varchar
(16),
-- update_time datetime
not null default now
() on update now
(),
update_time timestamp
,
reg_time datetime
not null default now
()
);
insert into t9
(id,name
) values
(1,"egon");
mysql
> select
* from t9
;
+------+------+---------------------+---------------------+
| id | name
| update_time
| reg_time
|
+------+------+---------------------+---------------------+
| 1 | egon
| 2020-09-01 16:45:51 | 2020-09-01 16:45:51 |
+------+------+---------------------+---------------------+
1 row
in set (0.00 sec
)
mysql
> update t9
set name
="EGON" where
id=1;
Query OK
, 1 row affected
(0.06 sec
)
Rows matched
: 1 Changed
: 1 Warnings
: 0
mysql
> select
* from t9
;
+------+------+---------------------+---------------------+
| id | name
| update_time
| reg_time
|
+------+------+---------------------+---------------------+
| 1 | EGON
| 2020-09-01 16:46:50 | 2020-09-01 16:45:51 |
+------+------+---------------------+---------------------+
1 row
in set (0.00 sec
)
mysql
>
char 定长,不够则补全空格
看起来特点:
浪费空间
读取速度快
varchar 变长,预留
1-2bytes来存储真实数据的长度
看起来特点:
节省空间
读取速度慢
ps:在存储的数据量刚好达到存储宽度限制时,其实varchar更费空间
总结:大多数情况下存储的数据量都达不到宽度限制,所以大多数情况下varchar更省空间
但省空间不是关键,关键是省空间 会带来io效率的提升,进而提升了查询效率
ab
|abc
|abcd
|
1bytes
+ab
|1bytes
+abc
|1bytes
+abcd
|
===============验证
create table t11
(x char
(5));
create table t12
(x varchar
(5));
insert t11 values
("我擦嘞 "); -- "我擦嘞 "
insert t12 values
("我擦嘞 "); -- "我擦嘞 "
t11
=>字符个数
5 字节个数
11
t12
=>字符个数
4 字节个数
10
set sql_mode
="pad_char_to_full_length";
select char_length
(x
) from t11
;
select char_length
(x
) from t12
;
select length
(x
) from t11
;
select length
(x
) from t12
;
枚举类型enum
("a","b","c","d") 多选
1
集合类型
set("a","b","c","d") 多选
CREATE TABLE shirts
(
name VARCHAR
(40),
size ENUM
('x-small', 'small', 'medium', 'large', 'x-large')
);
INSERT INTO shirts
(name
, size
) VALUES
('dress shirt','large'), ('t-shirt','medium'),('polo shirt','small');
CREATE TABLE user
(
name VARCHAR
(16),
hobbies
set("read","chou","drink","tang")
);
insert user values
("lxx","tang,chou");
insert user values
("hxx","tangchou");