约束名 | 关键字 | 含义 |
---|---|---|
非空约束 | not null | 值不能为空 |
唯一约束 | unique | 值不能重复 |
主键约束 | primary key | 值非空且唯一 |
外间约束 | foreign key | 让表于表产生关系,从而保证数据的正确性 |
create table 表名(
字段名 字段类型 not null -- 对该字段添加了非空约束
);
alter table 表名 modify 字段名 字段类型 not null;
alter table 表名 modify 字段名 字段类型;
create table 表名(
字段名 字段类型 unique
);
alter table 表名 modify 字段名 字段类型 unique;
alter table 表名 drop index 字段名;
注意:
1、一张表只能有一个字段为主键。
2、主键就是表中记录的唯一标识。
3、自增长只能用在主键上
create table 表名(
字段名 字段类型 primary key
);
alter table 表名 modify 字段名 字段类型 primary key;
alter table 表名 drop primary key;
create table 表名(
字段名 字段类型 primary key auto_increment
);
alter table 表名 modify 字段名 字段类型 auto_increment;
alter table 表名 modify 字段名 字段类型;
create table 表名(
字段名 字段类型 default 默认值
);
-- 部门表 和 员工表 (1 对 多的关系)
-- 部门表(主表:一方,用来约束别人的表)
create table department(
id int primary key auto_increment,
dep_name varchar(20),
dep_location varchar(20)
);
-- 员工表(从表:多方,被别人约束的表)
create table employee(
id int primary key auto_increment,
name varchar(20),
age int,
dep_id int, -- 外键对应主表的主键
-- 创建外键约束
foreign key (dep_id) references department(id)
)
create table 从表名(
外键字段名 字段类型, -- 外键对应主表的主键
[constraint] [外键名称] foreign key (外键列名称)
references 主表名称(主表列名称)
);
alter table 从表名 ADD CONSTRAINT 外键名称 FOREIGN KEY (外键字段名称) REFERENCES 主表名称(主表列名称);
alter table 表名 drop foreign key 外键名称;
注意:只能是创建表的时候创建级联关系。
-- 员工表
create table employee4(
id int primary key auto_increment,
name varchar(20),
age int,
dep_id int, -- 外键对应主表的主键
-- 创建外键约束
constraint emp_depid_fk2 foreign key (dep_id) references department(id) on update cascade on delete cascade
)
级联操作语法 | 描述 |
---|---|
on update cascade | 级联更新,更新主表中的主键,从表中的外键列也自动同步更新 |
on delete cascade | 级联删除 |
范式 | 特点 |
---|---|
1NF | 原子性:表中每列不可再拆分。 |
2NF | 不产生局部依赖,一张表只描述一件事情 |
3NF | 不产生传递依赖,表中每一列都直接依赖于主键。而不是通过其它列间接依赖于主键。 |
命令行方式:
图形化工具
看不到 join 关键字,条件使用 where 指定
select 字段名 from 表1, 表2 where 条件;
使用 inner join ... on 语句, 可以省略 inner
select 字段列表 from 表1 [inner] join 表2 on 条件;
查询的是左表所有数据以及其交集部分
select 字段列表 from 表1 left [outer] join 表2 on 条件;
查询的是右表所有数据以及其交集部分
select 字段列表 from 表1 right [outer] join 表2 on 条件;
查询中嵌套查询,称嵌套查询为子查询。
SELECT 查询字段 FROM 表 WHERE 字段=(子查询)
SELECT 查询字段 FROM 表 WHERE 字段 IN (子查询);
SELECT 查询字段 FROM (子查询) 表别名 WHERE 条件;
如果一个包含多个步骤的业务操作,被事务管理,那么这些操作要么同时成功,要么同时失败。
1、查看事务的默认提交方式:
SELECT @@autocommit; -- 1 代表自动提交 0 代表手动提交
2、修改默认提交方式:
set @@autocommit = 0;
功能 | SQL语句 |
---|---|
开启事务 | start transaction; |
提交事务 | commit; |
回滚事务 | rollback; |
多个事务之间隔离的,相互独立的。但是如果多个事务操作同一批数据,则会引发一些问题,设置不同的隔离级别就可以解决这些问题。
注意:隔离级别从小到大安全性越来越高,但是效率越来越低
select @@tx_isolation;
set global transaction isolation level 级别字符串;
1、切换到mysql数据库:use mysql;
2、查询user表:select * from user;
注:% 表示可以在任意主机使用用户登录数据库
create user '用户名'@'主机名' identified by '密码';
drop user '用户名'@'主机名';
update user set password = password('新密码') where user = '用户名';
set password for '用户名'@'主机名' = password('新密码');
1、管理员运行该cmd ---> net stop mysql --停止mysql服务
2、使用无验证方式启动mysql服务:mysqld --skip-grant-tables
3、打开新的cmd窗口,直接输入mysql命令,敲回车。就可以登录成功
4、use mysql
5、update user set password = password('你的新密码') where user = 'root';
6、关闭两个窗口
7、打开任务管理器,手动结束mysqld.exe 的进程
8、启动mysql服务,使用新密码登录
show grants for '用户名'@'主机名';
grant 权限列表 on 数据库名.表名 to '用户名'@'主机名';
-- 给用户授予所有权限,在任意数据库的任意表上
GRANT ALL ON *.* TO '用户名'@'主机名';
revoke 权限列表 on 数据库名.表名 from '用户名'@'主机名';
联系客服