python 数据库表操作

语法:
1. 修改表名
      ALTER TABLE 表名
                      RENAME 新表名;

2. 增加字段
      ALTER TABLE 表名
                      ADD 字段名  数据类型 [完整性约束条件…],
                      ADD 字段名  数据类型 [完整性约束条件…];

3. 删除字段
      ALTER TABLE 表名
                      DROP 字段名;

4. 修改字段
      ALTER TABLE 表名
                      MODIFY  字段名 数据类型 [完整性约束条件…];
      ALTER TABLE 表名
                      CHANGE 旧字段名 新字段名 旧数据类型 [完整性约束条件…];
      ALTER TABLE 表名
                      CHANGE 旧字段名 新字段名 新数据类型 [完整性约束条件…];

5.修改字段排列顺序/在增加的时候指定字段位置
    ALTER TABLE 表名
                     ADD 字段名  数据类型 [完整性约束条件…]  FIRST;
    ALTER TABLE 表名
                     ADD 字段名  数据类型 [完整性约束条件…]  AFTER 字段名;
    ALTER TABLE 表名
                     CHANGE 字段名  旧字段名 新字段名 新数据类型 [完整性约束条件…]  FIRST;
    ALTER TABLE 表名
                     MODIFY 字段名  数据类型 [完整性约束条件…]  AFTER 字段名;

alter操作非空和唯一(了解)

create table t(id int unique,name char(10) not null);

#去掉null约束
alter table t modify name char(10) null;
# 添加null约束
alter table t modify name char(10) not null;

# 去掉unique约束
alter table t drop index id;
# 添加unique约束
alter table t modify id int unique;

alter处理null和unique约束
alter操作主键(了解)1、首先创建一个数据表table_test:
create table table_test(
`id` varchar(100) NOT NULL,
`name` varchar(100) NOT NULL,
PRIMARY KEY (`name`)
);
2、如果发现主键设置错了,应该是id是主键,但如今表里已经有好多数据了,不能删除表再重建了,仅仅能在这基础上改动表结构。
先删除主键
alter table table_test drop primary key;
然后再增加主键
alter table table_test add primary key(id);
注:在增加主键之前,必须先把反复的id删除掉。

为表添加外键(了解)

