MySQL-DDL语句(mysql执行ddl文件)
wptr33 2025-05-03 16:58 10 浏览
介绍
数据库模式定义语言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 秒)
相关推荐
- F103C8T6移植FATFS文件系统 版本R0.15
-
STM32F103C8T6芯片在W25Q64上移植FATFS(版本R0.15)实现过程:1、首先完成USART初始化和调试,用于传输信息到串口调试软件。2、完成SPI相关参数配置及调试,用于单片机和存...
- stm32使用MPU6050或ADXL345控制的车辆减速灯
-
本实验例程采用MPU6050六轴运动处理组件...
- STM32F103串口输出prtinf覆盖(stm32printf函数的串口输出)
-
采用正点原子的板子,有如下坑,记录如下:(1)main中应用头文件#include"stdio.h"(2)采用hal进行fputc和fgetc覆盖,如下intfputc(intc...
- STM32 学习8 USART串口通讯与printf重定向
-
一、串口通信介绍STM32F103ZET6包含多个UART、USART串口。...
- 教你如何使用SEGGER RTT优雅的实现日志系统
-
今天开始了BMS系统的软件代码部分的搭建,计划是分成三层:硬件驱动,AFE层和系统应用层。第一步肯定是先把底层的IIC通信调通,CG861xx的IIC通信和TI的BQ769X0...
- 终极调试利器,各种Link通吃(link4a调制方式)
-
今天继续更新一期KEIL调试方法。事实上,关于调试方法,鱼鹰写了一个系列,汇总文为《佛祖保佑,永无BUG,永不修改|KEIL调试系列总结篇》,对于KEIL方法感兴趣的可以看看。这个调试...
- 在 STM32 中使用 printf() 函数,别漏掉这几行代码!
-
问:在STM32上轻松使用printf函数除了点亮LED外,向串行控制台发送打印信息可能是调试嵌入式项目时最简单、最直接且最常用的技术。虽然大多数平台都拥有可以在UART总线上传输数据的API,但它们...
- 高性能异步io机制:io_uring(异步io select)
-
io_uring是linux内核5.10引入的异步io接口。相比起用户态的DPDK、SPDK,io_uring作为内核的一部分,通过mmap的方式实现用户和内核共享内存,并基于m...
- 精品博文ARM中打印函数print 的几种实现方法
-
1利用C库函数printf步骤:1)首先需要包含头文件stdio.h。2)然后定义文件句柄。实际上就是一个int型变量封装在结构体中。struct__FILE{inthandle;};3)定...
- C语言char的详解(c语言(char))
-
在C语言中,char是一种基础数据类型,用于表示字符或小整数值。对char的理解和处理非常重要,尤其是在字符串操作、文件读写或其他需要直接控制内存的应用场景中。下面从基本定义、存储方式、常见用法...
- C语言之文件操作(c语言文件操作实验总结)
-
文件操作是C语言中非常重要的功能,用于读取和写入文件中的数据。C语言提供了一组标准库函数(如fopen、fclose、fread、fwrite等)来实现文件操作。以下是针对C语言初学者的详细讲解。...
- STM32-ADC如何把采集的数据转换为小数
-
编辑一、代码原理解析这段代码围绕“STM32中ADC数据采集、整数与小数计算及串口输出”展开,核心是数据类型的使用(unsignedint/signedint/float)、ADC数...
- 循环队列原理及在单片机串口通讯中的应用(二)
-
前言书接上回,前文主要介绍了环形队列的实现原理以及C语言实现及测试过程,本文将回归到嵌入式平台的应用中,话不多说,淦,上干货!...
- STM32编程中printf函数重定向背后的原理
-
在C语言中,printf是一个非常好用的函数,尤其是在程序调试阶段,我们可以通printf打印变量的值来帮助查错。在学习C语言的时候我们的开发环境和运行环境都是PC机,printf函数打印到PC机...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
git pull命令使用实例 git pull--rebase
-
git 执行pull错误如何撤销 git pull fail
-
面试官:git pull是哪两个指令的组合?
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
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)
- 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)