MySQL 语言--DDL

2020年8月4日20:24:04

一、库、用户、权限、表

1、库

CREATE DATABASE test_01;  -- 创建数据库
DROP DATABASE test_01; -- 删除数据库
USE mysql; -- 切换数据库

2、用户、权限

CREATE USER 'test01'@'localhost' IDENTIFIED BY '1111';  --  创建用户
DROP USER test01@'localhost'; --删除用户
GRANT ALL PRIVILEGES ON  *.* TO 'test01'@'localhost'; -- 授权
									-- MySQL 5.7 版本  IDENTIFIED BY '1111'  可有可无 
									-- 8.0版本 加上报错
FLUSH PRIVILEGES; -- 刷新权限

3、表

-- 创建表 
CREATE TABLE t_student(
	id int(2),
	`name`  VARCHAR(20),
	sex char(2),
	course VARCHAR(20),
	score DECIMAL(18,2)
)DROP TABLE t_student; -- 删除表

二、alter 用法

1、更改表名

-- alter table 表名 rename 新表名;
 alter table teacher rename t_teacher;

2、更改字段名

-- alter table 表名 change 列名 新列名 数据类型;
alter table t_teacher change name teacher_name varchar(20);

3、 添加字段

-- alter table 表名 add 列名 数据类型
alter table t_teacher add birthday datetime; -- 默认添加到尾部
alter table t_teacher add birthday datetime after teacher_name; -- 把列添加到指定列的后面
alter table t_teacher add birthday datetime first; -- 添加到第一列

4、删除字段

-- alter table 表名 drop 列名;
alter table t_teacher drop birthday;

5、更改字段类型(尽量不要更改)

-- alter table 表名 modify 列名 新数据类型;
alter table t_teacher modify sex2 varchar(20);
-- 更改类型的同时,还能添加注释说明
-- alter table 表名 modify 列名 新数据类型 '该列的注释说明';

6、查看建表语句

-- show create tab 表名

三、约束

1、主键(primary key)

①主键设置

-- 创建表语句时,添加主键约束
	create table person (
		id int,
		name varchar(100),
		income decimal(18,2),
		primary key (id,name)
	); -- 添加两个主键
	create table person1(
		id int,
		name varchar(100),
		income decimal(18,2),
		primary key(id)
	); -- 添加一个主键
	create table person2(
		id int primary key,
		name varchar(100),
		income decimal(18,2)
	); -- 添加一个主键 也可以直接写在列后面
	
-- 创建表完成之后,通过alter 添加主键约束
	-- 语法:alter table 表名 add primary key (列名,列名···);
	create table person3(
		id int,
		name varchar(100),
		income decimal(18,2)
	); -- 创建表
	alter table person3 add primary key(id); -- 添加约束

②主键自增(auto_increment)

-- 建表时,添加自增
	create table person(
		id int auto_increment,
		`name` varchar(200),
		primary key(id)
	);
	
-- 创建表之后,添加自增
	-- alter table 表名 modify 主键列名 类型 auto_increment
	create table person(
		id int,
		`name` varchar(200),
		primary key(id)
	);
	alter table person modify id int auto_increment;
	
--设置自增起始值
	-- alter table 表名 auto_increment=值;
	create table person(
		id int auto_increment,
		`name` varchar(200),
		primary key(id)
	);
	alter table person auto_increment=10000;

2、外键(foreign key)

-- 创建表时,添加外键约束
	create table teacher(
		id int,
		`name` varchar(20),
		primary key(id)
	);
	create table student(
		id int,
		`name` varchar(20),
		teacher_id int,
		primary key(id),
		foreign key(teacher_id) references teacher(id)
	); -- 注意 !!引用student中添加外键列,指向teacher表,所以必须先创建teacher表才行
	
-- 创建完表之后,添加外键约束
	-- alter table 表名 add foreign key (外键列的列名) references 指向的表名 (主键列的列名)
	create table student(
		id int,
		`name` varchar(20),
		teacher_id int,
		primary key (id)
	);
	create table teacher(
		id int,
		`name` varchar(20),
		primary key(id)
	);
	alter table student add foreign key (teacher_id) references teacher(id);

3、唯一约束( unique)

不允许出现重复的值,但可以为多个null
-- 创建表时,添加 unique 约束
	create table temp(
		id int,
		`name` varchar(20),
		unique(id)
	);
	
-- 创建表之后,添加 unique 约束
	create table temp (
		id int,
		`name` varchar(20)
	);
	alter table temp add unique(id);

4、非空和默认值(not null 和 default)

-- 创建表时,添加约束
	create table temp (
		id int not null,
		`name` varchar(20) default 'abc',
		sex varchar(20) not null default '男'
	);
	
-- 创建表之后,添加约束
	-- alter table 表名 modify 列名 数据类型 not null default 默认值
	create table temp(
		id int,
		`name` varchar(20),
		sex varchar(10)
	);
	alter table temp modify id int not null;
	alter table temp modify `name` varchar(20) default 'abc';
	alter table temp modify sex varchar(10) not null default '男';
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值