引用主键
使用主键引用其他表
逻辑删除(isDelete)
字段类型改成布尔型
展示所有数据库
show databases;
查看当前数据库
select database();
字段类型:
数字 int
decimal(5,2)浮点型,一共包含5位数,小数包含2位,如110.21
字符串 char() 存储固定字符 如:电话号码(固定长度)
varchar() 存储可变字符 如:姓名(不固定)
text 存储超长字符
日期 datatime
布尔 bit(8)8个二进制位
注意: gb-2312 中国大陆编码格式
utf-8 国际编码格式(使用)
约束
primary key 限制唯一标识符,查找速度非常快(物理上的存储结构是按主键来维护的,一个表只要一个主键)
not null 要求非空
unique 要求非重复
default 1 默认值为1
foreign key 外建
表的操作
创建表
标识符 类型 自动增长(auto_increment) 主键(primary key) 非空
mysql> create table student(
-> id int auto_increment primary key not null,
-> name varchar(10) not null,
-> gender bit default 1,
-> birthday datetime);
修改表
增加表的字段(add|change|drop)
mysql> alter table student add isDelete bit default 0;
查看表的创建语句
show create table student ;
查看表的结构
desc 表名;
更改表名称
rename table 原表名 to 新表名;
全列插入
insert into student values(0,"谢武",0,0,"1994-12-04");
缺省插入(指定插入)
inset into student(name) values("黄蓉")
inset into student(gender,name) values(0,"小龙女")
同时插入多条数据
insert into student(name) values("张三"),("李四"),("王五");
mysql> select * from student;
+----+--------+--------+----------+---------------------+
| id | name | gender | isDelete | birthday |
+----+--------+--------+----------+---------------------+
| 1 | 王六 | | | NULL |
| 2 | 王柳 | | | 1994-12-04 00:00:00 |
| 3 | 张三 | | | NULL |
| 4 | 李四 | | | NULL |
| 5 | 王五 | | | NULL |
+----+--------+--------+----------+---------------------+
update student set birthday="1992-05-15" where id=2;
物理删除
mysql> delete from student where id=5;
mysql> select * from student;
+----+--------+--------+----------+---------------------+
| id | name | gender | isDelete | birthday |
+----+--------+--------+----------+---------------------+
| 1 | 王六 | | | NULL |
| 2 | 王柳 | | | 1992-05-15 00:00:00 |
| 3 | 张三 | | | 1992-05-15 00:00:00 |
| 4 | 李四 | | | NULL |
+----+--------+--------+----------+---------------------+
逻辑删除,本质就是修改操作update
alter table students add isdelete bit default 0;
如果需要删除则
update students isdelete=1 where ...;
查询
0:select * from student;
1:选择查询
mysql> select id,name,birthday from student where id=2;
+----+--------+---------------------+
| id | name | birthday |
+----+--------+---------------------+
| 2 | 王六 | 1992-05-15 00:00:00 |
+----+--------+---------------------+
2:范围查询
mysql> select * from student;
+----+-----------+--------+----------+---------------------+
| id | name | gender | isDelete | birthday |
+----+-----------+--------+----------+---------------------+
| 1 | 王柳 | | | NULL |
| 2 | 王六 | | | 1992-05-15 00:00:00 |
| 3 | 张三 | | | 1992-05-15 00:00:00 |
| 4 | 李四 | | | NULL |
| 6 | 胡红梅 | | | 1994-10-12 00:00:00 |
+----+-----------+--------+----------+---------------------+
5 rows in set (0.00 sec)
mysql> select * from student where id in (1,3,6);
+----+-----------+--------+----------+---------------------+
| id | name | gender | isDelete | birthday |
+----+-----------+--------+----------+---------------------+
| 1 | 谢武 | | | NULL |
| 3 | 张三 | | | 1992-05-15 00:00:00 |
| 6 | 胡红梅 | | | 1994-10-12 00:00:00 |
+----+-----------+--------+----------+---------------------+
3 rows in set (0.00 sec)
mysql> select * from student where id between 3 and 6;
+----+-----------+--------+----------+---------------------+
| id | name | gender | isDelete | birthday |
+----+-----------+--------+----------+---------------------+
| 3 | 张三 | | | 1992-05-15 00:00:00 |
| 4 | 李四 | | | NULL |
| 6 | 胡红梅 | | | 1994-10-12 00:00:00 |
3:is not null
mysql> select * from student;
+----+-----------+--------+----------+---------------------+
| id | name | gender | isDelete | birthday |
+----+-----------+--------+----------+---------------------+
| 1 | 谢武 | | | NULL |
| 2 | 谢武 | | | 1992-05-15 00:00:00 |
| 3 | 张三 | | | 1992-05-15 00:00:00 |
| 4 | 李四 | | | NULL |
| 6 | 胡红梅 | | | 1994-10-12 00:00:00 |
| 7 | 张宇 | | | 1984-12-08 00:00:00 |
+----+-----------+--------+----------+---------------------+
mysql> select * from student where birthday is not null;
+----+-----------+--------+----------+---------------------+
| id | name | gender | isDelete | birthday |
+----+-----------+--------+----------+---------------------+
| 2 | 谢武 | | | 1992-05-15 00:00:00 |
| 3 | 张三 | | | 1992-05-15 00:00:00 |
| 6 | 胡红梅 | | | 1994-10-12 00:00:00 |
| 7 | 张宇 | | | 1984-12-08 00:00:00 |
+----+-----------+--------+----------+---------------------+
3:模糊查询
mysql> select * from student where name like '%红%';
+----+-----------+--------+----------+---------------------+
| id | name | gender | isDelete | birthday |
+----+-----------+--------+----------+---------------------+
| 6 | 胡红梅 | | | 1994-10-12 00:00:00 |
+----+-----------+--------+----------+---------------------+
1 row in set (0.00 sec)
mysql> select * from student where name like '胡_';
Empty set (0.00 sec)
mysql> select * from student where name like '胡__';
+----+-----------+--------+----------+---------------------+
| id | name | gender | isDelete | birthday |
+----+-----------+--------+----------+---------------------+
| 6 | 胡红梅 | | | 1994-10-12 00:00:00 |
+----+-----------+--------+----------+---------------------+
1 row in set (0.00 sec)
mysql> select * from student where name like '张_';
+----+--------+--------+----------+---------------------+
| id | name | gender | isDelete | birthday |
+----+--------+--------+----------+---------------------+
| 3 | 张三 | | | 1992-05-15 00:00:00 |
+----+--------+--------+----------+---------------------+
1 row in set (0.00 sec)
mysql> select * from student where name like '张%';
+----+--------+--------+----------+---------------------+
| id | name | gender | isDelete | birthday |
+----+--------+--------+----------+---------------------+
| 3 | 张三 | | | 1992-05-15 00:00:00
使用主键引用其他表
逻辑删除(isDelete)
字段类型改成布尔型
展示所有数据库
show databases;
查看当前数据库
select database();
字段类型:
数字 int
decimal(5,2)浮点型,一共包含5位数,小数包含2位,如110.21
字符串 char() 存储固定字符 如:电话号码(固定长度)
varchar() 存储可变字符 如:姓名(不固定)
text 存储超长字符
日期 datatime
布尔 bit(8)8个二进制位
注意: gb-2312 中国大陆编码格式
utf-8 国际编码格式(使用)
约束
primary key 限制唯一标识符,查找速度非常快(物理上的存储结构是按主键来维护的,一个表只要一个主键)
not null 要求非空
unique 要求非重复
default 1 默认值为1
foreign key 外建
表的操作
创建表
标识符 类型 自动增长(auto_increment) 主键(primary key) 非空
mysql> create table student(
-> id int auto_increment primary key not null,
-> name varchar(10) not null,
-> gender bit default 1,
-> birthday datetime);
修改表
增加表的字段(add|change|drop)
mysql> alter table student add isDelete bit default 0;
查看表的创建语句
show create table student ;
查看表的结构
desc 表名;
更改表名称
rename table 原表名 to 新表名;
全列插入
insert into student values(0,"谢武",0,0,"1994-12-04");
缺省插入(指定插入)
inset into student(name) values("黄蓉")
inset into student(gender,name) values(0,"小龙女")
同时插入多条数据
insert into student(name) values("张三"),("李四"),("王五");
mysql> select * from student;
+----+--------+--------+----------+---------------------+
| id | name | gender | isDelete | birthday |
+----+--------+--------+----------+---------------------+
| 1 | 王六 | | | NULL |
| 2 | 王柳 | | | 1994-12-04 00:00:00 |
| 3 | 张三 | | | NULL |
| 4 | 李四 | | | NULL |
| 5 | 王五 | | | NULL |
+----+--------+--------+----------+---------------------+
update student set birthday="1992-05-15" where id=2;
物理删除
mysql> delete from student where id=5;
mysql> select * from student;
+----+--------+--------+----------+---------------------+
| id | name | gender | isDelete | birthday |
+----+--------+--------+----------+---------------------+
| 1 | 王六 | | | NULL |
| 2 | 王柳 | | | 1992-05-15 00:00:00 |
| 3 | 张三 | | | 1992-05-15 00:00:00 |
| 4 | 李四 | | | NULL |
+----+--------+--------+----------+---------------------+
逻辑删除,本质就是修改操作update
alter table students add isdelete bit default 0;
如果需要删除则
update students isdelete=1 where ...;
查询
0:select * from student;
1:选择查询
mysql> select id,name,birthday from student where id=2;
+----+--------+---------------------+
| id | name | birthday |
+----+--------+---------------------+
| 2 | 王六 | 1992-05-15 00:00:00 |
+----+--------+---------------------+
2:范围查询
mysql> select * from student;
+----+-----------+--------+----------+---------------------+
| id | name | gender | isDelete | birthday |
+----+-----------+--------+----------+---------------------+
| 1 | 王柳 | | | NULL |
| 2 | 王六 | | | 1992-05-15 00:00:00 |
| 3 | 张三 | | | 1992-05-15 00:00:00 |
| 4 | 李四 | | | NULL |
| 6 | 胡红梅 | | | 1994-10-12 00:00:00 |
+----+-----------+--------+----------+---------------------+
5 rows in set (0.00 sec)
mysql> select * from student where id in (1,3,6);
+----+-----------+--------+----------+---------------------+
| id | name | gender | isDelete | birthday |
+----+-----------+--------+----------+---------------------+
| 1 | 谢武 | | | NULL |
| 3 | 张三 | | | 1992-05-15 00:00:00 |
| 6 | 胡红梅 | | | 1994-10-12 00:00:00 |
+----+-----------+--------+----------+---------------------+
3 rows in set (0.00 sec)
mysql> select * from student where id between 3 and 6;
+----+-----------+--------+----------+---------------------+
| id | name | gender | isDelete | birthday |
+----+-----------+--------+----------+---------------------+
| 3 | 张三 | | | 1992-05-15 00:00:00 |
| 4 | 李四 | | | NULL |
| 6 | 胡红梅 | | | 1994-10-12 00:00:00 |
3:is not null
mysql> select * from student;
+----+-----------+--------+----------+---------------------+
| id | name | gender | isDelete | birthday |
+----+-----------+--------+----------+---------------------+
| 1 | 谢武 | | | NULL |
| 2 | 谢武 | | | 1992-05-15 00:00:00 |
| 3 | 张三 | | | 1992-05-15 00:00:00 |
| 4 | 李四 | | | NULL |
| 6 | 胡红梅 | | | 1994-10-12 00:00:00 |
| 7 | 张宇 | | | 1984-12-08 00:00:00 |
+----+-----------+--------+----------+---------------------+
mysql> select * from student where birthday is not null;
+----+-----------+--------+----------+---------------------+
| id | name | gender | isDelete | birthday |
+----+-----------+--------+----------+---------------------+
| 2 | 谢武 | | | 1992-05-15 00:00:00 |
| 3 | 张三 | | | 1992-05-15 00:00:00 |
| 6 | 胡红梅 | | | 1994-10-12 00:00:00 |
| 7 | 张宇 | | | 1984-12-08 00:00:00 |
+----+-----------+--------+----------+---------------------+
3:模糊查询
mysql> select * from student where name like '%红%';
+----+-----------+--------+----------+---------------------+
| id | name | gender | isDelete | birthday |
+----+-----------+--------+----------+---------------------+
| 6 | 胡红梅 | | | 1994-10-12 00:00:00 |
+----+-----------+--------+----------+---------------------+
1 row in set (0.00 sec)
mysql> select * from student where name like '胡_';
Empty set (0.00 sec)
mysql> select * from student where name like '胡__';
+----+-----------+--------+----------+---------------------+
| id | name | gender | isDelete | birthday |
+----+-----------+--------+----------+---------------------+
| 6 | 胡红梅 | | | 1994-10-12 00:00:00 |
+----+-----------+--------+----------+---------------------+
1 row in set (0.00 sec)
mysql> select * from student where name like '张_';
+----+--------+--------+----------+---------------------+
| id | name | gender | isDelete | birthday |
+----+--------+--------+----------+---------------------+
| 3 | 张三 | | | 1992-05-15 00:00:00 |
+----+--------+--------+----------+---------------------+
1 row in set (0.00 sec)
mysql> select * from student where name like '张%';
+----+--------+--------+----------+---------------------+
| id | name | gender | isDelete | birthday |
+----+--------+--------+----------+---------------------+
| 3 | 张三 | | | 1992-05-15 00:00:00