使用python对学生表的查询_python — 表的操作(一)

本文介绍了如何使用Python进行学生表的创建,包括不同存储引擎的选择,以及表的结构查看。详细讲解了表的约束,如unsigned、not null、default、unique、auto_increment、primary key和foreign key的用法。还涵盖了表的修改和数据操作,如insert、delete、update和select查询,以及表关系的概念和类型。
摘要由CSDN通过智能技术生成

1. 创建表

创建表:

create table t1 (id int,name char(4));

create table t2 (id int,name char(4)) engine=myisam; # 使用myisam存储引擎

create table t3 (id int,name char(4)) engine=memory; # 使用memory存储引擎

查看表的结构:

show create table 表名; — 能够看到和这张表相关的所有信息

desc 表名; — 只能查看表的字段的基础信息

desc 表名; = describe 表名;

2. 表的约束

1.unsigned

unsigned —— 设置某一个数字无字符

2.not null

not null —— 某一个字段不能为空

严格模式会影响非空设置的效果

3.default

default 给某个字段设置默认值

create table t2(

id int not null,

name char(12) not null,

age int default 18, # 设置默认值为18,但不会自动填充

gender enum('male','female') not null default 'male' # 不填充gender的值时,会自动默认填充'male'

)

4.unique

unique 设置某一个字段不能重复

create table t3(

id int unique,

username char(12) unique,

password char(18)

);

联合唯一

需要联合的两个字段都不唯一,但是两个字段联合在一起时就要是唯一的。

将需要联合的两个字段写道unique()里 —— unique(字段名1,字段名2)

create table t4(

id int,

ip char(15),

server char(10),

port int,

unique(ip,port)

);

94783497727453eccd26b9469cb4a4be.png

5.auto_increment

auto_increment 设置某一个int类型的字段 自动增加

字段设置条件 :必须是数字 且 必须是唯一的 —— int + unique

auto_increment自带非空not null 、自增的效果

create table t5(

id int unique auto_increment,

username char(10),

password char(18)

)

insert into t5(username,password) values('alex','alex3714') # id字段设置为自增字段,增加数据时不输入id字段的值,它会自动增加

6.primary key 主键

primary key 设置某一个字段非空且不能重复

约束这个字段 :非空(not null) 且 唯一(unique) 相当于 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)

);

fe7ed51e13d47b92894cc89eec5bb980.png

7.foreign key 外键 涉及到两张表

references

外键关联的字段至少必须是唯一unique的,所以会直接将被关联的这个字段设置成主键。

先创建被关联的外表,再创建本表。

部门表 : 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)

)

员工表

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)

)

insert into post / staff values …………

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 cascade

)

如果级联删除外表中关联的数据后,让本表中被关联的外键列数据仍然存在,需要将外键列设置为空null :

on delete cascade 写成 on delete set null

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

)

on delete:

cascade方式:

在父表上update/delete记录时,同步update/delete掉子表的匹配记录

set null方式:

在父表上update/delete记录时,将子表上匹配记录的列设为null

注意:子表的外键列不能为not null

3. 修改表

1.什么时候会用到修改表?(一般不会常见)

创建项目之前已创建了表

项目开发、运行过程中

2.修改表语句

alter table 表名 add —— 添加字段

alter table 表名 add 字段名 数据类型(宽度) 约束 first/after name

alter table 表名 drop —— 删除字段

alter table 表名 drop 字段名;

alter table 表名 modify —— 修改已经存在的字段 的类型 宽度 约束

alter table 表名 modify name(字段名后面是修改的内容) varchar(12) not null

id name age

alter table 表名 modify age int not null after id; # 将age的位置修改到id后面

alter table 表名 modify age int not null first; # 将age的位置放在第一个

alter table 表名 change —— 修改已经存在的字段 的类型 宽度 约束 和 字段名字

alter table 表名 change name new_name varchar(12) not null

4. 表关系

两张表中的数据之间的关系:

1.多对一 :foreign key 永远是在多的那张表中设置外键

foreign key(多) references 表(一)

例:多个学生都是同一个班级的

​ 学生表 关联 班级表

​ 学生是多,班级是一

2.一对一 :foreign key +unique —— 后出现的后一张表中的数据作为外键,并且要约束这个外键类型是唯一的 unique

foreign key(后一) references 表(先一)

例:一个客户对应一个学生, 在学生表中创建外键

​ 一个商品 有一个商品详情 ,详情页中有外键

54d16c19f933d97781765ba05f2b64de.png

3.多对多 :产生第三张表,把两个关联关系的字段作为第三张表的外键

foreign key(外键名1) references 表1(主键)

foreign key(外键名2) references 表2(主键)

例:表一:一本书有多个作者

​ 表二:一个作者又写了多本书

09108a5ee3aa5fcb47faa5f6b4319ecd.png

5. 表数据的操作

1.增加 insert

1.insert into 表名 values (值....) —— 所有的在这个表中的字段都需要按照顺序被填写在这里

2.insert into 表名(字段名,字段名。。。) values (值....) —— 所有在字段位置填写了名字的字段和后面的值必须是一 一对应

3.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)

2.删除 delete

delete from 表 where 条件;

3.更新 update

update 表 set 字段=新的值 where 条件;

4.查询 select

表查询分为:单表查询 、多表查询

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值