一 一对多,多对一

 1.1 建立多对一 ,一对多的关系需要注意
先建立被关联的表,被关联的字段必须保证时唯一的
在创建关联的表,关联的字段一定是可以重复的 1.2 示例;
出版社 多对一,多个老师可能在一家出版社
一夫多妻 一对多
create table dep(. 被关联的字段必须保证唯一
id int primary key auto_increment,
name varchar(20),
comment varchar(50)
); create table emp(
id int primary key auto_increment,
name varchar(20),
dep_id int, 关联的字段一定保证可以重复的
constraint fk_depid_id foreign key(dep_id) references dep(id)
foreign key(dep_id) 本表关联字段
references 后面接指定关联的表,一定是唯一的
on update cascade
on delete cascade
);

二 一对一

 2.1 示例. 用户表,管理员表
create table user(
uid int primary key auto_increment,
name varchar(20)
);
insert into user(name) values('egon'); create table admin(
id int primary key auto_increment,
user_id int unique, 唯一
password varchar(20),
constraint foreign key(user_id) refreences user(uid) 被关联的字段一定是唯一的
on update cascade
on delete cascade
);
insert into admin(user_id,password) values(3,'alex3714'); 2.2 注意关联字段与被关联的字段一定都是唯一的 2.3 示例 学生和客户,客户转化为学生
一个学生肯定是一个客户,但是客户不一定学生

三 多对多,双向的多对一,就变成多对多

 3.1 示例. 作者,书
create table book(
id int primary key auto_increment,
name varchar(20)
price varchar(20)
); create table book2author(
id int primary key auto_increment,
book_id int,
author_id int,
constraint foreign key(book_id) references book(id),
constraint foreign key(author_id) references author(id)
on update cascade
on delete cascade,
unique(book_id,author_id) 联合唯一
); create table author(
id int primary key auto_increment,
name varchar(20)
);

四 简单单表查询

 1 简单查询
select * from t1; 先找到表,在找到记录,测试时候用
select name,id from t1; 2 where条件 and > < = != between or in is not
select name,id from t1 where id > 3; 先找表,在走条件,然后字段
select name,id from t1 where id > 3 and id <1 0; 多条件
select name,id from t1 where id between 3 and 10; 在..之间 not between
select id from t1 where id=3 or id=4 or id=5;
select id from t1 where id in (3,4,5);
select id from t1 where id is Null;可以判断是不是为空''并非空这么简单,只有Null才能用is判断,''用==判断
select name from t1 where name like '%n%';
select name from t1 where name like 'e__n'; _代表是匹配一个 3 group by分组
select stu_id,group_concat(name) from t1 group by stu_id; 按照id分组
group_concat(name) 看组里面有哪些人,就需要这个聚合函数
select stu_id,count(id) from t1 group by stu_id; 查看每个组里面多个人
select stu_id,max(id) from t1 group by stu_id; 查看每个组里的最大id
max min sum avg平均 删除字段
alter table t1 drop age;
alter table t1 change id id(int3); 可以用modify替代
alter table t1 add primary key(id,age); 将谁设置成主键
alter table t1 drop primary key; 删除主键

python开发mysql:表关系&单表简单查询的更多相关文章

  1. Python、mysql四-1:单表查询

    一 单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 二 关键 ...

  2. mysql 数据操作 单表查询 目录

    mysql 数据操作 单表查询 mysql 数据操作 单表查询 简单查询 避免重复DISTINCT mysql 数据操作 单表查询 通过四则运算查询 mysql 数据操作 单表查询 concat()函 ...

  3. 数据库开发-Django ORM的单表查询

    数据库开发-Django ORM的单表查询 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查询集 1>.查询集相关概述 查询会返回结果的集,它是django.db.mod ...

  4. mysql 数据操作 单表查询 where 约束 目录

    mysql 数据操作 单表查询 where约束 between and or mysql 数据操作 单表查询 where约束 is null in mysql 数据操作 单表查询 where约束 li ...

  5. mysql 数据操作 单表查询 group by 分组 目录

    mysql 数据操作 单表查询 group by 介绍 mysql 数据操作 单表查询 group by 聚合函数 mysql 数据操作 单表查询 group by 聚合函数 没有group by情况 ...

  6. MySQL数据库之单表查询中关键字的执行顺序

    目录 MySQL数据库之单表查询中关键字的执行顺序 1 语法顺序 2 执行顺序 3 关键字使用语法 MySQL数据库之单表查询中关键字的执行顺序 1 语法顺序 select distinct from ...

  7. MySQL数据库语法-单表查询练习

    MySQL数据库语法-单表查询练习 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客主要是对聚合函数和分组的练习. 一.数据表和测试数据准备 /* @author :yinz ...

  8. Python学习day44-数据库(单表及多表查询)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  9. mysql 基础入门 单表查询

    单表查询 select 表头,表头 as 别名 ,表头(+-*/的运算) from table_a 1.条件查询 where + 条件 <> , != 不等于 = 等于,也可以表示字符串值 ...

随机推荐

  1. SQL命令查询Oracle存储过程信息(代码内容等)

    SELECT * FROM ALL_SOURCE  where TYPE='PROCEDURE'  AND TEXT LIKE '%0997500%'; --查询ALL_SOURCE中,(脚本代码)内 ...

  2. spring4x,暂时停更

    spring4x,暂时停更 鄙人愚笨,没有spring基础,直接上了spring4x,发现无法理解(另外spring4x实战课本演示不详,本人学识有限),现从spring3开始.

  3. 解决:夜神模拟器连不上adb的问题

    一. adb devices发现不了设备 刚试了一下,在夜神模拟器开启的情况下,adb devices 死活找不到设备 adb kill-server和adb start-server也无济于事 二. ...

  4. C++多态、虚函数、纯虚函数、抽象类

    多态 同一函数调用形式(调用形式形同)可以实现不同的操作(执行路径不同),就叫多态. 两种多态: (1)静态多态:分为函数重载和运算符重载,编译时系统就能决定调用哪个函数. (2)动态多态(简称多态) ...

  5. 个人作业4——alpha阶段个人小结

    一.个人总结 在alpha 结束之后, 每位同学写一篇个人博客, 总结自己的alpha 过程: 请用自我评价表:http://www.cnblogs.com/xinz/p/3852177.html 有 ...

  6. elasticsearch 2.2+ index.codec: best_compression启用压缩

    官方说法,来自https://www.elastic.co/guide/en/elasticsearch/reference/2.2/index-modules.html#_static_index_ ...

  7. Struts05---动态查询

    01.在上面案例的login.jsp页面新增 <%-- 2.动态方法的调用 前提是在 struts.xml文件中开启 不推荐! --%> <a href="user/use ...

  8. react: next-redux-saga

    instead of using the Provider component, you can use the withRedux higher order component to inject ...

  9. 2017.11.7 Python 制作EFM32/ AVR批量烧录工具

    Customer need program quickly asap. ok,I need to set up a table for test. 1 reference data http://ww ...

  10. ubuntu 设置plank开机自启之后关机键失效变为注销键

    之前因为猎奇心,给我的ubuntu系统换了一个macUbuntu的桌面,但是之前用的dock是docky,昨日闲来无聊换成了plank,设置成然后就发现我的系统关不了机了,只能通过指令关机. 百度之后 ...