当前位置:网站首页>SQL操作数据库语法
SQL操作数据库语法
2022-07-02 06:28:00 【王子良.】
SQL的概念了解和用法
什么是SQL:
Structured Query Language: 结构化查询语言,
其实就是定义了操作所有关系型数据库的规则。
关系型数据库:
Reational DBMS;
SQL的通用语法:
· SQL语句可以单行或多行书写,以分号结尾。
· 可以使用空格和缩进来增强语言的可读性。
· MySql数据库的SQL语句不区分大小写。关键字建议使用大写。
SELECT * FROM student
· 三种注释W
单行注释: – 注释内容 或 # 注释内容 (Mysql特有) 多行注释 /* 注释 */
SQL的分类
SQL的分类:
1): DDL(Data Definition Language)数据定义语言
用来定义数据库对象: 数据库, 表, 列, 等。 关键字: caeate, drop, alter等
2): DML(Data Manipulation Language)数据操作语言
用来对数据库中的数据进行增删改。 关键字: insert, delete, update等
3): DQL(Data Query Language) 数据查询语言
用来查询数据库中表的记录(数据)。 关键字: select, where等。
4): DCL(Data Control Language) 数据控制语言(了解)
用来定义数据库访问权限和安全级别,及创建用户。 关键字: GRANT, REVOKE 等。
第一点 DDL: 操作数据库_创建查询
DDL: 操作数据库、表:
1. 操作数据库: CRUD
C(Create):创建 R(Retrieve): 查询 U(Update): 修改 D(Delete): 删除 使用数据库
C:
* 创建数据库:
* create database 数据库名称;
* 判断一下数据库是否存在。如果不存在我在创建,存在反之。
* create database if not exists 数据库名称;
* 在创建数据库的时候更改字符集(默认utf-8)
* create database 数据库名称 character set 字符集;
* 题目: 创建db4 数据库,判断是否存在,并且设置字符集为gbk;
* create database if not exists db4 character set gbk;
R:
* 查找所有数据库的名称:
* show databases;
* 查看对应的数据库的字符集: *(查看某个数据库的创建语句)
* show create database mysql; SHOW CREATE DATABASE mysql; (mysql 是对应的数据库,可以换成你要查看的数据库。)
U:
* 修改数据库的字符集:
* alter database 数据库名称 character set 字符集名称;
D:
* 删除数据库:
* drop database 数据库名称;
* 判断它存在 如果存在就删。反之不作为:
* drop database if exists 数据库名称;
使用数据库:
* 使用数据库
* use 数据库名称;
* 查询现在正在使用的数据库名称
* select database();
DDL操作表_查询_ 创建_修改_删除
C(Create):创建
* create table 表名(
列名1 数据类型1,
列名2 数据类型2,
...
列名n 数据类型n
);
*** SQL常见的数据类型:
int 整数类型;
double 小数类型;
double(5 ,2); // 小数最多为五位,小数后保留两位.
date 日期类型,只包含年月日的日期类型。 yyyy-MM-dd
datetime 日期类型, 包含 年月日时分秒 yyyy-MM-dd HH:mm:ss;
timestamp 时间戳类型; 包含 年月日时分秒 yyyy-MM-dd HH:mm:ss;
* 如果将来不给这个字段赋值,或者赋值为null;则默认使用当前的系统时间,来自动赋值。
varchar; 字符类型
* name varchar(20); // 最大字符长度为20位;
R(Retrieve): 查询
* 查询某个数据库中所有的表名称:
* show tables;
* 查询表结构:
* desc 表名;
U(Update): 修改
* 修改表名 : alter table 表名 rename to 新表名;
* 查看表的字符集: show create table 表名;
* 修改表的字符集: alter table 表名 character set 字符集;
* 添加一列: alter table 表名 add 列名 数据类型;
* 修改列的名称和类型: alter table 表名 change 旧列名 新列名 数据类型;
* 只改类型: alter table 表名 modify 列名 数据类型;
* 删除列 : alter table 表名 drop 列名;
D(Delete): 删除
* 删除表:
* drop table 表名;
* 判断是否存在在删除;反之不作为:
* drop tabele if exists 表名;
复制表: create table 新表 like 被复制的表;
SQL常用方法(部分持续更新):
建议所有语句使用大写!
*** SQL常见的数据类型:
int 整数类型;
double 小数类型;
double(5 ,2); // 小数最多为五位,小数后保留两位.
date 日期类型,只包含年月日的日期类型。 yyyy-MM-dd
datetime 日期类型, 包含 年月日时分秒 yyyy-MM-dd HH:mm:ss;
timestamp 时间戳类型; 包含 年月日时分秒 yyyy-MM-dd HH:mm:ss;
* 如果将来不给这个字段赋值,或者赋值为null;则默认使用当前的系统时间,来自动赋值。
varchar; 字符类型
* name varchar(20); // 最大字符长度为20位;
创建数据库:
create database 数据库名称;
* 判断一下是否存在。如果不存在我在创建,存在反之。
* create database if not exists 数据库名称;
* 在创建数据库的时候更改字符集(默认utf-8)
* create database 数据库名称 character set 字符集;
删除数据库:
drop database 数据库名称;
* 判断它存在 如果存在就删。反之不作为:
* drop database if exists 数据库名称;
使用数据库:(也可以说是进入数据库)
* use 数据库名称;
* 查询现在正在使用的数据库名称
* select database();
查询所有的数据库:
show databases; SHOW DATABASES;
查看对应的数据库的字符集: *(查看某个数据库的创建语句)
show create database mysql; SHOW CREATE DATABASE mysql; (mysql 是对应的数据库,可以换成你要查看的数据库。)
修改数据库的字符集:
alter database 数据库名称 character set 字符集名称;
对表的操作;
复制表: create table 新表 like 被复制的表;
C(Create):创建
* create table 表名(
列名1 数据类型1,
列名2 数据类型2,
...
列名n 数据类型n
);
R(Retrieve): 查询
* 查询某个数据库中所有的表名称:
* show tables;
* 查询表结构:
* desc 表名;
D(Delete): 删除
* 删除表:
* drop table 表名;
* 判断是否存在在删除;反之不作为:
* drop tabele if exists 表名;
U(Update): 修改
* 修改表名 : alter table 表名 rename to 新表名;
* 查看表的字符集: show create table 表名;
* 修改表的字符集: alter table 表名 character set 字符集;
* 添加一列: alter table 表名 add 列名 数据类型;
* 修改列的名称和类型: alter table 表名 change 旧列名 新列名 数据类型;
* 只改类型: alter table 表名 modify 列名 数据类型;
* 删除列 : alter table 表名 drop 列名;
DML: 增删改表中的数据
边栏推荐
- Mmdetection trains its own data set -- export coco format of cvat annotation file and related operations
- 【Wing Loss】《Wing Loss for Robust Facial Landmark Localisation with Convolutional Neural Networks》
- Correction binoculaire
- 常量指针和指针常量
- 力扣方法总结:查找类
- Gensim如何冻结某些词向量进行增量训练
- Business architecture diagram
- 【双目视觉】双目立体匹配
- I'll show you why you don't need to log in every time you use Taobao, jd.com, etc?
- 【FastDepth】《FastDepth:Fast Monocular Depth Estimation on Embedded Systems》
猜你喜欢

