一 一对多,多对一

 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. 如果在applicationContext.xml中没有配置bean的属性,那么也会导致空指针异常

    报错如下: java.lang.NullPointerException cn.itcast.action.VisitAction.toAddPage(VisitAction.java:37) sun ...

  2. spring3: 对JDBC的支持 之 Spring提供的其它帮助 SimpleJdbcInsert/SimpleJdbcCall/SqlUpdate/JdbcTemplate 生成主键/批量处理

    7.4  Spring提供的其它帮助 7.4.1  SimpleJdbc方式 Spring JDBC抽象框架提供SimpleJdbcInsert和SimpleJdbcCall类,这两个类通过利用JDB ...

  3. STM32F103: NRF24L01

    看了两天的24l01的相关资料了,一直有点模糊,今天下午感觉有点懂了,在板子上调试成功了,但是还没进行通讯测试.stm32和arduino进行通信还没成功 ,:( 先把stm32的NRF24L01配置 ...

  4. Makefile的补充学习2

    Makefile中使用通配符(1)* 若干个任意字符(2)? 1个任意字符(3)[] 将[]中的字符依次去和外面的结合匹配 还有个%,也是通配符,表示任意多个字符,和*很相似,但是%一般只用于规则描述 ...

  5. 广义线性模型(GLM)

    一.广义线性模型概念 在讨论广义线性模型之前,先回顾一下基本线性模型,也就是线性回归. 在线性回归模型中的假设中,有两点需要提出: (1)假设因变量服从高斯分布:$Y={{\theta }^{T}}x ...

  6. LeetCode OJ:Ugly Number(丑数)

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  7. 集合中的工具类Collections和Arrays

    集合框架的工具类: Collections: 方法sort(): List<String> list = new ArrayList<String>();        lis ...

  8. UI- Layer的使用总结(附动画)

    #pargma mark - Layer 1. 设置当前视图的背景颜色 self.view.backgroundColor = [UIColor lightGrayColor]; 2. 创建一个视图, ...

  9. L129

    Iraq Sees Spike in Water-Borne IllnessesIraqi health officials say that a health crisis stemming fro ...

  10. [置顶] Android RadioButton与TextView浪漫约会?

    情景一 今天主要实现一个国家与地区切换,就是当我们选中RadioButton时然后将值设置到TextView中,听着这需求应该不难对吧?那么我们就开始约会吧? 看下原型图 准备条件: 首先需要一个ra ...