DDL
DDL(data definition language)数据库定义语言:
其实就是我们在创建表的时候用到的一些sql,比如说:CREATE、ALTER、DROP等。DDL主要是用在定义或改变表的结构,数据类型,表之间的链接和约束等初始化工作上
语法
CREATE TABLE 表名称
(
列名称1 数据类型,
列名称2 数据类型,
列名称3 数据类型,
....
)
ALTER TABLE table_name
ALTER COLUMN column_name datatype
DROP TABLE 表名称
DROP DATABASE 数据库名称
DDL应用
库操作
库定义规范
1、 必须设置字符集
2、 库名:不要大写字母,不要数字开头,不要超过18字符,不要用内置字符串(关键字,如table、database),库名要和业务有关
1、建库
mysql> create database if not exists sch charset utf8mb4; Query OK, 1 row affected (0.03 sec)
2、查库
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sch | | sys | +--------------------+ 5 rows in set (0.03 sec)
3、查看建库语句
mysql> show create database sch; +----------+-------------------------------------------------------------------------------------------------------------------------------+ | Database | Create Database | +----------+-------------------------------------------------------------------------------------------------------------------------------+ | sch | CREATE DATABASE `sch` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */ | +----------+-------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
4、修改库定义
mysql> alter database sch charset utf8; Query OK, 1 row affected, 1 warning (0.01 sec)
5、删除数据库
mysql> drop database scho; ERROR 1008 (HY000): Can't drop database 'scho'; database doesn't exist mysql> drop database if exists scho; Query OK, 0 rows affected, 1 warning (0.01 sec)
表定义
建表规范:
1. 表名:不要大写字母,不要数字开头,不要超过18字符,不要用内置字符串,和业务有关。 2. 列名:业务有关,不要内置字符,不要超过18字符。 3. 数据类型:合适的,精简的,完整的。 4. 每个表要有且只有1个主键。每个列尽量Not null。尽量不要使用外键 5. 表要有注释,每列有注释。 6. 存储引擎InnoDB,字符集utf8mb4
建表
CREATE TABLE `students` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键', `sid` char(10) NOT NULL COMMENT '学号', `sname` varchar(128) NOT NULL COMMENT '学生姓名', `sage` tinyint(3) unsigned NOT NULL DEFAULT '18' COMMENT '年龄', `sgender` tinyint(3) unsigned NOT NULL DEFAULT '2' COMMENT '性别', `scardid` char(18) NOT NULL COMMENT '身份证号', `saddr` enum('北京市','上海市','天津市','重庆') NOT NULL COMMENT '省分', `hobby` set('足球','篮球','羽毛球','排球','乒乓球') DEFAULT NULL COMMENT '爱好', `salary` decimal(10,2) unsigned NOT NULL COMMENT '工资', `sdate` datetime NOT NULL COMMENT '入学时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci comment '学生表';
添加字段add
mysql> alter table student add stel char(11) not null unique comment '手机号'; mysql> alter table student add index idx_sid(sid);
修改字段数据类型modify
mysql> alter table student modify stel varchar(11); Query OK, 0 rows affected (0.05 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc student; +---------+---------------------------------------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+---------------------------------------------------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | sid | char(10) | NO | | NULL | | | sname | varchar(128) | NO | | NULL | | | sage | tinyint(3) unsigned | NO | | 18 | | | sgender | tinyint(3) unsigned | NO | | 2 | | | scardid | char(18) | NO | | NULL | | | saddr | enum('北京市','上海市','天津市','重庆') | NO | | NULL | | | hobby | set('足球','篮球','羽毛球','排球','乒乓球') | YES | | NULL | | | salary | decimal(10,2) unsigned | NO | | NULL | | | sdate | datetime | NO | | NULL | | | stel | varchar(11) | YES | | NULL | | +---------+---------------------------------------------------------+------+-----+---------+----------------+
修改字段名称和字段的数据类型 change
mysql> alter table student change stel phone_no char(11) not null unique; Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc student; +----------+---------------------------------------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------------------------------------------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | sid | char(10) | NO | | NULL | | | sname | varchar(128) | NO | | NULL | | | sage | tinyint(3) unsigned | NO | | 18 | | | sgender | tinyint(3) unsigned | NO | | 2 | | | scardid | char(18) | NO | | NULL | | | saddr | enum('北京市','上海市','天津市','重庆') | NO | | NULL | | | hobby | set('足球','篮球','羽毛球','排球','乒乓球') | YES | | NULL | | | salary | decimal(10,2) unsigned | NO | | NULL | | | sdate | datetime | NO | | NULL | | | phone_no | char(11) | NO | UNI | NULL | | +----------+---------------------------------------------------------+------+-----+---------+----------------+ 11 rows in set (0.00 sec)
添加或修改注释:
mysql> alter table students comment '学生表'; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
删表:
mysql> create table stu like students;
mysql> drop table stu;
删除字段
mysql> alter table student drop phone_no; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0
添加索引
mysql> alter table student add fulltext index idx_sid(sid); Query OK, 0 rows affected, 1 warning (0.64 sec) Records: 0 Duplicates: 0 Warnings: 1 mysql> show warnings -> ; +---------+------+--------------------------------------------------+ | Level | Code | Message | +---------+------+--------------------------------------------------+ | Warning | 124 | InnoDB rebuilding table to add column FTS_DOC_ID | +---------+------+--------------------------------------------------+ 1 row in set (0.00 sec)
删除索引
mysql> drop index idx_sid on student; Query OK, 0 rows affected (0.35 sec) Records: 0 Duplicates: 0 Warnings: 0