数据定义语言DDL
数据定义语言DDL用来创建数据库中的各种对象-----表、视图、
索引、同义词、聚簇等如
建表建库规范:
1库名和表名:强烈建议用小写字母(必须用小写)
2库名和表名:不能以数字和特殊符号开头
3库名和表名:不能使用内部函数名
4库名和表名:名字和务功能相关
1建库
1建库:
mysql> create database tangTest charset utf8mb4;
Query OK, 1 row affected (0.00 sec)
2查看库名:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| tangTest |
| testku |
+--------------------+
6 rows in set (0.01 sec)
mysql>
3查看具体的建库语句:
mysql> show create database tangTest;
+----------+----------------------------------------------------------------------+
| Database | Create Database |
+----------+----------------------------------------------------------------------+
| tangTest | CREATE DATABASE `tangTest` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ |
+----------+----------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
2改库
创建一个默认的拉丁文字符集库
mysql> create database tangTest01;
Query OK, 1 row affected (0.01 sec)
mysql> show create database tangTest01;
+------------+-----------------------------------------------------------------------+
| Database | Create Database |
+------------+-----------------------------------------------------------------------+
| tangTest01 | CREATE DATABASE `tangTest01` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+------------+-----------------------------------------------------------------------+
1 row in set (0.00 sec)
修改库的字符集
mysql> alter database tangTest01 charset utf8mb4;
Query OK, 1 row affected (0.01 sec)
mysql> show create database tangTest01;
+------------+------------------------------------------------------------------------+
| Database | Create Database |
+------------+------------------------------------------------------------------------+
| tangTest01 | CREATE DATABASE `tangTest01` /*!40100 DEFAULT CHARACTER SET utf8mb4
3删库:
mysql> drop database tangTest01;
1徒手建表:
create table tanglinux (
列名 数据类型 约束规范,
id int not null primary key auto_increment comment '学号',
name varchar(255) not null comment '姓名',
age tinyint unsigned not null default 0 comment '年龄',
gender enum('m','f','n') not null default 'n' comment '性别'
intime datatime not null default now() comment '时间';
)charset=utf8mb4 engine=innodb;
2改表
1)添加列,删除列,查看列,修改列属性
-- 添加一个列
alter table tanglinux add telnum char(11) not null unique comment '手机号';
-- 在表中加一个状态stat,非空,值是0或1,默认为0
alter table tanglinux add stat enum('0','1') not null default '1' comment '状态';
-- 或者
alter table tanglinux add state tinyint unsigned not null default 1 comment '状态';
-- 删除一个列
alter table tanglinux drop state;
-- 查看列信息
desc tanglinux;
-- 在name后面添加一个qq列
alter table tanglinux add qq varchar(255) not null unique comment 'qq' AFTER NAME;
-- 在name前面添加一个wechat
alter table tanglinux add wechat varchar(255) not null unique comment '微信号' after id;
-- 在首列添加一个uid
alter table tanglinux add uid varchar(255) not null unique comment '用户标识' first;
-- 修改列属性
alter table tanglinux modify name varchar(128) not null;
-- 将列名gender改为gg,并且将列的数据类型改为char
alter table tanglinux change gender gg char(1) not null default 'n';
-- 切换到tangTest库(需要登陆mysql后执行)
use tangTest;
-- 查看表的列信息
DESC tanglinux;
-- 查看建表语句
show create table tanglinux;
-- 建一个一模一样的表
create table tanglinuxcopy like tanglinux;
-- 查询tanglinux表在哪里
select table_schema ,table_name
from information_schema.tables
where table_name='tanglinux';