数据库

show variables like 'character_set_client';#查询字符集
show databases;#列出所有的服务器上的数据库alter
create database if not exists test;#创建一个数据库
drop database fk;#删除数据库
show tables from test;#显示一个数据库中的表
use test;

create table tb_dept(
Id int primary key auto_increment,#部门编号 整形 主键 自增长
Name varchar(18),#部门名称
description varchar(100)#描述
);

show tables from test;

desc tb_dept;#查看表信息

show create table tb_dept;

use test;
#员工表
create table tb_emp(
id int primary key auto_increment,#auto_increment只是MySQL特有的
Name varchar(18),
sex varchar(2),
age int,
address varchar(200),
email varchar(100)
);

drop table tb_dept;
#修改列类型
#注意:不是任何情况下都可以去修改的,
#只有当字段只包含空值时才可以修改。
alter table tb_emp modify sex varchar(4);
#增加列
alter table tb_emp add tel varchar(12);
#删除列
alter table tb_emp drop tel;
alter table tb_emp drop column tel;
#列改名
alter table tb_emp change Name emp_Name varchar(18);
#更改表名
alter table tb_emp rename emp;
rename table emp to tb_emp;

insert into dept_emp (Name,sex,age,address,email)values('','','','','');

#约束
#是在表上强制执行地数据校验规则,主要用于保证数据库地完整性
/*
not null
unique 唯一键tb_depttb_dept
primary key
foreign key 外键
check 检查
*/

create table tb_emp(
id int primary key auto_increment,
Name varchar(18),
sex varchar(2) default'男' check(sex='男'or sex='女'),#表级写法check 在mysql中不起作用
age int,
address varchar(200),
email varchar(100) unique,
dept_id int,#references tb_dept(id) #表级写法外键不起作用
constraint foreign key fk_emp(dept_id) references tb_dept(id)
);

#创建表之后在添加
alter table tb_emp add constraint foreign key fk_emp(dept_id) references tb_dept(id);

 

 

 

posted on 2018-05-29 16:53  runoob!  阅读(679)  评论(0编辑  收藏  举报

导航