alter table `userinfo` add `test01` varchar(11) NOT NULL COMMENT '修改表结构' FIRST; # 在第一行新增字段
alter table `userinfo` add `test02` varchar(11) NOT NULL COMMENT '修改表结构' after name; # 在name字段后面添加字段
alter table `userinfo` drop test01; 删除字段
ALTER TABLE `hong`.`honghong` ENGINE = MyISAM; #修改表的引擎为MYISAM
ALTER TABLE `hong`.`honghong` ENGINE = InnoDB;
DROP TABLE IF EXISTS `dept`; # 如果表存在则删除
# 创建数据库表 注意最后一行不加,号,单引号为 反引号
create table student2(
id int auto_increment primary key ,
name char(32) not null,
age int not null default 0,
register_date date not null
)engine=innodb default charset=utf8;
# innodb支持事务,charset设置默认字符集,(这些只要在mysql配置文件配置好,默认就是,不需要写)
# 一张表只能有一个主键,一个主键key可以有多列
default 0 # 默认值
not null # 不可为空
null # 可以为空
auto_increment : 自增,
primary key 主键约束 (不能重复,且不能为空),加速查找,主键key 每个表只能定义一个主键,主键值必须唯一标识表中的每一行
#对于自增段扩展知识 AUTO_INCREMENT
delete from student; #清空表数据 后再添加数据,id从原来的基础上自增
truncate from student; #清空表数据 后再添加数据,id从 1 自增
Create Table: CREATE TABLE `t1` ( # 显示数据表创建过程
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` char(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8
alter table t1 auto_increment=20; # 手动修改自增开始序号位置,当再次插入的时候从设置点开始添加
# 手动设置会话步长和全局步长
show session variables like 'auto_inc%'; #查看变量
set session auto_increment_increment=2; # 设置会话步长
show global variables like 'auto_inc%'; # 查看全局变量
set global auto_increment_increment=2; # 设置全局会话步长
# 外键(对应Django的一对多)
# 注意:两个表关联,如果id被设置了unsigned 则 外键也需要设置unsigned(无符号)
create table stu(
id int auto_increment primary key,
name char(32) not null ,
age int not null,
register_date date not null,
dept_id int not null,
constraint fk_user_dept foreign key (`dept_id`) references dept(`id`)
)engine=innodb default charset=utf8;
create table dept(
id int auto_increment primary key,
title char(15) not null
)engine=innodb default charset=utf8;
# 外键(再加唯一索引)(对应Django的一对一)
create table userinfo1(
id int auto_increment primary key,
name char(10),
gender char(10),
email varchar(64)
)engine=innodb default charset=utf8;
create table admin(
id int not null auto_increment primary key,
username varchar(64) not null,
password VARCHAR(64) not null,
user_id int not null,
unique uq_u1 (user_id), #加唯一索引
CONSTRAINT fk_admin_u1 FOREIGN key (user_id) REFERENCES userinfo1(id)
)engine=innodb default charset=utf8;
# 多对多(联合唯一索引,创建第三张表)
create table userinfo2(
id int auto_increment primary key,
name char(10),
gender char(10),
email varchar(64)
)engine=innodb default charset=utf8;
create table host(
id int auto_increment primary key,
hostname char(64)
)engine=innodb default charset=utf8;
create table user2host(
id int auto_increment primary key,
userid int not null,
hostid int not null,
unique uq_user_host (userid,hostid), # 联合唯一索引
CONSTRAINT fk_u2h_user FOREIGN key (userid) REFERENCES userinfo2(id),
CONSTRAINT fk_u2h_host FOREIGN key (hostid) REFERENCES host(id)
)engine=innodb default charset=utf8;
#--------------------------------下面是两种创建方式(暂时没搞懂)------------------------------
DROP TABLE IF EXISTS `dept`;
create table dept(
id int auto_increment,
title char(15) not null,
person_count int not null,
PRIMARY KEY (`id`) USING BTREE
)engine=innodb default charset=utf8;
ALTER TABLE `dept`ADD INDEX(`id`, `person_count`);
DROP TABLE IF EXISTS `stu`;
create table stu(
id int auto_increment,
name char(32) not null ,
age int not null,
register_date date not null,
dept_id int not null,
count_id int not null,
PRIMARY KEY (`id`) USING BTREE,
CONSTRAINT `fk_sut_dept` FOREIGN KEY (`dept_id`, `count_id`) REFERENCES `dept` (`id`, `person_count`)
)engine=innodb default charset=utf8;
DROP TABLE IF EXISTS `dept`;
create table dept(
id int auto_increment,
title char(15) not null,
person_count int not null,
PRIMARY KEY (`id`,`person_count`) USING BTREE
)engine=innodb default charset=utf8;
DROP TABLE IF EXISTS `stu`;
create table stu(
id int auto_increment,
name char(32) not null ,
age int not null,
register_date date not null,
dept_id int not null,
count_id int not null,
PRIMARY KEY (`id`) USING BTREE,
CONSTRAINT `fk_sut_dept` FOREIGN KEY (`dept_id`, `count_id`) REFERENCES `dept` (`id`, `person_count`)
)engine=innodb default charset=utf8;
#-------------------------------------------------------------------------------------
外键约束的修改更新状态(CASCADE,RESTRICT,NO ACTION,SET NULL )
cascade: 级联,当父表更新、删除,子表会同步更新和删除
set null: 置空,当父表更新、删除的时候,字表会把外键字段变为null,所以这个时候设计表的时候该字段要允许为null,否则会出错
restrict: 父表在删除和更新记录的时候,要在子表中检查是否有有关该父表要更新和删除的记录,如果有,则不允许删除个更改
no action: 和restrict一样