当前位置:网站首页>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 表名;
练习

边栏推荐
- 数据比对功能调研总结
- 故乡的素描画
- MySQL3
- Flutter "Hello world" program code
- 项目风险管理必备内容总结
- UE4 模型OnClick事件不生效的两种原因
- Write a method to flatten an array and deduplicate and sort it incrementally
- 云服务器下载安装mongo数据库并远程连接详细图文版本(全)
- Advice given by experts with four years of development experience in Flutter tutorial
- typescript26-字面量类型
猜你喜欢

High Numbers | 【Re-integration】Line Area Score 880 Examples

MySQL3

MLP neural network, GRNN neural network, SVM neural network and deep learning neural network compare and identify human health and non-health data

怀念故乡的月亮

MySQL3

Make your Lottie support word wrapping in text fields

Pyspark机器学习:向量及其常用操作

使用ts-node报错

风险策略调优中重要的三步分析法

typescript24-类型推论
随机推荐
Unity's primary method for implementing PlanarReflection under the BuildIn rendering pipeline
The Flow Of Percona Toolkit pt-table-checksum
scheduleWithFixedDelay和scheduleAtFixedRate的区别
基于STM32设计的UNO卡牌游戏(双人、多人对战)
Character encoding and floating point calculation precision loss problem
Flutter "Hello world" program code
7 行代码搞崩溃 B 站,原因令人唏嘘!
safari浏览器怎么导入书签
数组问题之《两数之和》以及《三数之和 》
leetcode6132. Make all elements in an array equal to zero (simple, weekly)
What is a programming language
Introduction to Oracle
【目标检测】YOLOv7理论简介+实践测试
Unknown Bounded Array
[FPGA tutorial case 43] Image case 3 - image sobel edge extraction through verilog, auxiliary verification through MATLAB
Advice given by experts with four years of development experience in Flutter tutorial
C# | 使用Json序列化对象时忽略只读的属性
typescript23-元组
Typescript22 - interface inheritance
typescript22-接口继承