创建press表
CREATE TABLE `press` (
  `id` int(11) NOT NULL,
  `name` char(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ;

创建book表
CREATE TABLE `book` (
  `id` int(11) DEFAULT NULL,
  `bk_name` char(12) DEFAULT NULL,
  `press_id` int(11) NOT NULL,
  KEY `press_id` (`press_id`)
) ;

为book表添加外键
alter table book add constraint fk_id foreign key(press_id) references press(id);

删除外键
alter table book drop foreign key fk_id;

示例

mysql> desc staff_info;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(11)               | YES  |     | NULL    |       |
| name  | varchar(50)           | YES  |     | NULL    |       |
| age   | int(3)                | YES  |     | NULL    |       |
| sex   | enum(‘male‘,‘female‘) | YES  |     | NULL    |       |
| phone | bigint(11)            | YES  |     | NULL    |       |
| job   | varchar(11)           | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
rows in set (0.00 sec)

# 表重命名
mysql> alter table staff_info rename staff;
Query OK, 0 rows affected (0.00 sec)

mysql> desc staff;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(11)               | YES  |     | NULL    |       |
| name  | varchar(50)           | YES  |     | NULL    |       |
| age   | int(3)                | YES  |     | NULL    |       |
| sex   | enum(‘male‘,‘female‘) | YES  |     | NULL    |       |
| phone | bigint(11)            | YES  |     | NULL    |       |
| job   | varchar(11)           | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
rows in set (0.00 sec)

# 删除sex列
mysql> alter table staff drop sex;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc staff;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(50) | YES  |     | NULL    |       |
| age   | int(3)      | YES  |     | NULL    |       |
| phone | bigint(11)  | YES  |     | NULL    |       |
| job   | varchar(11) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
rows in set (0.01 sec)

# 添加列
mysql> alter table staff add sex enum(‘male‘,‘female‘);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

# 修改id的宽度
mysql> alter table staff modify id int(4);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc staff;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(4)                | YES  |     | NULL    |       |
| name  | varchar(50)           | YES  |     | NULL    |       |
| age   | int(3)                | YES  |     | NULL    |       |
| phone | bigint(11)            | YES  |     | NULL    |       |
| job   | varchar(11)           | YES  |     | NULL    |       |
| sex   | enum(‘male‘,‘female‘) | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
rows in set (0.01 sec)

# 修改name列的字段名
mysql> alter table staff change name sname varchar(20);
Query OK, 4 rows affected (0.03 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> desc staff;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(4)                | YES  |     | NULL    |       |
| sname | varchar(20)           | YES  |     | NULL    |       |
| age   | int(3)                | YES  |     | NULL    |       |
| phone | bigint(11)            | YES  |     | NULL    |       |
| job   | varchar(11)           | YES  |     | NULL    |       |
| sex   | enum(‘male‘,‘female‘) | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
rows in set (0.00 sec)

# 修改sex列的位置
mysql> alter table staff modify sex enum(‘male‘,‘female‘) after sname;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc staff;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(4)                | YES  |     | NULL    |       |
| sname | varchar(20)           | YES  |     | NULL    |       |
| sex   | enum(‘male‘,‘female‘) | YES  |     | NULL    |       |
| age   | int(3)                | YES  |     | NULL    |       |
| phone | bigint(11)            | YES  |     | NULL    |       |
| job   | varchar(11)           | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
rows in set (0.00 sec)

# 创建自增id主键
mysql> alter table staff modify id int(4) primary key auto_increment;
Query OK, 4 rows affected (0.02 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> desc staff;
+-------+-----------------------+------+-----+---------+----------------+
| Field | Type                  | Null | Key | Default | Extra          |
+-------+-----------------------+------+-----+---------+----------------+
| id    | int(4)                | NO   | PRI | NULL    | auto_increment |
| sname | varchar(20)           | YES  |     | NULL    |                |
| sex   | enum(‘male‘,‘female‘) | YES  |     | NULL    |                |
| age   | int(3)                | YES  |     | NULL    |                |
| phone | bigint(11)            | YES  |     | NULL    |                |
| job   | varchar(11)           | YES  |     | NULL    |                |
+-------+-----------------------+------+-----+---------+----------------+
rows in set (0.00 sec)

# 删除主键,可以看到删除一个自增主键会报错
mysql> alter table staff drop primary key;
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key

# 需要先去掉主键的自增约束,然后再删除主键约束
mysql> alter table staff modify id int(11);
Query OK, 4 rows affected (0.02 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> desc staff;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(11)               | NO   | PRI | 0       |       |
| sname | varchar(20)           | YES  |     | NULL    |       |
| sex   | enum(‘male‘,‘female‘) | YES  |     | NULL    |       |
| age   | int(3)                | YES  |     | NULL    |       |
| phone | bigint(11)            | YES  |     | NULL    |       |
| job   | varchar(11)           | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
rows in set (0.01 sec)

mysql> alter table staff drop primary key;
Query OK, 4 rows affected (0.06 sec)
Records: 4  Duplicates: 0  Warnings: 0

# 添加联合主键
mysql> alter table staff add primary key (sname,age);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

# 删除主键
mysql> alter table staff drop primary key;
Query OK, 4 rows affected (0.02 sec)
Records: 4  Duplicates: 0  Warnings: 0

# 创建主键id
mysql> alter table staff add primary key (id);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc staff;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(11)               | NO   | PRI | 0       |       |
| sname | varchar(20)           | NO   |     |         |       |
| sex   | enum(‘male‘,‘female‘) | YES  |     | NULL    |       |
| age   | int(3)                | NO   |     | 0       |       |
| phone | bigint(11)            | YES  |     | NULL    |       |
| job   | varchar(11)           | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
rows in set (0.00 sec)

# 为主键添加自增属性
mysql> alter table staff modify id int(4) auto_increment;
Query OK, 4 rows affected (0.02 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> desc staff;
+-------+-----------------------+------+-----+---------+----------------+
| Field | Type                  | Null | Key | Default | Extra          |
+-------+-----------------------+------+-----+---------+----------------+
| id    | int(4)                | NO   | PRI | NULL    | auto_increment |
| sname | varchar(20)           | NO   |     |         |                |
| sex   | enum(‘male‘,‘female‘) | YES  |     | NULL    |                |
| age   | int(3)                | NO   |     | 0       |                |
| phone | bigint(11)            | YES  |     | NULL    |                |
| job   | varchar(11)           | YES  |     | NULL    |                |
+-------+-----------------------+------+-----+---------+----------------+
rows in set (0.00 sec)

多表结构的创建与分析


如何找出两张表之间的关系 

分析步骤:
#1、先站在左表的角度去找
是否左表的多条记录可以对应右表的一条记录,如果是,则证明左表的一个字段foreign key 右表一个字段(通常是id)

#2、再站在右表的角度去找
是否右表的多条记录可以对应左表的一条记录,如果是,则证明右表的一个字段foreign key 左表一个字段(通常是id)

#3、总结:
#多对一:
如果只有步骤1成立,则是左表多对一右表
如果只有步骤2成立,则是右表多对一左表

#多对多
如果步骤1和2同时成立,则证明这两张表时一个双向的多对一,即多对多,需要定义一个这两张表的关系表来专门存放二者的关系

#一对一:
如果1和2都不成立,而是左表的一条记录唯一对应右表的一条记录,反之亦然。这种情况很简单,就是在左表foreign key右表的基础上,将左表的外键字段设置成unique即可

建立表之间的关系

#一对多或称为多对一
三张表:出版社,作者信息,书

一对多(或多对一):一个出版社可以出版多本书sql示例

关联方式:foreign key
=====================多对一=====================
create table press(
id int primary key auto_increment,
name varchar(20)
);

create table book(
id int primary key auto_increment,
name varchar(20),
press_id int not null,
foreign key(press_id) references press(id)
on delete cascade
on update cascade
);

insert into press(name) values
(‘北京工业地雷出版社‘),
(‘人民音乐不好听出版社‘),
(‘知识产权没有用出版社‘)
;

insert into book(name,press_id) values
(‘九阳神功‘,1),
(‘九阴真经‘,2),
(‘九阴白骨爪‘,2),
(‘独孤九剑‘,3),
(‘降龙十巴掌‘,2),
(‘葵花宝典‘,3)
;
#多对多
三张表:出版社,作者信息,书

多对多:一个作者可以写多本书,一本书也可以有多个作者,双向的一对多,即多对多
  
关联方式:foreign key+一张新的表
=====================多对多=====================
create table author(
id int primary key auto_increment,
name varchar(20)
);

#这张表就存放作者表与书表的关系,即查询二者的关系查这表就可以了
create table author2book(
id int not null unique auto_increment,
author_id int not null,
book_id int not null,
constraint fk_author foreign key(author_id) references author(id)
on delete cascade
on update cascade,
constraint fk_book foreign key(book_id) references book(id)
on delete cascade
on update cascade,
primary key(author_id,book_id)
);

#插入四个作者,id依次排开
insert into author(name) values(‘egon‘),(‘alex‘),(‘yuanhao‘),(‘wpq‘);

#每个作者与自己的代表作如下
egon:
九阳神功
九阴真经
九阴白骨爪
独孤九剑
降龙十巴掌
葵花宝典
alex:
九阳神功
葵花宝典
yuanhao:
独孤九剑
降龙十巴掌
葵花宝典
wpq:
九阳神功

insert into author2book(author_id,book_id) values
(1,1),
(1,2),
(1,3),
(1,4),
(1,5),
(1,6),
(2,1),
(2,6),
(3,4),
(3,5),
(3,6),
(4,1)
;
#一对一
两张表:学生表和客户表

一对一:一个学生是一个客户

关联方式:foreign key+unique
create table customer(
    -> id int primary key auto_increment,
    -> name varchar(20) not null,
    -> qq varchar(10) not null,
    -> phone char(16) not null
    -> );

create table student(
    -> id int primary key auto_increment,
    -> class_name varchar(20) not null,
    -> customer_id int unique, #该字段一定要是唯一的
    -> foreign key(customer_id) references customer(id) #外键的字段一定要保证unique
    -> on delete cascade
    -> on update cascade
    -> );

#增加客户
mysql> insert into customer(name,qq,phone) values
    -> (‘韩蕾‘,‘31811231‘,13811341220),
    -> (‘杨澜‘,‘123123123‘,15213146809),
    -> (‘翁惠天‘,‘283818181‘,1867141331),
    -> (‘杨宗河‘,‘283818181‘,1851143312),
    -> (‘袁承明‘,‘888818181‘,1861243314),
    -> (‘袁清‘,‘112312312‘,18811431230)

mysql> #增加学生
mysql> insert into student(class_name,customer_id) values
    -> (‘脱产1班‘,3),
    -> (‘周末1期‘,4),
    -> (‘周末1期‘,5)
    -> ;

sql示例

原文地址:https://www.cnblogs.com/shaohuagu/p/12299770.html

时间: 2024-08-01 07:01:16

python 数据库表操作的相关文章

Python/MySQL表操作以及连接

Python/MySQL表操作以及连接 mysql表操作: 主键:一个表只能有一个主键.主键可以由多列组成. mysql> create table yuan(id int auto_increment,yuangongname int,bumen_id int, primary key(id,yuangongname))engine=innodb default charset=utf8; Query OK, 0 rows affected (0.43 sec) 外键 :可以进行联合外键,操作

mysql数据库表操作及授权

表操作:增删改查 把/etc/passwd文件的内容导入 passwd表里. mysql>load data infile"/etc/passwd" into table passwd fields terminated by ":"; 基于前面的passwd表,完成下列操作: 1:列出uid低于500且3个字母的用户 mysql> select name from passwd where uid<500 and name like "

python数据库(mysql)操作

一.软件环境 python环境默认安装了sqlite3,如果需要使用sqlite3我们直接可以在python代码模块的顶部使用import sqlite3来导入该模块.本篇文章我是记录了python操作mysql数据库,mysql数据库下载 由于我之前安装的是wampserver,默认安装了php.mysql和apache这3个环境,因此我没有在单独安装mysql数据库,只是下载了一个mysql管理工具Navicat for MySQL.在使用Navicat for MySQL连接本地mysql

Mysql1:数据库表操作,增删改查举例

数据库表的相关操作 添加数据库表 语法:  实例: 查看数据库表 语法: show tables; 实例: 查看数据库表结构 1)使用DESCRIBE/DESC  语法: 实例:  2)SHOW CREATE TABLE 语法:  实例: 使用此方法查看数据库表结构时,不仅可以查看表创建时候的详细语句,而且还可以查看存储引擎和字符编码.  修改表名 语法: to为可选参数,使用与否均不影响 实例: 将user_info表改为user_data 修改字段的数据类型 语法:  实例: 将id 的数据

2、数据库和数据库表操作

一.(dos下)数据库的创建.删除.查看和使用 1.1 普通方式登录后再使用数据库 1 Microsoft Windows [版本 6.1.7601] 2 版权所有 (c) 2009 Microsoft Corporation.保留所有权利. 3 4 C:\Users\Dell>cd c: 5 6 c:\>d: 7 8 D:\>cd D:\Program Files\MySQL\MySQL Server 5.0\bin 9 10 D:\Program Files\MySQL\MySQL

4.4 - 数据库 - 表操作练习题

表操作练习题:练习:用户表,用户组表,主机表,业务线表 关联:用户与用户组 主机与业务线 用户与主机 # 用户表create table user( id int not null unique auto_increment, username varchar(20) not null, password varchar(50) not null, primary key(username,password));insert into user(username,password) values

Python 数据库骚操作 — MongoDB

前言 MongoDB GUI 工具 PyMongo(同步) Motor(异步) 后记 前言 最近这几天准备介绍一下 Python 与三大数据库的使用,这是第一篇,首先来介绍 MongoDB 吧,这里介绍 MongoDB 的两款操作库,走起!! MongoDB GUI 工具 首先介绍一款 MongoDB 的 GUI 工具 Robo 3T,初学 MongoDB 用这个来查看数据真的很爽.可以即时看到数据的增删改查,不用操作命令行来查看. PyMongo(同步) PyMongo 是一个同步操作的数据存

Mysql 数据库表操作

1 创建表: create table table_name ( filed 字段类型 [完整性约束], filed 字段类型 [完整性约束], filed 字段类型 [完整性约束], ): 2 查看表 desc table_name; 查看表结构 show tables: 是该数据库下所有的表名 show create table table_name : 查看数据库的表创建信息 3 修改表: alter table table_name add field type 完整性约束 alter

数据库表操作时出去死锁或卡主,最好的解决方法。。。。

就是利用可视化工具,先复制这张表,然后在删除卡死的那张表,最后将复制的表重命名即可. 出现卡死的原因可能是: 1.频繁的对某张表的字段进行操作,比如修改他的大小或数据类型啥的,可能就会导致出现锁表或卡死的状态. 2.若发现对某张表进行的某个字段进行操作时,卡死了,先去试试其他字段,或者其他表的字段看可不可以修改,若可以修改,怎就按照上面的办法,先复制,在删除,最后重命名. 还有一种方法: 利用可视化工具Navicat,按F6进行命令行,然后使用命令查看进程: >show full process