前端开发入门到精通的在线学习网站

网站首页 > 资源文章 正文

MySQL-DDL语句(mysqlddl语句有哪些)

qiguaw 2025-05-02 20:39:48 资源文章 11 ℃ 0 评论

介绍

数据库模式定义语言DDL(Data Definition Language),是用于描述数据库中要存储的现实世界实体的语言。

数据库模式定义语言并非程序设计语言,DDL数据库模式定义语言是SQL语言(结构化查询语言)的组成部分。DDL描述的模式,必须由计算机软件进行编译,转换为便于计算机存储、查询和操纵的格式,完成这个转换工作的程序称为模式编译器。

模式编译器处理模式定义主要产生两种类型的数据:数据字典以及数据类型和结构定义。

1、数据库操作-上

1.1、DDL概述

DDL(data definition language)数据库定义语言:其实就是我们在创建表的时候用到的一些sql,比如说:CREATE、ALTER、DROP等。DDL主要是用在操作数据库,定义或改变数据库表的结构,数据类型等初始化工作。

1.2、创建数据库

直接创建数据库

格式:
create database 数据库名;

判断数据库是否已经存在,不存在则创建

格式:
create database if not exists 数据库名;

创建数据库并指定字符集

格式:
create database 数据库名 character set 字符集;

案例:

#创建数据库
mysql> create database gongjunhe;
Query OK, 1 rows affected (0.08 秒)#创建成功
#在次创建同名数据库
mysql> create database gongjunhe;
Can't create database 'gongjunhe'; database exists #无法创建数据库“gongjunhe”;数据库存在
#判断是否存在,如果不存在则创建数据库gongjunhe
mysql> create database if not exists gongjunhe;
Query OK, 1 rows affected, 1 warnings (0.01 秒)
#创建数据库并指定字符集为 gbk
mysql> create database gongjunhe01 default character set gbk;
Query OK, 1 rows affected (0.03 秒)

注意:

default 可以不要

补充:了解字符集查看**

#查看字符集
mysql> show character set;
+----------+---------------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+---------------------------------+---------------------+--------+
| armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
| ascii | US ASCII | ascii_general_ci | 1 |
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| binary | Binary pseudo charset | binary | 1 |
| cp1250 | Windows Central European | cp1250_general_ci | 1 |
| cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
| cp1256 | Windows Arabic | cp1256_general_ci | 1 |
| cp1257 | Windows Baltic | cp1257_general_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| cp852 | DOS Central European | cp852_general_ci | 1 |
| cp866 | DOS Russian | cp866_general_ci | 1 |
| cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 |
| euckr | EUC-KR Korean | euckr_korean_ci | 2 |
| gb18030 | China National Standard GB18030 | gb18030_chinese_ci | 4 |
| gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |
| gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |
| geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
| greek | ISO 8859-7 Greek | greek_general_ci | 1 |
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
| latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
| latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
| macce | Mac Central European | macce_general_ci | 1 |
| macroman | Mac West European | macroman_general_ci | 1 |
| sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |
| swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
| tis620 | TIS620 Thai | tis620_thai_ci | 1 |
| ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |
| ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
| utf16 | UTF-16 Unicode | utf16_general_ci | 4 |
| utf16le | UTF-16LE Unicode | utf16le_general_ci | 4 |
| utf32 | UTF-32 Unicode | utf32_general_ci | 4 |
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
| utf8mb4 | UTF-8 Unicode | utf8mb4_0900_ai_ci | 4 |
+----------+---------------------------------+---------------------+--------+
41 行于数据集 (0.02 秒)

1.3、查看数据库

查看所有数据库

格式:
show databases;

查看某个数据库

格式:
show create database 数据库名;

案例:

#查看所有数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mb |
| mysql |
| performance_schema |
| sys |
| gongjunhe |
| gongjunhe01 |
+--------------------+
7 行于数据集 (0.01 秒)
#查看数据库gongjunhe的信息
mysql> show create database gongjunhe;
+----------+-------------------------------------------------------------------------------------------------+
| Database | Create Database |
+----------+-------------------------------------------------------------------------------------------------+
| gongjunhe | CREATE DATABASE `gongjunhe` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ |
+----------+-------------------------------------------------------------------------------------------------+
1 行于数据集 (0.01 秒)
2、数据库操作-下

2.1、修改数据库

修改字符集

格式:
alter database 数据库名 character set 字符集;

案例:

#需求:将gongjunhe01数据库的字符集改成 utf8
mysql> alter database gongjunhe01 character set utf8;
Query OK, 1 rows affected, 1 warnings (0.09 秒)

注意:

为什么修改的不是数据库名?

容易引起数据丢失。

rename database 旧数据库名 to 新数据库名;

这个是5.1.7到5.1.23版本可以用,但是官方不推荐,会有丢失数据的危险

2.2、删除数据库

删除数据库

格式:
drop database 数据库名;

案例:

#需求:删除gongjunhe01数据库
mysql> drop database gongjunhe01;
Query OK, 0 rows affected (0.07 秒)
#查看所有数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mb |
| mysql |
| performance_schema |
| sys |
| gongjunhe |
+--------------------+
6 行于数据集 (0.01 秒)

2.3、使用数据库

查看当前数据库

格式:
select database();#mysql中的全局函数

切换数据库

格式:
use 数据库名;

案例:

#查看当前使用的数据库
mysql> select database();
+------------+
| database() |
+------------+
| NULL |#当前没有使用的数据库
+------------+
1 行于数据集 (0.01 秒)
#切换或指定当前使用的数据库
mysql> use gongjunhe;
Query OK, 0 rows affected (0.01 秒)
#查看当前使用的数据库
mysql> select database();
+------------+
| database() |
+------------+
| gongjunhe |#当前使用的数据库为gongjunhe
+------------+
1 行于数据集 (0.01 秒)