What if a new window always pops up when opening a folder on a laptop

It's great to save 10000 pictures of girls

用于类别增量学习的动态可扩展表征 -- DER

【学习笔记】反向误差传播之数值微分

Using super ball embedding to enhance confrontation training

What if the laptop task manager is gray and unavailable
![[multimodal] clip model](/img/45/8501269190d922056ea0aad2e69fb7.png)
[multimodal] clip model
![[learning notes] matlab self compiled image convolution function](/img/82/43fc8b2546867d89fe2d67881285e9.png)
[learning notes] matlab self compiled image convolution function

应对长尾分布的目标检测 -- Balanced Group Softmax

open3d学习笔记三【采样与体素化】
随机推荐
Open3d learning note 5 [rgbd fusion]
Mmdetection model fine tuning
C#与MySQL数据库连接
STL速查手册
Apple added the first iPad with lightning interface to the list of retro products
【双目视觉】双目立体匹配
【Random Erasing】《Random Erasing Data Augmentation》
【DIoU】《Distance-IoU Loss:Faster and Better Learning for Bounding Box Regression》
Eklavya -- infer the parameters of functions in binary files using neural network
用于类别增量学习的动态可扩展表征 -- DER
Mmdetection trains its own data set -- export coco format of cvat annotation file and related operations
AR系统总结收获
w10升级至W11系统,黑屏但鼠标与桌面快捷方式能用,如何解决
Dynamic extensible representation for category incremental learning -- der
Income in the first month of naked resignation
静态库和动态库
What if the notebook computer cannot run the CMD command
SQLyog远程连接centos7系统下的MySQL数据库
解决jetson nano安装onnx错误(ERROR: Failed building wheel for onnx)总结
What if a new window always pops up when opening a folder on a laptop