当前位置:网站首页>MySQL-数据定义语言-DDLdatebase define language
MySQL-数据定义语言-DDLdatebase define language
2022-08-01 04:39:00 【LXMXHJ】
文章目录
库的增改删
库创建create database
语法
create database [ is not exists] 库名
[character set 字符集名];
理解
is not exists 如果不存在就创建;
案例
库修改alter database
语法:修改字符集
alter database 库名 character set 字符集名;
修改库名
rename database 旧库名 to 新库名;
# 一般都是在mysql中的data文件中找到数据库文件夹直接修改名字。
库删除 trop database
语法
drop database [if exists] 库名;
案例
字符集、字符序
概念
在数据存储上,MySQL提供了不同的字符集支持;
在数据的对比操作上,提供了不同的字符序支持;
字符集 = 定义了字符以及字符的编码
字符序 = 字符的比较规则
MySQL 支持多种字符集 与 字符序。
一个字符集至少对应一个字符序(一般为一对多);
两个不同的字符集不能有相同字符序;
每个字符集都有默认的字符序;
常见sql命令
查看支持的字符集
show character set;
use information_schema;
select * from character_sets;
显示字段有:character_set_name 、 default_collate_name、description、maxlen
查看支持的字符序
show collation
use information_schema;
select * from collations;
备注
information_schema 用于保存源数据的信息;
表的增改删
表创建create table
语法
create table [is not exists] 表名(
字段名 字段类型【长度 约束】,
字段名 字段类型【长度 约束】,
.....
字段名 字段类型【长度 约束】,
);
案例
一个字符是一个字符,一个汉字也是一个字符;
表修改alter table
语法
要求 | sql语句 |
---|---|
添加新的列 | alter table 表名 add column 列名 列类型 [约束]; alter table 表名 add column 列名 列类型 [ first / later 字段名] ; fisrt表示新字段添加到第一列;later字段名表示添加新字段到字段名后 |
删除列 | alter table 表名 drop column 列名; |
修改列名 | alter table 表名 change column 旧列名 新列名 类型; |
修改列类型 或 约束 | alter table 表名 modify column 列名 新类型 新约束; |
修改字段默认数据 | alter table 表名 alter column 列名 set default 默认值; |
修改表名 | alter table 表名 rename [to] 新表名; |
案例
表删除drop table
语法
drop table [if exists] 表名;
# if exists 容错需求。
案例
表的复制
语法
需求 | sql语句 |
---|---|
仅仅复制表结构 | create table 新表名 like 旧表名 |
复制结构 + 全部数据 | create table 新表名 select * from 旧表 |
复制部分结构 + 部分数据 | create table 新表名 select 部分字段 from 旧表 where 筛选条件 |
仅仅复制某些字段 没有数据 | create table 新表 select 某些字段 from 旧表 where false |
备注
可以跨库复制,需要使用的是库名.表的形式。
案例
库表常见写法
drop database [is exists] 库名;
drop table [is exists] 表名;
create database 库名;
create table 表名;
练习
边栏推荐
- Article summary: the basic model of VPN and business types
- 律师解读 | 枪炮还是玫瑰?从大厂之争谈元宇宙互操作性
- Four implementations of
batch insert: have you really got it? - 风险策略调优中重要的三步分析法
- Simple and easy to use task queue - beanstalkd
- 25. 这三道常见的面试题,你有被问过吗?
- Introduction to Oracle
- Summary of mobile page optimization in seconds
- Pyspark Machine Learning: Vectors and Common Operations
- 让你的 Lottie 支持文字区域内自动换行
猜你喜欢
2022-07-31: Given a graph with n points and m directed edges, you can use magic to turn directed edges into undirected edges, such as directed edges from A to B, with a weight of 7.After casting the m
Difference Between Compiled and Interpreted Languages
怀念故乡的面条
typescript21 - Comparison of Interfaces and Type Aliases
产品经理访谈 | 第五代验证码的创新与背景
scheduleWithFixedDelay和scheduleAtFixedRate的区别
The 16th day of the special assault version of the sword offer
Pyspark机器学习:向量及其常用操作
Error using ts-node
mysql中解决存储过程表名通过变量传递的方法
随机推荐
时时刻刻保持敬畏之心
Advice given by experts with four years of development experience in Flutter tutorial
How to promote new products online?
2. # code comments
Step by step hand tearing carousel Figure 3 (nanny level tutorial)
Flink 1.13 (8) CDC
雪糕和轮胎
UE4 模型OnClick事件不生效的两种原因
ICML2022 | Deep Dive into Permutation-Sensitive Graph Neural Networks
SQL Analysis of ShardingSphere
UE4 制作遇到的问题
Error using ts-node
这里有110+公开的专业数据集
Unity's primary method for implementing PlanarReflection under the BuildIn rendering pipeline
【愚公系列】2022年07月 Go教学课程 025-递归函数
Message queue MySQL table for storing message data
typescript21 - Comparison of Interfaces and Type Aliases
Dynamic Programming 01 Backpack
FFmpeg 搭建本地屏幕录制环境
typescript27 - what about enumeration types