DDL的使用

创建数据库:

1.概念:

database: 数据库---->DB

relational database management system ----> 数据库管理系统—>RDBMS

database administrator —> 数据库管理员–>DBA

database system ----> 数据库系统---->DBS(包含了DBS,DBMS,DBA)

DDL:数据定义语言—>create /drop/alter

DML:数据操作语言—> insert / delete / update

DQL:数据查询语言—> select

DCL:数据控制语言—>grant / revoke

2.命令:

清除之前的输入: create database 输入很多但是不想删除,在后面加上一个  \c

DDL(数据定义语言):
以前的版本字符集为latin1
* 创建数据库: create database school default charset utf8mb4;
* 创建数据库: create database school default character set utf8mb4;
* 创建不一样的数据库: create database  ` database` default character set utf8mb4;
* 删除数据库: drop database if exists  school;
* 修改数据库默认字符集: alter database school default charset utf8mb4;

查看刚才新建的数据库;show create database school;
使用刚才创建的数据库: use school;
查看数据库中的内容: show tables \G;

# 建表:
create table tb_student
(
stu_id int not null comment '学号',
stu_name varchar(20) not null default 1 comment '姓名',
stu_sex char(1) not null default '男' comment '性别',
stu_birth date not null default '2001-01-01' comment '出生日期',
primary key (stu_id)
)engine=innodb comment='学生表';

# 向二维表中添加数据:
alter table tb_student add column stu_tel varchar(20) comment '电话号码';

# 查看表的结构
desc tb_student;

# 删除表:
drop table if exists tb_student;

# 修改表
	# 添加列: 
	alter table tb_student add column stu_tel varchar(20) not null;
	alter table tb_student add column stu_addr varchar(200) default '';
	# 删除列:
	alter table tb_student drop column stu_addr;
	# 修改列的类型:
	alter table tb_student modify column stu_sex char(1) default '男';
	
	# 修改列的名称和默认值
	alter table tb_student change column stu_sex stu_gender char(1) not null default 'M';
	
	# 修改表的名字
	alter table tb_student rename to tb_text;
	# 添加约束条件:
		# 唯一性约束:
	alter table tb_student add constraint uk_student_tel unique (stu_tel);
		# 检查性约束:
	alter table tb_student add constraint ck_student_birth check (stu_birth between '1990-1-1' and '2000-1-1');
	# 删除约束条件:
	alter table tb_student drop constraint uk_student_tel;


练习建立教师的二维表:

create table tb_teacher
(
	tea_id int not null comment '工号',
    tea_name varchar(50) not null comment '名字',
    tea_sex char(2) default '男' comment '性别',
    tea_birth date comment '生日',
    tea_job varchar(5) not null default '一级讲师',
    tea_education varchar(5) not null default '研究生',
    primary key (tea_id),
    check (tea_sex in ('男','女','保密'))
)engine=innodb comment '教师表';

    

建立学院的二维表:

create table tb_college
(
    col_id int not null auto_increment comment '学院编号',
    col_name varchar(20) not null comment '学院名称',
    col_url varchar(50) not null comment '学院网址',
    col_tel varchar(20) not null comment '学院电话',
    primary key (col_id),
    unique(col_tel)
)engine=innodb comment='学院表';

auto_increment: 自动设置编号,必须是整数,必须是主键

unique(col_tel) :添加唯一约束

check (tea_sex in (‘男’,‘女’,‘保密’)):检查师傅符合条件

MySQL中的数据类型:

# 查看数据类型帮助: 
	? data types;
	help data types;

# 整数:
	int / integer / bigint /samllint(4bit)(x) / tinyint(8bit)(x) / unsigned
	- int / integer ---> 32bit ---> 4byte ---> -2^31 ~ 2^31-1
	int unsigned --->无符号整数--->只能表示0和正数---> 0~ 2^32-1
	int(4) zerofill ---> 1 ---> 0001
	- bigint ---> 64bit --->8byte ---> -2^63 ~ 2^63-1
	bigint unsigned ---> 0~ 2^64-1
	- smallint ---> 16bit ---> 2byte---> 0~ 65535
	- tinyint ---> 8 bit ---> 1byte ---> -128 ~ 127


# 小数:
	float(x) / double(x) / decimal(65,30)
	M默认10,N默认0;
	M最大65,N最大30;
	选择decimal(M,N)----> M:小数点前面有多少位。N:小数点后面有多少位
# 日期和时间:
	date / time / datetime / timestamp(x) / year
	时间戳:timestamp--->调整时区--->底层是一个int类型,记录的现在距离1970-1-1-0.0.0到现在的时间---> overflow风险
# 字符串:
	varchar / char(x)(0-255)
	varchar(M)-----65535(使用中文存不了那么多)
	超长内容:clob----> character large object ---> longtext (4GB)
		----> 通过这个列保存数据的URL链接即可
# JSON(√):
	json
# 二进制(x): blob --- > binary large object
	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值