peiyuan1030 发表于 2017-12-11 20:24:08

MySQL创建表的语句

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(

  
    >  
   Name varchar(18),#部门名称
  
   description varchar(100)#描述
  
);
  

  
show tables from test;
  

  
desc tb_dept;#查看表信息
  

  
show create table tb_dept;
  

  
use test;
  
#员工表
  
create table tb_emp(

  
>  
Name varchar(18),
  
sex varchar(2),
  
age int,
  
address varchar(200),
  
email varchar(100)
  
);
  

  
drop table tb_dept;
  
#修改列类型
  
#注意:不是任何情况下都可以去修改的,
  
#只有当字段只包含空值时才可以修改。

  
>  
#增加列

  
>  
#删除列

  
>
  
>  
#列改名

  
>  
#更改表名

  
>  
rename table emp to tb_emp;
  

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

  
#约束
  
#是在表上强制执行地数据校验规则,主要用于保证数据库地完整性
  

  
      
  

  
create table tb_emp(

  
>  
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)
  
);
  

  
#创建表之后在添加

  
>
页: [1]
查看完整版本: MySQL创建表的语句