MySQL-DDL语句(mysql执行ddl文件)
wptr33 2025-05-03 16:58 4 浏览
介绍
数据库模式定义语言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 秒)
相关推荐
- Java中JDK里用到了哪些设计模式?让面试官眼前一亮!
-
大家好,欢迎来到程序视点!我是小二哥。Java中JDK里用到了哪些设计模式?...
- 当问到组件实现原理时,面试官是在刁难你吗?
-
今天我想跟你探讨的话题是:当面试官问你某某组件的实现原理是什么时,他究竟想了解什么?你又需要了解到什么层面上呢?...
- 京东大佬问我,在SpringBoot中怎么使用时间轮?要考虑哪些方面?
-
京东大佬问我,什么是时间轮?为什么要用时间轮?在SpringBoot中怎么使用时间轮?要考虑哪些方面的问题呢?嗯,用户问到了时间轮,还有在SpringBoot中怎么用,需要考虑哪些问题。首先,我得先...
- Redis和Memcached区别详解(5大核心区别)
-
Redis和Memcached都是常见的内存缓存系统,但也有区别,以下是5大Redis和Memcached的区别@mikechen本篇已收于mikechen原创超30万字《阿里架构师进阶专题合集》里面...
- 工作中用Redis最多的10种场景(redis实际应用场景)
-
前言Redis作为一种优秀的基于key/value的缓存,有非常不错的性能和稳定性,无论是在工作中,还是面试中,都经常会出现。今天这篇文章就跟大家一起聊聊,我在实际工作中使用Redis的10种场景,希...
- Redis面试攻防战:如何赢得技术博弈的胜利
-
今天,我面试了某大厂的java开发岗位,迎面走来一位风尘仆仆的中年男子,手里拿着屏幕还亮着的mac,他冲着我礼貌的笑了笑,然后说了句“不好意思,让你久等了”,然后示意我坐下,说:“我们开始吧。看了你的...
- 深入浅出聊聊 Redis 高级特性(redis如何实现高性能)
-
Redis数据结构Redis常用的数据类型主要有以下五种:StringHashListSetSortedsetRedis内部使用一个redisObject对象来表示所有的key和va...
- Redis在Java项目中的典型应用场景
-
Redis在Java项目中的典型应用场景在Java项目的世界里,Redis作为一种高性能的内存数据库,其应用已经变得极为广泛。它不仅具备缓存功能,还能胜任分布式锁、消息队列等多种角色。今天,我们就来聊...
- Redis与Java集成的最佳实践:打造高效缓存系统
-
Redis与Java集成的最佳实践:打造高效缓存系统在当今高并发的时代,Redis作为一款高效的内存数据库,已经成为Java开发者不可或缺的工具之一。它不仅能显著提升系统的响应速度,还能有效减轻数据库...
- Redis 慢查询:从青铜到王者的进阶之路
-
各位程序员老铁们,欢迎来到Redis吐槽大会!今天咱们要吐槽的「摸鱼选手」叫慢查询——这货表面上是条普通命令,背地里却能让你的Redis分分钟变成「龟速数据库」。想知道它是怎么搞破坏的?跟...
- 订单超时自动取消的7种方案,我用这种!
-
前言在电商、外卖、票务等系统中,订单超时未支付自动取消是一个常见的需求。...
- Redis在Java项目中的奇妙应用(redis在java项目中的使用)
-
Redis在Java项目中的奇妙应用在Java的世界里,Redis就像是那位低调却实力非凡的幕后英雄。它虽不像Spring那样被频繁提及,但它的身影却无处不在。今天,我们就来聊聊Redis这位“存储大...
- 2015年在Twitter上刷屏的那些事儿
-
我们将盘点在今年12个月里Twitter上最有影响力的大V跟那些轰动整个网络的新闻事件。今年,我们看到了巴黎恐怖袭击、成千上万难民们试图远离战争等许多重大的事件。本周,Twitter公布了整整12个月...
- Swift 语言指南-Issue 43(swift语句)
-
本期特别推荐1.项目:Filterpedia(完整、强大的图片滤镜类库)、ElasticTransition(畅快、无违和感的皮筋式动画转场)以及VWInstantRun(Xcode高效调试插件...
- 1小时入门Swift语法(swift语法 简书)
-
简介Swift语言由苹果公司在2014年推出,用来撰写OSX和iOS应用程序2014年,在AppleWWDC发布特点从它的语法中能看到Objective-C、JavaScrip...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
git pull命令使用实例 git pull--rebase
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
面试官:git pull是哪两个指令的组合?
-
git 执行pull错误如何撤销 git pull fail
-
git pull 之后本地代码被覆盖 解决方案
-
还可以这样玩?Git基本原理及各种骚操作,涨知识了
-
git命令之pull git.pull
-
- 最近发表
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (33)
- mysql max (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)