3、数据库表操作-上

3.1、创建表

创建表结构

格式:
create table 数据库表名(
字段名1 字段类型1,
字段名2 字段类型2,
...
字段名n 字段类型n
);

关键字说明

create:创建

table:表

3.2、数据类型(mysql)

数字类型

日期类型

字符串类型

BLOB/TEXT

BINARY/VARBINARY

ENUM/SET

案例:

创建一个学生表,里面包含了编号、学生名字、出生年月等数据

分析:

表名:students

字段有:编号(id,int类型)、学生名字(sname,varchar()类型)、出生日期(birthday date类型)

create table students(
id int, -- 学生id
sname varchar(20), -- 学生名字
birthday date -- 学生出生日期
);
#想要创建数据库表先进入,数据库
mysql> use gongjunhe;
Query OK, 0 rows affected (0.01 秒)
#创建表
mysql> create table students(id int ,sname varchar(20),birthday date);
Query OK, 0 rows affected (0.05 秒)
#查看所有表
mysql> show tables;
+--------------------+
| Tables_in_gongjunhe |
+--------------------+
| students |
+--------------------+
1 行于数据集 (0.01 秒)

3.3、查看表

查看所有表

格式:
show tables;

查看表结构

格式:
desc 数据库表名;

查看表SQL信息

格式:
show create table 数据库表名;

案例:

查看gongjunhe数据库下的所有表

#进入mysql数据库
mysql> use gongjunhe;
Query OK, 0 rows affected (0.01 秒)
#查看数据库里的所有表
mysql> show tables;
+--------------------+
| Tables_in_gongjunhe |
+--------------------+
| students |
+--------------------+
1 行于数据集 (0.01 秒)

查看students数据库表的结构

mysql> desc students;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| sname | varchar(20) | YES | | NULL | |
| birthday | date | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 行于数据集 (0.01 秒)

查看students数据库表的信息

#查看students数据库表SQL信息
mysql> show create table students;
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| students | CREATE TABLE `students` (
`id` int(11) DEFAULT NULL,
`sname` varchar(20) DEFAULT NULL,
`birthday` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 行于数据集 (0.01 秒)

4、数据库表操作-中

4.1、快速建表

建新表

格式:
create table 新数据库表名 like 旧数据库表名;

案例:

创建一个students01表,要求表结构与students相同

#创建一个新表与旧表结构相同
mysql> create table students01 like students;
Query OK, 0 rows affected (0.11 秒)
#查看表结构
mysql> desc students01;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| sname | varchar(20) | YES | | NULL | |
| birthday | date | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 行于数据集 (0.02 秒)

4.2、删除表

直接删除表

格式:
drop table 数据库表名;

判断表是否存在,存在则删除

格式:
drop table if exists 数据库表名;

案例:

#直接删除students01;
mysql> drop table students01;
Query OK, 0 rows affected (0.09 秒)
#有就删除students01,没有就不删除;
mysql> drop table if exists students01;
Query OK, 0 rows affected, 1 warnings (0.01 秒)

5、数据库表操作-下

5.1、修改表

添加表字段

格式:
alter table 数据库表名 add 字段名 字段类型;

案例:

为students表添加一个字段性别(sex char类型)

#增加字段
mysql> alter table students add sex char;
Query OK, 0 rows affected (0.05 秒)
#查看表结构
mysql> desc students;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| sname | varchar(20) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 行于数据集 (0.01 秒)

修改表字段类型

格式:
alter table 数据库表名 modify 字段名 新字段类型;

案例:

将students表中字段性别(sex)的字段类型改为varchar(2)

#修改字段类型
mysql> alter table students modify sex varchar(2);
Query OK, 0 rows affected (0.06 秒)
#查看表结构
mysql> desc students;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| sname | varchar(20) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| sex | varchar(2) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 行于数据集 (0.01 秒)

修改表字段名

格式:
alter table 数据库表名 change 旧字段名 新字段名 字段类型;

案例:

将students表中的性别(sex)改成班级(classes)类型为varchar(10)

#修改字段名
mysql> alter table students change sex classes varchar(10);
Query OK, 0 rows affected (0.07 秒)
#查看表结构
mysql> desc students;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| sname | varchar(20) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| classes | varchar(10) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 行于数据集 (0.02 秒)

删除表中字段

格式:
alter table 数据库表名 drop 字段名;

案例:

删除students表中的班级(classes)字段

#删除字段
mysql> alter table students drop classes;
Query OK, 0 rows affected (0.05 秒)
#查看表结构
mysql> desc students;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| sname | varchar(20) | YES | | NULL | |
| birthday | date | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 行于数据集 (0.01 秒)

修改表名

格式:
rename table 数据库表名 to 新数据库表;

案例:

#修改表名
mysql> rename table students to student;
Query OK, 0 rows affected (0.04 秒)
#查看所有表
mysql> show tables;
+--------------------+
| Tables_in_gongjunhe |
+--------------------+
| student |
+--------------------+
1 行于数据集 (0.01 秒)

修改字符集

格式:
alter table 数据库表名 character set 字符集;

案例:

修改student表的字符集

#修改数据库表字符集
mysql> alter table student character set gbk;
Query OK, 0 rows affected (0.07 秒)
#查看数据库表SQL信息
mysql> show create table student;
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
`id` int(11) DEFAULT NULL,
`sname` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`birthday` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 行于数据集 (0.01 秒)

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表