当前位置:网站首页>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 表名;
练习
边栏推荐
猜你喜欢
Hackers can how bad to what degree?
How to promote new products online?
typescript21 - Comparison of Interfaces and Type Aliases
微软 Win10 照片磁贴后的又一“刺客”,谷歌 Chrome 浏览器将在新标签页展示用户照片
Progressive Reconstruction of Visual Structure for Image Inpainting 论文笔记
The method of solving stored procedure table name passing through variable in mysql
Typescript22 - interface inheritance
6-23漏洞利用-postgresql代码执行利用
C# | 使用Json序列化对象时忽略只读的属性
API Design Notes: The pimpl trick
随机推荐
Unity's primary method for implementing PlanarReflection under the BuildIn rendering pipeline
Immutable
怀念故乡的月亮
Make your Lottie support word wrapping in text fields
PMP 项目质量管理
JS new fun(); class and instance JS is based on object language Can only act as a class by writing constructors
云服务器下载安装mongo数据库并远程连接详细图文版本(全)
【愚公系列】2022年07月 Go教学课程 024-函数
Mysql基础篇(Mysql数据类型)
Visual Studio提供的 Command Prompt 到底有啥用
How to write a high-quality digital good article recommendation
【kali-信息收集】枚举——DNS枚举:DNSenum、fierce
Passive anti-islanding-UVP/OVP and UFP/OFP passive anti-islanding model simulation based on simulink
button remove black frame
数据比对功能调研总结
What is dynamic programming and what is the knapsack problem
typescript28-枚举类型的值以及数据枚举
Optional parameters typescript19 - object
25. Have you been asked these three common interview questions?
项目风险管理必备内容总结