数据表,相当于数据库(文件夹)下的文件

数据表,相当于数据库(文件夹)下的文件 import pymysql#数据表相当于文件夹下的文件conn pymysql.connect(host127.0.0.1 , port3306 , userroot , passwdroot123 , charsetutf-8)cursor conn.cursor()cursor.execute(create databases db3 default charset utf-8 collate utf-8_general_ci)cursor.execute(use db3)#先查看表里有什么cursor.execute(show tables)print(cursor.fetchall())create table tb1(id int primary key, #主键不允许为空不能重复name varchar(16) not null, #不允许为空email varchar(32) null, #允许为空默认age int default 3 #插入数据时如果不给age列设置值默认为3)default charset utf-8#查看表desc tb2create table tb2(id int not null auto_increment primary key #主键不允许为空不能重复不为空自增)default charset utf-8#一个表只能有一个自增列一般是主键#删除表 drop table 表名drop table tb1#清空表 delete from 表名delete from tb2#修改表添加列删除列修改列删除列#添加列alter table tb2 add name type#删除列alter table tb2 drop column name#修改列 类型alter table tb2 modify column name type#修改列 类型名称alter table tb2 change before_name new_name new_type#修改列 默认值alter table tb2 alter name set default 1000#添加主键alter table tb2 add primary key(name)#删除主键alter table tb2 drop priamry key