MySQL-DDL语句(mysql执行ddl文件)
wptr33 2025-05-03 16:58 9 浏览
介绍
数据库模式定义语言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 秒)
相关推荐
- SpringBoot 3 + Flutter3 实战低代码运营管理-10章
-
获课》aixuetang.xyz/5075/三天构建运营管理系统:SpringBoot3+Flutter3高效开发方法论...
- SpringBoot探针实现:从零构建应用健康监控利器
-
SpringBoot探针实现:从零构建应用健康监控利器声明本文中的所有案例代码、配置仅供参考,如需使用请严格做好相关测试及评估,对于因参照本文内容进行操作而导致的任何直接或间接损失,作者概不负责。本文...
- Spring Batch中的JobRepository:批处理的“记忆大师”是如何工作
-
一、JobRepository是谁?——批处理的“档案馆”JobRepository是SpringBatch的“记忆中枢”,负责记录所有Job和Step的执行状态。它像一位严谨的档案管理员,把任务执...
- 还在为 Spring Boot3 技术整合发愁?一文解锁大厂都在用的实用方案
-
你在使用SpringBoot3开发后端项目时,是不是常常陷入这样的困境?想提升项目性能和功能,却不知道该整合哪些技术;好不容易选定技术,又在配置和使用上频频踩坑。其实,这是很多互联网大厂后端开发...
- 一文吃透!Spring Boot 项目请求日志记录,这几招你绝对不能错过!
-
在互联网应用开发的高速赛道上,系统的稳定性、可维护性以及安全性是每一位开发者都必须关注的核心要素。而请求日志记录,就如同系统的“黑匣子”,能够为我们提供排查故障、分析用户行为、优化系统性能等关键信息...
- spring-boot-starter-actuator简单介绍
-
SpringBootActuator是SpringBoot的一个功能强大的子项目,它提供了一些有用的监控和管理SpringBoot应用程序的端点。SpringBootActuat...
- 使用SpringBoot钩子或Actuator实现优雅停机
-
服务如何响应停机信号在java中我们可以直接利用通过Runtime...
- 28-自定义Spring Boot Actuator指标
-
上篇我们学习了《27-自定义SpringBootActuator健康指示器》,本篇我们学习自定义SpringBootActuator指标(Metric)。...
- 如何在Spring Boot中整合Spring Boot Actuator进行服务应用监控?
-
监控是确保系统稳定性和性能的关键组成部分,而在SpringBoot中就提供了默认的应用监控方案SpringBootActuator,通过SpringBootActuator提供了开箱即用的应...
- 「Spring Boot」 Actuator Endpoint
-
Actuator官网地址:https://docs.spring.io/spring-boot/docs/2.5.6/reference/html/actuator.html目的监控并管理应用程序...
- Spring Boot Actuator监控功能全面剖析
-
SpringBootActuator监控功能全面剖析在现代企业级Java开发中,SpringBoot以其轻量化、高效率的特性深受开发者青睐。而作为SpringBoot生态系统的重要组成部分,S...
- 1000字彻底搞懂SpringBootActuator组件!
-
SpringBootActuator组件SpringBootActuator通过HTTPendpoints或者JMX来管理和监控SpringBoot应用,如服务的审计、健康检查、指标统计和...
- JavaScript数据类型(javascript数据类型介绍)
-
基本数据类型BooleanNullNumberStringSymbolUndefined对象数据类型ObjectArray定义:JavaScript数组是内置的对象之一,它可以用一个变量来存储多个同种...
- 能运行,不代表它是对的:5 个潜伏在正常功能下的 JavaScript 错误
-
JavaScript的动态性和复杂性意味着,代码虽然表面上正常运行,但一些深层次、隐蔽的陷阱往往让人意想不到,梳理了几个JavaScript开发中难以发现的隐蔽错误,旨在帮助我们写出更健壮、更可...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
git pull命令使用实例 git pull--rebase
-
面试官:git pull是哪两个指令的组合?
-
git 执行pull错误如何撤销 git pull fail
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
git pull 之后本地代码被覆盖 解决方案
-
还可以这样玩?Git基本原理及各种骚操作,涨知识了
-
git命令之pull git.pull
-
- 最近发表
-
- SpringBoot 3 + Flutter3 实战低代码运营管理-10章
- SpringBoot探针实现:从零构建应用健康监控利器
- Spring Batch中的JobRepository:批处理的“记忆大师”是如何工作
- Github霸榜的SpringBoot全套学习教程,从入门到实战,内容超详细
- 还在为 Spring Boot3 技术整合发愁?一文解锁大厂都在用的实用方案
- 一文吃透!Spring Boot 项目请求日志记录,这几招你绝对不能错过!
- spring-boot-starter-actuator简单介绍
- 使用SpringBoot钩子或Actuator实现优雅停机
- 28-自定义Spring Boot Actuator指标
- 如何在Spring Boot中整合Spring Boot Actuator进行服务应用监控?
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (33)
- mybatis 分页 (35)
- vba split (37)
- redis watch (34)
- python list sort (37)
- nvarchar2 (34)
- mysql not null (36)
- hmset (35)
- python telnet (35)
- python readlines() 方法 (36)
- munmap (35)
- docker network create (35)
- redis 集合 (37)
- python sftp (37)
- setpriority (34)
- c语言 switch (34)
- git commit (34)