


主键:可唯一表示该资料(可以设置多个列表为主键)

设置外键进行表与表的相连,且外键必须是其他表的主键(外键也可以设置自己表格的主键)
–为注释
;为结束命令的格式

MySQL的资料形态:
create table student(
`student_id` int primary key, — 第一列
`name` varchar(20), — 第二列
`major` varchar(20) — 第三列,20指的是最大字符长度
); — 创建表格并设计属性
describe `student`; — 展示表格
drop table `student`; — 删除表格
alter table `student` add gpa decimal(3,2); — 增加资料属性
alter table `student` drop column gpa ; — 删除资料属性

insert into `student` values(1,’小白’,’历史’); — 写入表格数据
insert into `student` values(2,’小黑’,’生物’); — 写入表格数据
insert into `student` values(3,’小绿’,null); — 写入表格数据,null为空
insert into `student`(`name`,`major`,`student_id`) values(‘小蓝’,’英语’,’4′); — 按照设置写入表格数据
insert into `student`(`major`,`student_id`) values(‘英语’,’5′); — 按照设置写入表格数据,没有的数据则为空白null

create table student(
`student_id` int primary key auto_increment, — 第一列,auto_increment自动会加一
`name` varchar(20), — 第二列,not null指这个属性不可以为空
`major` varchar(20) default ‘历史’ — 第三列,20指的是最大字符长度,default指预设值,如果没有写该属性,则为预设值
); — 创建表格并设计属性
drop table `student`;
select * from `student`; — 搜索表格的全部资料
insert into `student`(`name`,`major`) values(‘小蓝’,’英语’); — 按照设置写入表格数据
insert into `student`(`name`) values(‘小黑’); — 按照设置写入表格数据

条件:>,<,>=,<=,=,<>
set sql_safe_updates = 0; — 把预设更新模式关闭,这样更新操作才可以成功insert into `student`(`name`,`major`) values(‘小蓝’,’英语’); — 按照设置写入表格数据
insert into `student`(`name`,`major`) values(‘小白’,’化学’); — 按照设置写入表格数据
insert into `student`(`name`,`major`) values(‘小黑’,’生物’); — 按照设置写入表格数据
update `student` — 更新哪个表格
set `major` = ‘英语文学’ — 将什么更新成什么
where `major` = ‘英语’; — 将其中谁的什么进行更新
— 还可以进行多个更新
update `student` — 更新哪个表格
set `major` = ‘生化’ — 将什么更新成什么
where `major` = ‘生物’ or `major` = ‘化学’ ; — 将其中谁的什么进行更新
update `student` — 更新哪个表格
set `name` = ‘小辉’,`major` = ‘生化’ — 将什么更新成什么
where `student_id`=1 ; — 将其中谁的什么进行更新
— 不加条件则都改
update `student` — 更新哪个表格
set `major` = ‘物理’; — 将其中谁的什么进行更新,都改成了生化
delete from `student`
where `student_id` = 3; — 删除表格中的数据
— 条件也可以设置多个
delete from `student`
where `name` = ‘小白’ and `major`=’物理’; — 删除表格中的数据
delete from `student`; — 删除所有的资料
create table `employee`(
`emp_id` int primary key, — 第一列
`name` varchar(20), — 第二列,20指的是最大字符长度
`bath_date` date, — 第三列
`sex` varchar(1),
`salary` int,
`branch_id` int,
`sup_id` int
); — 创建表格并设计属性
create table `branch`(
`branch_id` int primary key , — 第一列
`branch_name` varchar(20), — 第二列
`manager_id` int, — 第三列,20指的是最大字符长度
foreign key (`manager_id`) references `employee`(`emp_id`) on delete set null — 设置好外键(选择什么是并对应什么表格的什么属性)
); — 创建表格并设计属性
— 补充外键(外表格对应)
alter table `employee` — 在什么表格上进行更新
add foreign key(`branch_id`) — 在他的什么属性上
references `branch`(`branch_id`) — 从哪的什么属性对应
on delete set null;
— 补充外键(内表格对应)
alter table `employee` — 在什么表格上进行更新
add foreign key(`sup_id`) — 在他的什么属性上
references `employee`(`emp_id`) — 从哪的什么属性对应
on delete set null;
create table `client`(
`client_id` int primary key , — 第一列
`client_name` varchar(20), — 第二列
`phone` varchar(20) — 第三列,20指的是最大字符长度
); — 创建表格并设计属性
create table `works_with`(
`emp_id` int, — 第一列
`client_id` int, — 第二列
`total_sales` int, — 第三列,20指的是最大字符长度
primary key(`emp_id`,`client_id`),
foreign key (`emp_id`) references `employee`(`emp_id`) on delete cascade, — 设置好外键(选择什么是并对应什么表格的什么属性)
foreign key (`client_id`) references `client`(`client_id`) on delete cascade — 设置好外键(选择什么是并对应什么表格的什么属性)
); — 创建表格并设计属性
— 当增加资料冲突的时候可以先将其设置为null然后再更新
insert into `branch` values(1,’研发’,null);
insert into `employee` values(206,’xiaohuang’,’1998-10-08′,’F’,50000,1,null);
update `branch`
set `manager_id` = 206
where `branch_id` = 1;
`branch_id` int primary key , — 第一列注:当为主键时则不能设置为on delete set nullcreate table `branch`(
# 导入功能包(mysql-connector-python)
import mysql.connector# 创入连线
connection = mysql.connector.connect(host='localhost', # mysql的位置
port='3306', # 连接通道
user='root', # 使用者名称
password='123456') # 使用者密码
'''connection = mysql.connector.connect(host='localhost', # mysql的位置
port='3306', # 连接通道
user='root', # 使用者名称
password='123456', # 使用者密码
database='sql_tutorial') # 直接打开目标资料库'''# 告知开始使用操作
cursor = connection.cursor()
# 在对mysql操作时即用该格式:cursor.execute("你要执行的mysql语句命令")# 创建资料库
# cursor.execute("CREATE DATABASE `qq`;")# 取得所有资料库名称
cursor.execute("show databases;")
records = cursor.fetchall() # 取出回传资料库
for r in records:
print(r) # 因为回传列表,为便于观察,进行循环打印# 选择资料库
cursor.execute("use `sql_tutorial`;")# 创建表格
# 告知关闭操作
cursor.close()
# 若要懂资料进行修改需要结尾加一个指令,这样才能提交指令生效
connection.commit()
# 关闭连线
connection.close()到此这篇关于MySQL操作并使用Python进行连接的文章就介绍到这了,更多相关MySQL操作 Python连接内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!