Python学习笔记——mysql数据库建立关系表与连接查询

本文介绍了如何使用Python操作MySQL数据库,创建包含外键的关系表,如学生、科目和成绩表,并详细讲解了INNER JOIN、LEFT JOIN和RIGHT JOIN查询的用法,以实现不同表间的数据联合查询。
摘要由CSDN通过智能技术生成

建立关系表

假如我们现在要建立三张表
学生表(students):
id
name
gender
科目表(subjects):
id
title
成绩表(scores):
id
score
stutid(外键)
subid(外键)


create database stu chartset=utf8;
use stu;
create table students(int id auto_increment primary key,name varchar(10) not null,gender bit default 1 )
create table subjects(int id auto_increment primary key,title varchar(10) not null)
create table scores(int id auto_increment primary key,score decimal(4,1) not null,stuid int,subid int,foreign key(stuid) references students(id),foreign key(subid) references subjects(id));

最有一个分数表的建立,也可以先建立表,然后在添加外键
建立表:create table scores(int id auto_increate primary key not null,score decimal(4,1) not null);
添加外键:alter table scores add

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值