MariaDB数据库----查(实例演示)
MariaDB数据--查
SQl语句执行顺序
基础查询
查询
- 添加数据
MariaDB [test]> insert into huluwa values
-> (1 ,'葫芦爷爷',73,1.75,'boy','yes'),
-> (2,'大娃',7,1.72,'boy','yes'),
-> (3,'二娃',6,1.63,'girl','yes');
Query OK, 3 rows affected (0.026 sec)
Records: 3 Duplicates: 0 Warnings: 0
MariaDB [test]> insert into huluwa(id,name,age,lost) values
-> (4,'三娃',5,'no'),
-> (5,'四娃',4,'no');
Query OK, 2 rows affected (0.004 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [test]> insert into huluwa values
-> (6,'五娃',3,1.38,'boy','no'),
-> (7,'六娃',2,1.82,'boy','no'),
-> (8,'七娃',3,1.83,'girl','no');
Query OK, 3 rows affected (0.385 sec)
Records: 3 Duplicates: 0 Warnings: 0
- 查询所有数据; 耗内存 (葫芦娃数据)
MariaDB [test1]> -- select * from 表名
MariaDB [test1]> select * from huluwa;
+----+--------------+------+------+--------+------+
| id | name | age | high | gender | lost |
+----+--------------+------+------+--------+------+
| 1 | 葫芦爷爷 | 73 | 1.75 | boy | yes |
| 2 | 大娃 | 7 | 1.72 | boy | yes |
| 3 | 二娃 | 6 | 1.63 | girl | yes |
| 4 | 三娃 | 5 | NULL | unknow | no |
| 5 | 四娃 | 4 | NULL | unknow | no |
| 6 | 五娃 | 3 | 1.38 | boy | no |
| 7 | 六娃 | 2 | 1.82 | boy | no |
| 8 | 七娃 | 3 | 1.83 | girl | no |
+----+--------------+------+------+--------+------+
- 一定条件查询(查看丢了的葫芦娃)
MariaDB [test1]> select * from huluwa where lost='yes';
+----+--------------+------+------+--------+------+
| id | name | age | high | gender | lost |
+----+--------------+------+------+--------+------+
| 1 | 葫芦爷爷 | 73 | 1.75 | boy | yes |
| 2 | 大娃 | 7 | 1.72 | boy | yes |
| 3 | 二娃 | 6 | 1.63 | girl | yes |
+----+--------------+------+------+--------+------+
- 给字段起别名--as
MariaDB [test1]> select gender as '性别',lost as '丢了没?',name from huluwa;
+--------+--------------+--------------+
| 性别 | 丢了没? | name |
+--------+--------------+--------------+
| boy | yes | 葫芦爷爷 |
| boy | yes | 大娃 |
| girl | yes | 二娃 |
| unknow | no | 三娃 |
| unknow | no | 四娃 |
| boy | no | 五娃 |
| boy | no | 六娃 |
| girl | no | 七娃 |
+--------+--------------+--------------+
- 通过表名字查询
MariaDB [test1]> select huluwa.name from huluwa;
+--------------+
| name |
+--------------+
| 葫芦爷爷 |
| 大娃 |
| 二娃 |
| 三娃 |
| 四娃 |
| 五娃 |
| 六娃 |
| 七娃 |
+--------------+
- 消除重复行 distinct
MariaDB [test1]> select distinct age from huluwa;
+------+
| age |
+------+
| 73 |
| 7 |
| 6 |
| 5 |
| 4 |
| 3 |
| 2 |
+------+
- 条件查询---between
MariaDB [test1]> select * from huluwa where age between 3 and 6;
+----+--------+------+------+--------+------+
| id | name | age | high | gender | lost |
+----+--------+------+------+--------+------+
| 3 | 二娃 | 6 | 1.63 | girl | yes |
| 4 | 三娃 | 5 | NULL | unknow | no |
| 5 | 四娃 | 4 | NULL | unknow | no |
| 6 | 五娃 | 3 | 1.38 | boy | no |
| 8 | 七娃 | 3 | 1.83 | girl | no |
+----+--------+------+------+--------+------+
- 条件查询---not : 取反
MariaDB [test1]> select * from huluwa where age not between 3 and 6;
+----+--------------+------+------+--------+------+
| id | name | age | high | gender | lost |
+----+--------------+------+------+--------+------+
| 1 | 葫芦爷爷 | 73 | 1.75 | boy | yes |
| 2 | 大娃 | 7 | 1.72 | boy | yes |
| 7 | 六娃 | 2 | 1.82 | boy | no |
+----+--------------+------+------+--------+------+
MariaDB [test1]> select * from huluwa where not (age > 3 and age < 6);
+----+--------------+------+------+--------+------+
| id | name | age | high | gender | lost |
+----+--------------+------+------+--------+------+
| 1 | 葫芦爷爷 | 73 | 1.75 | boy | yes |
| 2 | 大娃 | 7 | 1.72 | boy | yes |
| 3 | 二娃 | 6 | 1.63 | girl | yes |
| 6 | 五娃 | 3 | 1.38 | boy | no |
| 7 | 六娃 | 2 | 1.82 | boy | no |
| 8 | 七娃 | 3 | 1.83 | girl | no |
+----+--------------+------+------+--------+------+
- 条件查询---and, or
MariaDB [test1]> select name,gender,lost as '丢了没?' from huluwa where age > 3 and lost='no';
+--------+--------+--------------+
| name | gender | 丢了没? |
+--------+--------+--------------+
| 三娃 | unknow | no |
| 四娃 | unknow | no |
+--------+--------+--------------+
MariaDB [test1]> select name,gender,lost as '丢了没?' from huluwa where age > 3 or lost='no';
+--------------+--------+--------------+
| name | gender | 丢了没? |
+--------------+--------+--------------+
| 葫芦爷爷 | boy | yes |
| 大娃 | boy | yes |
| 二娃 | girl | yes |
| 三娃 | unknow | no |
| 四娃 | unknow | no |
| 五娃 | boy | no |
| 六娃 | boy | no |
| 七娃 | girl | no |
+--------------+--------+--------------+
模糊查询
like
% 代替一个或多个字符
_ 下划线代替一个字符
MariaDB [test1]> select * from huluwa where name like '%大%';
+----+--------+------+------+--------+------+
| id | name | age | high | gender | lost |
+----+--------+------+------+--------+------+
| 2 | 大娃 | 7 | 1.72 | boy | yes |
+----+--------+------+------+--------+------+
MariaDB [test1]> select * from huluwa where name like '____';
+----+--------------+------+------+--------+------+
| id | name | age | high | gender | lost |
+----+--------------+------+------+--------+------+
| 1 | 葫芦爷爷 | 73 | 1.75 | boy | yes |
+----+--------------+------+------+--------+------+
- 判断是否为空 --- is null
MariaDB [test1]> select * from huluwa where high is null;
+----+--------+------+------+--------+------+
| id | name | age | high | gender | lost |
+----+--------+------+------+--------+------+
| 4 | 三娃 | 5 | NULL | unknow | no |
| 5 | 四娃 | 4 | NULL | unknow | no |
+----+--------+------+------+--------+------+
- 判断是否为空 --- is not null
MariaDB [test1]> select * from huluwa where high is not null;
+----+--------------+------+------+--------+------+
| id | name | age | high | gender | lost |
+----+--------------+------+------+--------+------+
| 1 | 葫芦爷爷 | 73 | 1.75 | boy | yes |
| 2 | 大娃 | 7 | 1.72 | boy | yes |
| 3 | 二娃 | 6 | 1.63 | girl | yes |
| 6 | 五娃 | 3 | 1.38 | boy | no |
| 7 | 六娃 | 2 | 1.82 | boy | no |
| 8 | 七娃 | 3 | 1.83 | girl | no |
+----+--------------+------+------+--------+------+
排序
-- order by 字段
-- asc 升序
-- desc 降序
- 身高降序排序
MariaDB [test1]> select * from huluwa order by high desc;
+----+--------------+------+------+--------+------+
| id | name | age | high | gender | lost |
+----+--------------+------+------+--------+------+
| 8 | 七娃 | 3 | 1.83 | girl | no |
| 7 | 六娃 | 2 | 1.82 | boy | no |
| 1 | 葫芦爷爷 | 73 | 1.75 | boy | yes |
| 2 | 大娃 | 7 | 1.72 | boy | yes |
| 3 | 二娃 | 6 | 1.63 | girl | yes |
| 6 | 五娃 | 3 | 1.38 | boy | no |
| 4 | 三娃 | 5 | NULL | unknow | no |
| 5 | 四娃 | 4 | NULL | unknow | no |
+----+--------------+------+------+--------+------+
- 男孩身高排序
MariaDB [test1]> select * from huluwa where gender='boy' order by high ;
+----+--------------+------+------+--------+------+
| id | name | age | high | gender | lost |
+----+--------------+------+------+--------+------+
| 6 | 五娃 | 3 | 1.38 | boy | no |
| 2 | 大娃 | 7 | 1.72 | boy | yes |
| 1 | 葫芦爷爷 | 73 | 1.75 | boy | yes |
| 7 | 六娃 | 2 | 1.82 | boy | no |
+----+--------------+------+------+--------+------+
- 按年龄降序,年龄相同按身高降序;
MariaDB [test1]> select * from huluwa order by age desc ,high desc ;
+----+--------------+------+------+--------+------+
| id | name | age | high | gender | lost |
+----+--------------+------+------+--------+------+
| 1 | 葫芦爷爷 | 73 | 1.75 | boy | yes |
| 2 | 大娃 | 7 | 1.72 | boy | yes |
| 3 | 二娃 | 6 | 1.63 | girl | yes |
| 4 | 三娃 | 5 | NULL | unknow | no |
| 5 | 四娃 | 4 | NULL | unknow | no |
| 8 | 七娃 | 3 | 1.83 | girl | no |
| 6 | 五娃 | 3 | 1.38 | boy | no |
| 7 | 六娃 | 2 | 1.82 | boy | no |
+----+--------------+------+------+--------+------+
- 数字类型的列可以进行四则运算,无法转换为整数的以0计算
select *,age*1.5 from huluwa ;
- 也支持ifnull语句
select *,age+ifnull(high,1) from huluwa ;
聚合函数
-- 总数
-- count
-- 查询男孩多少
MariaDB [test1]> select count(*) as '男娃总人数' from huluwa where gender='boy';
+-----------------+
| 男娃总人数 |
+-----------------+
| 4 |
+-----------------+
-- 最大值
-- max
-- 最高身高
MariaDB [test1]> select max(high) as '最高男孩' from huluwa where gender='boy';
+--------------+
| 最高男孩 |
+--------------+
| 1.82 |
+--------------+
-- 求和 sum
-- 计算身高总和
MariaDB [test1]> select sum(high) from huluwa;
+-----------+
| sum(high) |
+-----------+
| 10.13 |
+-----------+
-- 平均值 avg
-- 计算平均身高 (round 保留两位小数)
MariaDB [test1]> select round(avg(high),2) from huluwa;
+--------------------+
| round(avg(high),2) |
+--------------------+
| 1.69 |
+--------------------+
分组: group by
- 按性别分组
MariaDB [test1]> select gender from huluwa group by gender;
+--------+
| gender |
+--------+
| boy |
| girl |
| unknow |
+--------+
- 每组性别人数
MariaDB [test1]> select gender ,count(*) as '人数' from huluwa group by gender;
+--------+--------+
| gender | 人数 |
+--------+--------+
| boy | 4 |
| girl | 2 |
| unknow | 2 |
+--------+--------+
- 组人数超过三人的组的组员人名(group_concat , having)
MariaDB [test1]> select gender,group_concat(name) from huluwa group by gender having count(*) > 3;
+--------+-----------------------------------+
| gender | group_concat(name) |
+--------+-----------------------------------+
| boy | 葫芦爷爷,大娃,五娃,六娃 |
+--------+-----------------------------------+
分段查询
示例1:查询第四行后面的两行(即:第五行和第六行)信息
MariaDB [test1]> select * from huluwa limit 4,2;
+----+--------+------+------+--------+------+
| id | name | age | high | gender | lost |
+----+--------+------+------+--------+------+
| 5 | 四娃 | 4 | NULL | unknow | no |
| 6 | 五娃 | 3 | 1.38 | boy | no |
+----+--------+------+------+--------+------+
示例2:
MariaDB [test1]> select * from huluwa where gender=1 limit 2,2;
+----+--------+------+------+--------+------+
| id | name | age | high | gender | lost |
+----+--------+------+------+--------+------+
| 6 | 五娃 | 3 | 1.38 | boy | no |
| 7 | 六娃 | 2 | 1.82 | boy | no |
+----+--------+------+------+--------+------+
MariaDB [test1]> select * from huluwa where gender=1;
+----+--------------+------+------+--------+------+
| id | name | age | high | gender | lost |
+----+--------------+------+------+--------+------+
| 1 | 葫芦爷爷 | 73 | 1.75 | boy | yes |
| 2 | 大娃 | 7 | 1.72 | boy | yes |
| 6 | 五娃 | 3 | 1.38 | boy | no |
| 7 | 六娃 | 2 | 1.82 | boy | no |
+----+--------------+------+------+--------+------+
4 rows in set (0.000 sec)
连接查询
- 自关联查询
MariaDB [test1]> select * from heros;
+-----+--------------+------+
| aid | name | pid |
+-----+--------------+------+
| 1 | 动画片 | NULL |
| 2 | 纪录片 | NULL |
| 3 | 武侠片 | NULL |
| 4 | 葫芦娃 | 1 |
| 5 | 小虎还乡 | 1 |
| 6 | 天龙八部 | 3 |
+-----+--------------+------+
- 自关联查询--内关联:inner join ... on
MariaDB [test1]> select * from heros as movie inner join heros as human on movie.aid=human.pid;
+-----+-----------+------+-----+--------------+------+
| aid | name | pid | aid | name | pid |
+-----+-----------+------+-----+--------------+------+
| 1 | 动画片 | NULL | 4 | 葫芦娃 | 1 |
| 1 | 动画片 | NULL | 5 | 小虎还乡 | 1 |
| 3 | 武侠片 | NULL | 6 | 天龙八部 | 3 |
+-----+-----------+------+-----+--------------+------+
- 自关联+按需筛选---having
注意:指定是哪个别名表的字段
MariaDB [test1]> select * from heros as movie inner join heros as human on movie.aid=human.pid having movie.name='动画片';
+-----+-----------+------+-----+--------------+------+
| aid | name | pid | aid | name | pid |
+-----+-----------+------+-----+--------------+------+
| 1 | 动画片 | NULL | 4 | 葫芦娃 | 1 |
| 1 | 动画片 | NULL | 5 | 小虎还乡 | 1 |
+-----+-----------+------+-----+--------------+------+
葫芦爷爷开了个学校教葫芦娃知识;
## 创建一个学校表(创建表)
MariaDB [test1]> create table school(id int unsigned auto_increment primary key,name varchar(20),cla_id int unsigned);
Query OK, 0 rows affected (0.009 sec)
## 开了三个班(插入表数据)
MariaDB [test1]> insert into school values(0,'葫芦一班',1),(0,'葫芦二班',2),(0,'葫芦三班',3);
Query OK, 3 rows affected (0.002 sec)
Records: 3 Duplicates: 0 Warnings: 0
## 查询下班级信息
MariaDB [test1]> select * from school;
+----+--------------+--------+
| id | name | cla_id |
+----+--------------+--------+
| 1 | 葫芦一班 | 1 |
| 2 | 葫芦二班 | 2 |
| 3 | 葫芦三班 | 3 |
+----+--------------+--------+
## 查询葫芦娃信息
MariaDB [test1]> select * from huluwa;
+----+--------------+------+------+--------+
| id | name | age | high | gender |
+----+--------------+------+------+--------+
| 1 | 葫芦爷爷 | 73 | 1.75 | boy |
| 2 | 大娃 | 7 | 1.72 | boy |
| 3 | 二娃 | 6 | 1.63 | girl |
| 4 | 三娃 | 5 | NULL | unknow |
| 5 | 四娃 | 4 | NULL | unknow |
| 6 | 五娃 | 3 | 1.38 | boy |
| 7 | 六娃 | 2 | 1.82 | boy |
| 8 | 七娃 | 3 | 1.83 | girl |
+----+--------------+------+------+--------+
## 给葫芦哇添加班级列(修改表结构--添加列)
MariaDB [test1]> alter table huluwa add cla_id int;
Query OK, 0 rows affected (0.003 sec)
Records: 0 Duplicates: 0 Warnings: 0
## 给葫芦娃分配班级(更新表数据)
MariaDB [test1]> update huluwa set cla_id=1 where name='大娃';
Query OK, 1 row affected (0.002 sec)
Rows matched: 1 Changed: 1 Warnings: 0
...
...
##查看
MariaDB [test1]> select * from huluwa;
+----+--------------+------+------+--------+--------+
| id | name | age | high | gender | cla_id |
+----+--------------+------+------+--------+--------+
| 1 | 葫芦爷爷 | 73 | 1.75 | boy | NULL |
| 2 | 大娃 | 7 | 1.72 | boy | 1 |
| 3 | 二娃 | 6 | 1.63 | girl | 2 |
| 4 | 三娃 | 5 | NULL | unknow | 3 |
| 5 | 四娃 | 4 | NULL | unknow | 4 |
| 6 | 五娃 | 3 | 1.38 | boy | 1 |
| 7 | 六娃 | 2 | 1.82 | boy | 2 |
| 8 | 七娃 | 3 | 1.83 | girl | 3 |
+----+--------------+------+------+--------+--------+
- 两表关联查询(内关联)
MariaDB [test1]> select * from huluwa inner join school on school.cla_id=huluwa.cla_id;
+----+--------+------+------+--------+--------+----+--------------+--------+
| id | name | age | high | gender | cla_id | id | name | cla_id |
+----+--------+------+------+--------+--------+----+--------------+--------+
| 2 | 大娃 | 7 | 1.72 | boy | 1 | 1 | 葫芦一班 | 1 |
| 3 | 二娃 | 6 | 1.63 | girl | 2 | 2 | 葫芦二班 | 2 |
| 4 | 三娃 | 5 | NULL | unknow | 3 | 3 | 葫芦三班 | 3 |
| 6 | 五娃 | 3 | 1.38 | boy | 1 | 1 | 葫芦一班 | 1 |
| 7 | 六娃 | 2 | 1.82 | boy | 2 | 2 | 葫芦二班 | 2 |
| 8 | 七娃 | 3 | 1.83 | girl | 3 | 3 | 葫芦三班 | 3 |
+----+--------+------+------+--------+--------+----+--------------+--------+
- 两表关联查询(左关联)
MariaDB [test1]> select * from huluwa left join school on school.cla_id=huluwa.cla_id;
+----+--------------+------+------+--------+--------+------+--------------+--------+
| id | name | age | high | gender | cla_id | id | name | cla_id |
+----+--------------+------+------+--------+--------+------+--------------+--------+
| 2 | 大娃 | 7 | 1.72 | boy | 1 | 1 | 葫芦一班 | 1 |
| 6 | 五娃 | 3 | 1.38 | boy | 1 | 1 | 葫芦一班 | 1 |
| 3 | 二娃 | 6 | 1.63 | girl | 2 | 2 | 葫芦二班 | 2 |
| 7 | 六娃 | 2 | 1.82 | boy | 2 | 2 | 葫芦二班 | 2 |
| 4 | 三娃 | 5 | NULL | unknow | 3 | 3 | 葫芦三班 | 3 |
| 8 | 七娃 | 3 | 1.83 | girl | 3 | 3 | 葫芦三班 | 3 |
| 1 | 葫芦爷爷 | 73 | 1.75 | boy | NULL | NULL | NULL | NULL |
| 5 | 四娃 | 4 | NULL | unknow | 4 | NULL | NULL | NULL |
+----+--------------+------+------+--------+--------+------+--------------+--------+
- 左关联和右关联的区别
left join (左联接) 返回包括左表中的所有记录和右表中关联字段相等的记录
right join (右联接) 返回包括右表中的所有记录和左表中关联字段相等的记录
inner join (内关联) 只返回两个表中关联字段相等的行
多表关联
select * from
(select t_area.name, t_account.money from t_account left join t_area on t_account.areaid=t_area.id where month=1) 1y
left join
(select t_area.name, t_account.money from t_account left join t_area on t_account.areaid=t_area.id where month=2) 2y
on 1y.name=2y.name
left join
(select t_area.name, t_account.money from t_account left join t_area on t_account.areaid=t_area.id where month=3) 3y
on 1y.name=3y.name
left join
(select t_area.name, t_account.money from t_account left join t_area on t_account.areaid=t_area.id where month=4) 4y
on 1y.name=4y.name
left join
(select t_area.name, t_account.money from t_account left join t_area on t_account.areaid=t_area.id where month=5) 5y
on 1y.name=5y.name
left join
(select t_area.name, t_account.money from t_account left join t_area on t_account.areaid=t_area.id where month=6) 6y
on 1y.name=6y.name
left join
(select t_area.name, t_account.money from t_account left join t_area on t_account.areaid=t_area.id where month=7) 7y
on 1y.name=7y.name
left join
(select t_area.name, t_account.money from t_account left join t_area on t_account.areaid=t_area.id where month=8) 8y
on 1y.name=8y.name
left join
(select t_area.name, t_account.money from t_account left join t_area on t_account.areaid=t_area.id where month=9) 9y
on 1y.name=9y.name
left join
(select t_area.name, t_account.money from t_account left join t_area on t_account.areaid=t_area.id where month=10) 10y
on 1y.name=10y.name
left join
(select t_area.name, t_account.money from t_account left join t_area on t_account.areaid=t_area.id where month=11) 11y
on 1y.name=11y.name
right join
(select t_area.name, t_account.money from t_account left join t_area on t_account.areaid=t_area.id where month=12) 12y
on 1y.name=12y.name
;
- 按需查询(需求列之间加 , )
MariaDB [test1]> select huluwa.name as '姓名', school.name as '班级' from huluwa left join school on school.cla_id=huluwa.cla_id;
+--------------+--------------+
| 姓名 | 班级 |
+--------------+--------------+
| 大娃 | 葫芦一班 |
| 五娃 | 葫芦一班 |
| 二娃 | 葫芦二班 |
| 六娃 | 葫芦二班 |
| 三娃 | 葫芦三班 |
| 七娃 | 葫芦三班 |
| 葫芦爷爷 | NULL |
| 四娃 | NULL |
+--------------+--------------+
- 查询并按葫芦娃年龄排序:order by
MariaDB [test1]> select * from huluwa left join school on school.cla_id=huluwa.cla_id order by huluwa.age;
+----+--------------+------+------+--------+--------+------+--------------+--------+
| id | name | age | high | gender | cla_id | id | name | cla_id |
+----+--------------+------+------+--------+--------+------+--------------+--------+
| 7 | 六娃 | 2 | 1.82 | boy | 2 | 2 | 葫芦二班 | 2 |
| 8 | 七娃 | 3 | 1.83 | girl | 3 | 3 | 葫芦三班 | 3 |
| 6 | 五娃 | 3 | 1.38 | boy | 1 | 1 | 葫芦一班 | 1 |
| 5 | 四娃 | 4 | NULL | unknow | 4 | NULL | NULL | NULL |
| 4 | 三娃 | 5 | NULL | unknow | 3 | 3 | 葫芦三班 | 3 |
| 3 | 二娃 | 6 | 1.63 | girl | 2 | 2 | 葫芦二班 | 2 |
| 2 | 大娃 | 7 | 1.72 | boy | 1 | 1 | 葫芦一班 | 1 |
| 1 | 葫芦爷爷 | 73 | 1.75 | boy | NULL | NULL | NULL | NULL |
+----+--------------+------+------+--------+--------+------+--------------+--------+
视图
- 视图的本质:就是对查询的封装
- 视图数据不能增,删,改
- 以 v_ 开头命名
## 创建视图
MariaDB [test1]> create view 视图名 as select语句;
MariaDB [test1]> create view v_class as select huluwa.name as '姓名', school.name as '班级' from huluwa left join school on school.cla_id=huluwa.cla_id;
Query OK, 0 rows affected (0.004 sec)
##查看视图
MariaDB [test1]> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| heros |
| huluwa |
| school |
| v_class |
+-----------------+
##删除视图
MariaDB [test1]> drop view v_class;
Query OK, 0 rows affected (0.000 sec)
外键 foreign key
如果一个实体的某个字段指向另一个实体的主键,就称为外键。被指向的实体,称之为主实体(主表),也叫父实体(父表)。负责指向的实体,称之为从实体(从表),也叫子实体(子表)
对关系字段进行约束,当为从表中的关系字段填写值时,会到关联的主表中查询此值是否存在,如果存在则填写成功,如果不存在则填写失败并报错
## (因为外键需要对应)对表做些改动
MariaDB [test1]> select * from huluwa;
+----+--------+------+------+--------+--------+
| id | name | age | high | gender | cla_id |
+----+--------+------+------+--------+--------+
| 2 | 大娃 | 7 | 1.72 | boy | 1 |
| 3 | 二娃 | 6 | 1.63 | girl | 2 |
| 4 | 三娃 | 5 | NULL | unknow | 3 |
| 5 | 四娃 | 4 | NULL | unknow | 4 |
| 6 | 五娃 | 3 | 1.38 | boy | 1 |
| 7 | 六娃 | 2 | 1.82 | boy | 2 |
| 8 | 七娃 | 3 | 1.83 | girl | 3 |
+----+--------+------+------+--------+--------+
7 rows in set (0.001 sec)
MariaDB [test1]> select * from school;
+----+--------------+--------+
| id | name | cla_id |
+----+--------------+--------+
| 1 | 葫芦一班 | 1 |
| 2 | 葫芦二班 | 2 |
| 3 | 葫芦三班 | 3 |
| 4 | 葫芦四班 | 4 |
+----+--------------+--------+
## 查看表结构(关联键须同type)
MariaDB [test1]> desc school;
+--------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| cla_id | int(10) unsigned | YES | | NULL | |
+--------+------------------+------+-----+---------+----------------+
3 rows in set (0.001 sec)
MariaDB [test1]> alter table huluwa change cla_id cla_id int unsigned;
Query OK, 7 rows affected (0.055 sec)
Records: 7 Duplicates: 0 Warnings: 0
MariaDB [test1]> desc huluwa;
+--------+-----------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-----------------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(10) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
| high | decimal(5,2) | YES | | NULL | |
| gender | enum('boy','girl','unknow') | YES | | unknow | |
| cla_id | int(10) unsigned | YES | | NULL | |
+--------+-----------------------------+------+-----+---------+----------------+
## 给葫芦娃表添加外键
-- 外键必须受约束于主键;即(school(id)中的id必须是主键)
-- 外键和主键的结构需要一样,即(type相同)
MariaDB [test1]> alter table huluwa add constraint fk foreign key(cla_id) references school(id);
Query OK, 7 rows affected (0.052 sec)
Records: 7 Duplicates: 0 Warnings: 0
## 删除外键
MariaDB [test1]> alter table huluwa drop foreign key fk;
Query OK, 0 rows affected (0.002 sec)
Records: 0 Duplicates: 0 Warnings: 0
## 添加 级联删除的外键(删除父表内容,子表数据级联删除):on delete cascade
MariaDB [test1]> alter table huluwa add constraint fk2 foreign key(cla_id) references school(id) on delete cascade;
Query OK, 7 rows affected (0.058 sec)
Records: 7 Duplicates: 0 Warnings: 0
## 删除父表内容
MariaDB [test1]> delete from school where id=1;
Query OK, 1 row affected (0.002 sec)
## 查看
MariaDB [test1]> select * from school;
+----+--------------+--------+
| id | name | cla_id |
+----+--------------+--------+
| 2 | 葫芦二班 | 2 |
| 3 | 葫芦三班 | 3 |
| 4 | 葫芦四班 | 4 |
+----+--------------+--------+
3 rows in set (0.000 sec)
MariaDB [test1]> select * from huluwa;
+----+--------+------+------+--------+--------+
| id | name | age | high | gender | cla_id |
+----+--------+------+------+--------+--------+
| 3 | 二娃 | 6 | 1.63 | girl | 2 |
| 4 | 三娃 | 5 | NULL | unknow | 3 |
| 5 | 四娃 | 4 | NULL | unknow | 4 |
| 7 | 六娃 | 2 | 1.82 | boy | 2 |
| 8 | 七娃 | 3 | 1.83 | girl | 3 |
+----+--------+------+------+--------+--------+
## 查看外键
-- show create table 表名
索引
- 索引作用:当数据库中数据量很大时,可以加快索引速度,但不是越多越好,数据y有大量变动的不适合添加索引;
-- create index 索引名 on 表名(字段名)
MariaDB [test1]> create index age_index on huluwa(age);
Query OK, 0 rows affected (0.013 sec)
Records: 0 Duplicates: 0 Warnings: 0
-- 查看索引
show index from 表名;
-- 删除索引
drop index 索引名 on 表名;
MariaDB数据库----查(实例演示)的更多相关文章
- MariaDB数据库 ----数据库简介,用户管理,数据库创建,数据类型、数据增删改(实例演示)
数据库简介 数据库--即电子文件柜,用户可以对文件中的数据进行增,删,改,查等操作. 数据库分类 关系型数据库 关系型数据库管理系统(Relational Database Management Sy ...
- python链接oracle数据库以及数据库的增删改查实例
初次使用python链接oracle,所以想记录下我遇到的问题,便于向我这样初次尝试的朋友能够快速的配置好环境进入开发环节. 1.首先,python链接oracle数据库需要配置好环境. 我的相关环境 ...
- java:JSP(JSPWeb.xml的配置,动态和静态导入JSP文件,重定项和请求转发,使用JSP实现数据库的增删改查实例)
1.JSP的配置: <%@ page language="java" import="java.util.*" pageEncoding="UT ...
- 最简实例演示asp.net5中用户认证和授权(3)
上接: 最简实例演示asp.net5中用户认证和授权(2) 在实现了角色的各种管理接口后,下一步就是实现对用户的管理,对用户管理的接口相对多一些,必须要实现的有如下三个: 1 public inter ...
- 最简实例演示asp.net5中用户认证和授权(2)
上接最简实例演示asp.net5中用户认证和授权(1) 基础类建立好后,下一步就要创建对基础类进行操作的类了,也就是实现基础类的增删改查(听起来不太高大上),当然,为了使用asp.net5的认证机制, ...
- MySQL/MariaDB数据库的主从复制
MySQL/MariaDB数据库的主从复制 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL复制概述 1>.传统扩展方式 垂直扩展(也叫向上扩展,Sacle ...
- MySQL/MariaDB数据库的用户和权限管理
MySQL/MariaDB数据库的用户和权限管理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.元数据数据库(mysql) 系统授权表(均在mysql数据库中): db hos ...
- oracle数据库_实例_用户_表空间之间的关系(转)
数据库:Oracle数据库是数据的物理存储.这就包括(数据文件ORA或者DBF.控制文件.联机日志.参数文件).其实Oracle数据库的概念和其它数据库不一样,这里的数据库是一个操作系统只有一个库.可 ...
- CentOS 7.0下配置MariaDB数据库
刚刚配置了下CentOS 7.0版本的服务器,配置数据库时发现# mysql_secure_installation命令用不了,之后网上查了一下发现CentOS 7.0版本用MariaDB替换了mys ...
随机推荐
- cmake - 可执行文件
1.生成可执行文件 add_executable(hello xxx.cpp xxxxx.cpp) ##根据文件xxx.cpp和xxxx.cpp生成可执行文件hello,但是这两个可执行文件如果依赖其 ...
- 改进你的c#代码的5个技巧(二)
在本文中,我将向你展示c#编程的5个最佳实践.我从日常编程经验中学到了这些实践.我在release模式下测试了所有的代码,并在开发环境稳定后进行了截屏.我想你会喜欢这些建议的. 在使用数据类型之前选择 ...
- 设计模式之SOLID原则
介绍 设计模式中的SOLID原则,分别是单一原则.开闭原则.里氏替换原则.接口隔离原则.依赖倒置原则.前辈们总结出来的,遵循五大原则可以使程序解决紧耦合,更加健壮. SRP 单一责任原则 OCP 开放 ...
- SpringBoot 集成Elasticsearch进行简单增删改查
一.引入的pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...
- linux系统重启后提示An error occurred during the file system check.
一.问题描述 生产环境中一台浪潮NF8480M3外观红灯报警,鉴于无法登陆带外管理口,只能对服务器进行断电重启操作 二.问题现象 重启后进入开机过程并报错,正常来说进入此界面后直接输入root密码即可 ...
- 简单做了一个代办事项列表系统 ,增删改查。 前台页面用 jquery 后台用nodejs ,数据库用的是mongdoDB 。
引入npm 第三方mongoose包连接mongoDb本地数据库todolist const mongoose = require('mongoose'); mongoose.connect('mon ...
- LeetCode703 流中第k大的元素
前言: 我们已经介绍了二叉搜索树的相关特性,以及如何在二叉搜索树中实现一些基本操作,比如搜索.插入和删除.熟悉了这些基本概念之后,相信你已经能够成功运用它们来解决二叉搜索树问题. 二叉搜索树的有优点是 ...
- 面试官:Netty的线程模型可不只是主从多Reactor这么简单
笔者看来Netty的内核主要包括如下图三个部分: 其各个核心模块主要的职责如下: 内存管理 主要提高高效的内存管理,包含内存分配,内存回收. 网通通道 复制网络通信,例如实现对NIO.OIO等底层JA ...
- 虚拟机linux共享文件夹
linux共享文件夹问题 1. 初配置 2. 挂载 018.7.8 为了实现win7与VMware中linux文件夹共享很花费了一些时间,但终成正果 1. 初配置 虚拟机设置->选项->共 ...
- MySQL 使用MD5对数据进行加密
数据库MD5加密 -- ================ 测试 MD5 加密 ============== CREATE TABLE `testmd5`( id INT(11) NOT NULL AU ...