百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT技术 > 正文

MySQL-DDL语句(mysql执行ddl文件)

wptr33 2025-05-03 16:58 2 浏览

介绍

数据库模式定义语言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 秒)

相关推荐

删库不跑路!我含泪写下了 MySQL 数据恢复大法…

1前言数据恢复的前提的做好备份,且开启...

mysqldump备份操作大全及相关参数详解

mysqldump简介mysqldump是用于转储MySQL数据库的实用程序,通常我们用来迁移和备份数据库;它自带的功能参数非常多,文中列举出几乎所有常用的导出操作方法,在文章末尾将所有的参数详细说明...

MySQL表中没有主键,怎么找到重复的数据

在没有主键的MySQL表中查找重复数据可能会有点复杂,但通过使用下述方法中的任何一种,你都应该能够识别并处理这些重复项。在MySQL中,没有主键的表可能会存在重复的数据行。为了找到这些重复的数据,你可...

MySql 大数据 批量删除 Hint 操作

业务中有会碰到数据库中大量冗余数据的情况。比如压测场景,这个时候就需要我们去清理这些数据。怎么操作呢?这个时候mysql的hint就可以派上用场了,直接上语句:DELETE/*+QU...

Linux卸载MySQL教程(linux 卸载数据库)

在Linux系统中,卸载MySQL需要执行以下步骤:停止MySQL服务在卸载MySQL之前,需要先停止MySQL服务,可以使用以下命令停止MySQL服务:sudosystemctlstopmys...

用SQL语句删除数据库重复数据,只保留一条有效数据

原文链接http://t.zoukankan.com/c-Ajing-p-13448349.html在实际开发中,可能会遇到数据库多条数据重复了,此时我们需要删除重复数据,只保留一条有效数据,用SQ...

Mybatis 如何批量删除数据(mybatis删除多条数据)

Mybatis如何批量删除数据本期以最常用的根据id批量删除数据为例:接口设计1:List类型单参数IntegerdeleteByIds(List<Integer>ids);...

MySQL常用命令汇总(mysql数据库常用命令总结)

以下是一份MySQL常用命令汇总,涵盖数据库、表、数据操作及管理功能,方便快速查阅:一、数据库操作1.连接数据库```bash...

「删库跑路」使用Binlog日志恢复误删的MySQL数据

前言“删库跑路”是程序员经常谈起的话题,今天,我就要教大家如何删!库!跑!路!开个玩笑,今天文章的主题是如何使用Mysql内置的Binlog日志对误删的数据进行恢复,读完本文,你能够了解到:MySQL...

MySQL查询是否安装&amp;删除(判断mysql是否安装)

1、查找以前是否装有mysql命令:rpm-qa|grep-imysql可以看到如下图的所示:...

windows版MySQL软件的安装与卸载(windows卸载mysql5.7)

一、卸载1、软件的卸载方式一:通过控制面板方式二:通过电脑管家等软件卸载方式三:通过安装包中提供的卸载功能卸载...

使用 SQL 语句将 Excel VBA 中的表格修改为 MySQL 数据库

在ExcelVBA中与MySQL数据库进行交互时,通常需要使用ADODB连接来执行SQL语句。以下是一个完整的示例,展示了如何将Excel表格中的数据插入到MySQL数据库的...

MySql数据库Innodb引擎删除一行数据会在内存上留下空洞吗

当使用InnoDB引擎删除一行数据时,实际上并不会在内存上留下空洞。InnoDB存储引擎采用了多版本并发控制(MVCC)机制来实现事务的隔离性,每行记录都会保存两个隐藏列,一个保存行的创建版本,另一个...

MySQL批量生成建表语句(mysql 批量新增)

摘要:MySQL批量生成建表语句关键词:MySQL、大批量、挑选、建表语句整体说明在使用MySQL的时候,遇到需要在大批量的表中,挑选一部分表,权限又只有只读权限,工具又没有合适的,最终使用了My...

MySQL数据库之死锁与解决方案(mysql解决死锁的三种方法)

一、表的死锁产生原因:...