10.3.1表的约束
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 约束
# unsigned 设置某一个数字无符号
# not null 某一个字段不能为空
# default 给某个字段设置默认值
# unique 设置某一个字段不能重复
# 联合唯一
# auto_increment 设置某一个int类型的字段 自动增加
# auto_increment自带not null效果
# 设置条件 int unique
# primary key 设置某一个字段非空且不能重复
# 约束力相当于 not null + unique
# 一张表只能由一个主键
# foreign key 外键
# references
# 级联删除和更新
# not null
# default
# create table t2(
# id int not null,
# name char(12) not null,
# age int default 18,
# gender enum('male','female') not null default 'male'
# )
# unique 设置某一个字段不能重复
# create table t3(
# id int unique,
# username char(12) unique,
# password char(18)
# );
# 联合唯一
# create table t4(
# id int,
# ip char(15),
# server char(10),
# port int,
# unique(ip,port)
# );
# 自增
# auto_increment
# 自增字段 必须是数字 且 必须是唯一的
# create table t5(
# id int unique auto_increment,
# username char(10),
# password char(18)
# )
# insert into t5(username,password) values('alex','alex3714')
# primary key 主键
# 一张表只能设置一个主键
# 一张表最好设置一个主键
# 约束这个字段 非空(not null) 且 唯一(unique)
# create table t6(
# id int not null unique, # 你指定的第一个非空且唯一的字段会被定义成主键
# name char(12) not null unique
# )
# create table t7(
# id int primary key, # 你指定的第一个非空且唯一的字段会被定义成主键
# name char(12) not null unique
# )
# 联合主键
# create table t4(
# id int,
# ip char(15),
# server char(10),
# port int,
# primary key(ip,port)
# );
# 外键 foreign key 涉及到两张表
# 员工表
# create table staff(
# id int primary key auto_increment,
# age int,
# gender enum('male','female'),
# salary float(8,2),
# hire_date date,
# post_id int,
# foreign key(post_id) references post(pid)
# )
# 部门表
# pid postname post_comment post_phone
# create table post(
# pid int primary key,
# postname char(10) not null unique,
# comment varchar(255),
# phone_num char(11)
# )
# update post set pid=2 where pid = 1;
# delete from post where pid = 1;
# 级联删除和级联更新
# create table staff2(
# id int primary key auto_increment,
# age int,
# gender enum('male','female'),
# salary float(8,2),
# hire_date date,
# post_id int,
# foreign key(post_id) references post(pid) on update cascade on delete set null
# )
10.3.2表的修改
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 创建项目之前
# 项目开发、运行过程中
# alter table 表名 add 添加字段
# alter table 表名 drop 删除字段
# alter table 表名 modify 修改已经存在的字段 的类型 宽度 约束
# alter table 表名 change 修改已经存在的字段 的类型 宽度 约束 和 字段名字
# alter table 表名 add 字段名 数据类型(宽度) 约束 first/after name
# alter table 表名 drop 字段名
# alter table 表名 modify name varchar(12) not null
# alter table 表名 change name new_name varchar(12) not null
# id name age
# alter table 表名 modify age int not null after id;
# alter table 表名 modify age int not null first;
10.3.3表的关系
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 两张表中的数据之间的关系
# 多对一 foreign key 永远是在多的那张表中设置外键
# 多个学生都是同一个班级的
# 学生表 关联 班级表
# 学生是多 班级是一
# 一对一 foreign key +unique # 后出现的后一张表中的数据 作为外键,并且要约束这个外键是唯一的
# 客户关系表 : 手机号码 招生老师 上次联系的时间 备注信息
# 学生表 :姓名 入学日期 缴费日期 结业
# 多对多 产生第三张表,把两个关联关系的字段作为第三张表的外键
# 书
# 作者
# 出版社
# 书
10.3.4数据的操作
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 增加 insert
# 删除 delete
# 修改 update
# 查询 select
# 增加 insert
# insert into 表名 values (值....)
# 所有的在这个表中的字段都需要按照顺序被填写在这里
# insert into 表名(字段名,字段名。。。) values (值....)
# 所有在字段位置填写了名字的字段和后面的值必须是一一对应
# insert into 表名(字段名,字段名。。。) values (值....),(值....),(值....)
# 所有在字段位置填写了名字的字段和后面的值必须是一一对应
# value单数 values复数
# 一次性写入一行数据 一次性写入多行数据
# t1 id,name,age
# insert into t1 value (1,'alex',83)
# insert into t1 values (1,'alex',83),(2,'wusir',74)
# insert into t1(name,age) value ('alex',83)
# insert into t1(name,age) values ('alex',83),('wusir',74)
# 第一个角度
# 写入一行内容还是写入多行
# insert into 表名 values (值....)
# insert into 表名 values (值....),(值....),(值....)
# 第二个角度
# 是把这一行所有的内容都写入
# insert into 表名 values (值....)
# 指定字段写入
# insert into 表名(字段1,字段2) values (值1,值2)
# 删除 delete
# delete from 表 where 条件;
# 更新 update
# update 表 set 字段=新的值 where 条件;
# 查询
# select语句
# select * from 表
# select 字段,字段.. from 表
# select distinct 字段,字段.. from 表 # 按照查出来的字段去重
# select 字段*5 from 表 # 按照查出来的字段去重
# select 字段 as 新名字,字段 as 新名字 from 表 # 按照查出来的字段去重
# select 字段 新名字 from 表 # 按照查出来的字段去重