MySQL的外键,修改表,基本数据类型,表级别操作,其他(条件,通配符,分页,排序,分组,联合,连表操作):
a.创建2张表
create table userinfo(nid int not null auto_increment primary key,
name varchar(10),
age int,
part_nid int
)engine=innodb default charset=utf8; create table part(
nid int not null auto_increment primary key,
caption varchar(20)
)engine=innodb default charset=utf8; 添加一些数据,创建一个中间表约束part_nid和part表,这就是外键
mysql> select * from userinfo;
+-----+------+------+----------+
| nid | name | age | part_nid |
+-----+------+------+----------+
| 1 | h | 19 | 2 |
| 2 | hh | 19 | 2 |
| 3 | hhh | 19 | 2 |
+-----+------+------+----------+
3 rows in set (0.00 sec) mysql> alter table userinfo add constraint fk_u_p foreign key userinfo(part_nid) references part(nid);
Query OK, 3 rows affected (0.88 sec)
Records: 3 Duplicates: 0 Warnings: 0 然后我们在进行数据的插入,这时候由于约束条件的存在,所以我们就无法进行插入part_nid为100的数据
mysql> insert into userinfo(name,age,part_nid) values('b',19,100);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test`.`userinfo`, CONSTRAINT `fk_u_p` FOREIGN KEY (`part_nid`) REFERENCES `part` (`nid`))
mysql> b.外键foreign key ,一对多(在创建表之前就知道有关联的列和表)
总结:
2张表的建立约束
--约束
c.修改表(列的增删查改)
添加列:alter table 表名 add 列名 类型;
删除列:alter table 表名 drop column 列名;
修改列:
alter table 表名 modify column 列名 类型;--类型
alter table 表名 change 原列名 新列名 类型;--列名,类型
添加主键:
alter table 表名 add primary key(列名);
删除主键:
alter table 表名 drop primary key;
alter table 表名 modify 列名 int,drop primary key;
添加外键:
alter table 从表 add constraint 外键名字(形如:fk_从表_主表) foreign key 从表(外键字段)references 主表(主键字段);
删除外键:
alter table 表名 drop foreign key 外键名称;
d.基本的数据类型:数值,时间和字符串
数值:
bit 二进制
tinyint
smallint
int
bigint
--范围不一样 decimal:十进制小数,精确的
FLOAT
DOUBLE 字符串:
char(定长)
create table tb13(n char(7))
不管怎样都要占用7个字符的空间,查找速度快,浪费内存空间
varchar(变长)
varchar是最大占用7个字符的空间,查找速度慢,节省空间
text
mediumtext
longtext
二进制数据:
TineyBlob Blob MediumBlob LongBlob
#上传文件
#强制二进制文件
#将上传的文件保存在硬盘
时间:
DATE: YYYY--MM--DD
TIME: HH:MM:SS
YEAR: YYYY
DATETIME: YYYY--MM--DD HH:MM:SS
TIMESTAMP: YYYMMDD HHMMSS enum:
表级别的操作*****
select * from tb1;
#增
insert into biao2(name,age) values('bob',234);#可以插一条数据,
insert into biao2(name,age) values('lizebo',26),('eric',30);#可以插多条
insert into biao1(name,age) select name,age form biao2;#可以把另外一个表中的内容插入到这个表中
#删
delete from biao2;#全清空
delete from biao2 where id=1 and name='alex';#按条件删除
#查
select * from 表;#查看表的全部内容,select *这种操作效率比较低,最好的方式就是写一遍,效率高。
select * from 表 where id>1;#按条件查询
select nid,name,gender as gg from 表 where id>1;
#改(跟新)
update 表 set name='alex' where id>1;#跟新设置某项内容 #其他
a.条件
select * from 表 where id>1 and name != 'alizbeo' and nid=12;
select * from 表 where id between 5 and 16;#在...之间的数据
select * from 表 where id in (11,22,33);#是否在这个元组中
select * from 表 where id not in (22,33,44);
select * from 表 where id in (select * from biao2);
b.通配符(模糊搜索)
select * from 表 where name like 'alex%';#alex开头的所有(多个字符串)
select * from 表 where name like 'ale_';#_表示ale开头的所有(一个字符)
c.分页
select * from 表 limit 5;#前5行
select * from 表 limit 0,4;#从0行开始取4行
select * from 表 limit 4 offset 0;#从0开始取4行,这个比较常用
d.排序
select * from 表 order by 列 asc;#根据‘列’从小到大排序
select * from 表 order by 列 desc;#根据‘列’从大到小排序
select * from 表 order by 列1 desc,列2 asc;#根据‘列1’从小打到排序,如果相同则按列2从小到大排序
e.分组(重要)
select num form 表 group by num;
select num,nid from 表 group by num,nid;
select num,nid from 表 where nid>10 group by num,nid order nid desc;
select num,nid ,conut(*),sum(score),max(score),min(score) form 表 group by num,nid; 当我们对聚合条件进行查询时候,我们需要使用having
select num from 表 group by num having max(id)>10;
特别的:group by 必须在where 之后,order by之前
列子:我们有这样的一个userinfo表:
mysql> select * from userinfo;
+-----+--------+------+----------+
| nid | name | age | part_nid |
+-----+--------+------+----------+
| 1 | h | 19 | 2 |
| 2 | hh | 19 | 2 |
| 3 | hhh | 19 | 2 |
| 4 | eirc | 23 | 1 |
| 5 | lizebo | 23 | 1 |
| 6 | bobli | 24 | 1 |
| 7 | jim | 21 | 3 |
| 8 | jams | 22 | 3 |
+-----+--------+------+----------+
我们需要进行分组查询,
mysql> select part_nid, min(nid),max(nid),count(nid) from userinfo group by part_nid;
+----------+----------+----------+------------+
| part_nid | min(nid) | max(nid) | count(nid) |
+----------+----------+----------+------------+
| 1 | 4 | 6 | 3 |
| 2 | 1 | 3 | 3 |
| 3 | 7 | 8 | 2 |
+----------+----------+----------+------------+
3 rows in set (0.00 sec)
这里的part_nid就是把相同的nid分成了3类,1,2,3类
min(nid)==最小的nid,max(nid)==最大的nid,count(nid)==总数nid
f.联合
组合,自动处理组合
select nid from 表 union select nid from 表2;#这是去重数据的
select nid from 表 union all select nid from 表2;#这是去重的
g.连表操作
第一种连表操作:
如果有一张userinfo和part表,并且这2张表式通过外键关联的,我们要查询这2张表中所有数据
mysql> select * from userinfo;
+-----+--------+------+----------+
| nid | name | age | part_nid |
+-----+--------+------+----------+
| 1 | h | 19 | 2 |
| 2 | hh | 19 | 2 |
| 3 | hhh | 19 | 2 |
| 4 | eirc | 23 | 1 |
| 5 | lizebo | 23 | 1 |
| 6 | bobli | 24 | 1 |
| 7 | jim | 21 | 3 |
| 8 | jams | 22 | 3 |
+-----+--------+------+----------+
8 rows in set (0.00 sec) mysql> select * from part;
+-----+---------+
| nid | caption |
+-----+---------+
| 1 | IT |
| 2 | TI |
| 3 | SA |
| 4 | DEV |
+-----+---------+
4 rows in set (0.00 sec)
如果我们这样查询连表会产生笛卡儿积,表1中的每条数据都会到表2中进行查询4次。
mysql> select name,age,part_nid from userinfo,part;
+--------+------+----------+
| name | age | part_nid |
+--------+------+----------+
| h | 19 | 2 |
| h | 19 | 2 |
| h | 19 | 2 |
| h | 19 | 2 |
| hh | 19 | 2 |
| hh | 19 | 2 |
| hh | 19 | 2 |
| hh | 19 | 2 |
| hhh | 19 | 2 |
| hhh | 19 | 2 |
| hhh | 19 | 2 |
| hhh | 19 | 2 |
| eirc | 23 | 1 |
| eirc | 23 | 1 |
| eirc | 23 | 1 |
| eirc | 23 | 1 |
| lizebo | 23 | 1 |
| lizebo | 23 | 1 |
| lizebo | 23 | 1 |
| lizebo | 23 | 1 |
| bobli | 24 | 1 |
| bobli | 24 | 1 |
| bobli | 24 | 1 |
| bobli | 24 | 1 |
| jim | 21 | 3 |
| jim | 21 | 3 |
| jim | 21 | 3 |
| jim | 21 | 3 |
| jams | 22 | 3 |
| jams | 22 | 3 |
| jams | 22 | 3 |
| jams | 22 | 3 |
+--------+------+----------+
32 rows in set (0.00 sec)
消除笛卡儿积:
第一种方式:
mysql> select name,age,part_nid from userinfo,part where userinfo.part_nid=part.nid;
+--------+------+----------+
| name | age | part_nid |
+--------+------+----------+
| h | 19 | 2 |
| hh | 19 | 2 |
| hhh | 19 | 2 |
| eirc | 23 | 1 |
| lizebo | 23 | 1 |
| bobli | 24 | 1 |
| jim | 21 | 3 |
| jams | 22 | 3 |
+--------+------+----------+
8 rows in set (0.06 sec)
其实就是按关联条件查询!
第二种方式:
mysql> select * from userinfo left join part on userinfo.part_nid=part.nid;
+-----+--------+------+----------+------+---------+
| nid | name | age | part_nid | nid | caption |
+-----+--------+------+----------+------+---------+
| 4 | eirc | 23 | 1 | 1 | IT |
| 5 | lizebo | 23 | 1 | 1 | IT |
| 6 | bobli | 24 | 1 | 1 | IT |
| 1 | h | 19 | 2 | 2 | TI |
| 2 | hh | 19 | 2 | 2 | TI |
| 3 | hhh | 19 | 2 | 2 | TI |
| 7 | jim | 21 | 3 | 3 | SA |
| 8 | jams | 22 | 3 | 3 | SA |
+-----+--------+------+----------+------+---------+
8 rows in set (0.00 sec)
可以用inner join on其实对left join on进行了一个null的过滤。

最新文章

  1. fmt 标签格式化 日期
  2. Chart图表
  3. group_concat函数详解
  4. Android自定义对话框(Dialog)位置,大小
  5. Python开发入门与实战6-表单
  6. arm64 boot
  7. 修复 Java 内存模型,第 1 部分——Brian Goetz
  8. 巴科斯范式和sql语言
  9. 异步编程之Promise(2):探究原理
  10. Serv-U软件在64位操作系统下使用不了odbc解决方法
  11. bzoj3143
  12. mysql主从监控
  13. 怎样使用 App Studio 高速定制你自己的 Universal Windows App
  14. Spring MVC 的文件下载
  15. BZOJ 2038: [2009国家集训队]小Z的袜子(hose)【莫队算法裸题&&学习笔记】
  16. Linux 基本bash命令
  17. Go channel实现源码分析
  18. (27)How to stay calm when you know you'll be stressed
  19. HanLP 关键词提取算法分析
  20. HttpResonse 要记得关闭

热门文章

  1. hibernate使用注解简化开发
  2. mysql 外键的几种约束
  3. Windows7搭建Wamp环境
  4. JavaAPI 中 <E> 与 <T> 的含义
  5. iOS 之 UITextField
  6. 10大H5前端框架,让你开发不愁
  7. 前端性能优化jQuery性能优化
  8. H5新特性汇总
  9. linux学习(十一)用户和用户组管理
  10. Winsock网络编程笔记(1)----入门