day43
操作数据表
增
create table 表名(
字段名1 列类型[可选参数], # 记住加逗号
字段名2 列类型[可选参数],
字段名3 列类型[可选参数] # 最后一行不加逗号
)charset=utf8;
列约束:(************)
auto_increment : 自增 1
primary key : 主键索引,加快查询速度, 列的值不能重复
NOT NULL 标识该字段不能为空
DEFAULT 为该字段设置默认值
列类型:(**************)
数字
- 整型
tinyint
smallint
int
mediumint
bigint
a.整数类型
b.取值范围
c.unsigned 加上代表不能取负数,只适用于整型
- 浮点型
float:不一定精确
decimal:非常的精确的数字
- 字符串
- char(长度):定长
- varchar(长度):变长
区别:
char:定长,无论插入的字符是多少个,永远固定占规定的长度
场景:
1.身份证
2.手机号 char(11)
3.md5加密之后的值,比如密码等
varchar:变长,根据插入的字符串的长度来计算所占的字节数,
注意:如果不能确定插入的数据的大小,一半建议使用varchar()
- 时间日期类型
YEAR
YYYY(1901/2155)
DATE
YYYY-MM-DD(1000-01-01/9999-12-31)
TIME
HH:MM:SS('-838:59:59'/'838:59:59')
DATETIME(*********)
YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00)
TIMESTAMP
YYYYMMDD HHMMSS(1970-01-01 00:00:00)
- 枚举
列出所有的选项
create table t1(
id int auto_increment primary key,
gender enum('male','female')
)charset utf8;
删
drop table 表名
改
1.修改表名
alter table 表名 rename 新表名;
2.增加字段
alter table 表名
add 字段名 数据类型 [完整性约束条件...],
add 字段名 数据类型 [完整性约束条件...];
alter table 表名
add 字段名 数据类型 [完整性约束条件...] first;
alter table表名
add 字段名 数据类型 [完整性约束条件...] after 字段名;
3.删除字段
alter table 表名 drop 字段名;
4.修改字段
alter table 表名 modify 字段名 数据类型 [完整性约束条件...];
alter table 表名 change 旧字段名 新字段名 新数据类型 [完整性约束条件...];
查
describe 表名; #查看表结构,可简写为desc 表名
show create table 表名\G; # 查看表详细结构
复制表
create table 新表 select * from 旧表; # 复制表结构+记录
create table 新表 like 旧表; # 复制表结构
操作记录
查
语法:
select 列1, 列2 from 表名; (*代表查询所有的列)
select * from 表名; (*代表查询所有的列)
select * from t66 where id>30 and id<40;
select * from t66 where id>30;
select * from t66 where id<30;
select * from t66 where id<=30;
select * from t66 where id>=30;
select * from t66 where id!=30;
select * from t66 where id<>30;
mysql> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 1 | zekai |
+------+-------+
1 row in set (0.00 sec)
between..and...: 取值范围是闭区间
select * from t66 where id between 30 and 40;
mysql> select * from t66 where id between 31 and 33;
+----+--------+
| id | name |
+----+--------+
| 31 | dsadsa |
| 32 | dsadsa |
| 33 | dsadsa |
+----+--------+
避免重复DISTINCT
mysql> select distinct name from t66;
+--------+
| name |
+--------+
| xxxx |
| hds |
| dsadsa |
+--------+
3 rows in set (0.00 sec)
通过四则运算查询 (不要用)
mysql> select name, age*10 from t3;
+------+--------+
| name | age*10 |
+------+--------+
| xxx | 100 |
+------+--------+
1 row in set (0.01 sec)
mysql> select name, age*10 as age from t3;
+------+-----+
| name | age |
+------+-----+
| xxx | 100 |
+------+-----+
1 row in set (0.02 sec)
in(80,90,100):
mysql> select * from t66 where id in (23,34,11);
+----+------+
| id | name |
+----+------+
| 11 | xxxx |
| 23 | hds |
+----+------+
2 rows in set (0.04 sec)
like : 模糊查询
以x开头:
mysql> select * from t66 where name like 'x%';
+----+------+
| id | name |
+----+------+
| 1 | xxxx |
| 2 | xxxx |
| 3 | xxxx |
| 4 | xxxx |
| 8 | xxxx |
| 9 | xxxx |
| 10 | xxxx |
| 11 | xxxx |
| 15 | xxxx |
| 16 | xxxx |
| 17 | xxxx |
| 18 | xxxx |
| 30 | xxxx |
+----+------+
13 rows in set (0.05 sec)
以x结尾:
mysql> select * from t66 where name like '%x';
+----+------+
| id | name |
+----+------+
| 1 | xxxx |
| 2 | xxxx |
| 3 | xxxx |
| 4 | xxxx |
| 8 | xxxx |
| 9 | xxxx |
| 10 | xxxx |
| 11 | xxxx |
| 15 | xxxx |
| 16 | xxxx |
| 17 | xxxx |
| 18 | xxxx |
| 30 | xxxx |
+----+------+
13 rows in set (0.00 sec)
包含x的:
mysql> select * from t66 where name like '%x%';
不让用
插入数据insert
1. 插入完整数据(顺序插入)
语法一:
INSERT INTO 表名(字段1,字段2,字段3…字段n) VALUES(值1,值2,值3…值n);
语法二:
INSERT INTO 表名 VALUES (值1,值2,值3…值n);
2. 指定字段插入数据
语法:
INSERT INTO 表名(字段1,字段2,字段3…) VALUES (值1,值2,值3…);
3. 插入多条记录
语法:
INSERT INTO 表名 VALUES
(值1,值2,值3…值n),
(值1,值2,值3…值n),
(值1,值2,值3…值n);
4. 插入查询结果
语法:
INSERT INTO 表名(字段1,字段2,字段3…字段n)
SELECT (字段1,字段2,字段3…字段n) FROM 表2
WHERE …;
更新数据update
语法:
UPDATE 表名 SET
字段1=值1,
字段2=值2,
WHERE CONDITION;
示例:
UPDATE mysql.user SET password=password(‘123’)
where user=’root’ and host=’localhost’;
删除数据delete
语法:
DELETE FROM 表名
WHERE CONITION;
示例:
DELETE FROM mysql.user
WHERE password=’’;
练习:
更新MySQL root用户密码为mysql123
删除除从本地登录的root用户以外的所有用户