首页 > 数据库技术 > 详细

转载 MySQL创建表的语句 示例

时间:2017-11-12 13:04:07      阅读:195      评论:0      收藏:0      [点我收藏+]
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);

技术分享

 

转载 MySQL创建表的语句 示例

原文:http://www.cnblogs.com/mike-mei/p/7821386.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!