一. 复习
1. MySQL:
- 服务端
- 客户端
2. 通信交流
- 授权
- SQL语句
- 数据库
创建数据库:
create database db1 default charset utf8;
删除数据库:
drop database db1;
- 数据表
创建数据表:
create table tb1(
id int not null auto_increment primary key,
name char(10),
depatment_id int,
constraint fk_l foreign key ('department_id') references 表名('tid')
)engine = innodb default charset = utf8;
- 数据行
增:insert into tb1(name,age) values('alex','18');
删:delete from tb1;
delete from tb1 where id > 10
truncate table tb1;
改:update tb1 set name = 'root' where > 10
查:select * from tb; # * 的效率低
select id,name from tb;
二. 外键的补充
1. 主键:
- 一张表 只能有 一个主键
- 一个主键 不一定 是一列
主键可以这样写:
create table t1(
nid int(11) not null auto_increment,
pid int(11) not null,
num int(11) null,
primery key (nid,pid) # 主键有两列,不常用
)engine = innodb default charset = utf8;
# 主键设置成两列,外键就可以关联两列
create table t2(
id int auto_increment primary key,
name varchar(32),
id1 int,
id2 int,
constraint fk_t1_t2 foreign key ('id1','id2') references t1('nid','pid')
)engine = innodb default charset = utf8;
create table t1(
nid int(11) not null auto_increment,
pid int(11) not null,
num int(11) null,
primery key (nid,pid) # 主键有两列,不常用
)engine = innodb default charset = utf8;
三. 自增列之起始值
show create table t1 # 查看怎么创建的(代码)
show create table t1 \G # 查看怎么创建的(代码)
create table t1(
nid int(11) not null auto_increment,
pid int(11) not null,
num int(11) null,
primery key (nid,pid) # 主键有两列,不常用
)engine = innodb auto_increment = 4 default charset = utf8; # 设置auto_increment默认值
alter table t1 auto_increment = 1 # 修改自增的初始值
四. 自增列之步长
1. MySQL:自增步长
基于会话级别:
登录一次可以设置一个步长,不能像sqlServer一样(可以在每个表里面设置步长)
show session variables like 'auto_inc%'; # 查看步长(auto_increment_increment)
set session auto_increment_increment = 2; # 设置会话步长
基于全局级别:(尽量不用)
show global variables like 'auto_inc%'; # 查看全局变量
set global auto_increment_increment = 2;
更改之后,打开一个会话就会默认使用更改之后的步长
2. sqlServer:自增步长
基于表级别:
create table t1(
nid int(11) not null auto_increment,
pid int(11) not null,
num int(11) null,
primery key (nid,pid)
)engine = innodb auto_increment = 4 步长 = 2 default charset = utf8;
create table t2(
nid int(11) not null auto_increment,
pid int(11) not null,
num int(11) null,
primery key (nid,pid)
)engine = innodb auto_increment = 4 步长 = 2 default charset = utf8;
python学习之老男孩python全栈第九期_数据库day001知识点总结 —— MySQL数据库day2
猜你喜欢
转载自www.cnblogs.com/YD2018/p/9434319.html
今日推荐
周排行