当前位置:网站首页>MySQL based operations
MySQL based operations
2022-07-31 04:01:00 【Hi teacher, I'm Dabai】
创建数据库
CREATE DATABASE 数据库名;
# 实际操作
mysql> CREATE DATABASE ncayudb;
Query OK, 1 row affected (0.00 sec)
mysql>
# 编码
utf8mb4
# 排序规则
utf8mb4_unicode_ci
# 创建数据库的时候,使用这个命令创建数据库
CREATE DATABASE `ncayu` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE DATABASE `ncayuDB` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
DDL:
DDL(Data Definition Language)数据定义语言:
适用范围:对数据库中的某些对象(例如,database,table)进行管理,如Create,Alter和Drop.truncate
DDL操作是隐性提交的,不能rollback!
DDL代表数据定义语言,It defines the database structure or database schema,Additional properties of data defined in the database can be defined as domains;Tools are also provided to specify some constraints to maintain data consistency.DDLCommands help create the structure of databases and other database objects.Its commands are automatically committed;因此,Changes will be permanently saved in the database.
一些DDL命令包括create,drop,alter,truncate和rename.createCommands help create new databases and tables;dropCommands help to drop databases and tables;alterCommands help modify existing database objects;truncateThe command is used to delete everything in the table;renameCommands are used to rename content in the database.These are some common onesDDL命令.
# DDL(数据定义语言,Data Definition Language)
建库、建表、设置约束等:create\drop\alter
### 1、创建数据库:
create database IF NOT EXISTS ncayu CHARACTER SET utf8;
### 2、创建表格:
use ncayu;
create table IF NOT EXISTS students(
id int,
name varchar(30),
age int
);
### 3、更改表结构(设置约束)
desc students; //查看表结构
alter table students drop column age;
alter table students add column age int;
### 4、删除表、删除数据库
drop table students;
drop database students;
基础操作
###创建数据库:
create database 数据库名;
### 显示所有的数据库:
show databases;
### 删除数据库:
drop database 数据库名;
###使用数据库:
use 数据库名;
### 创建表:
create table 表名(
字段1的名字 数据类型(长度) [not null] [primary key] [auto_increment],
字段2的名字 数据类型(长度) [default ‘默认值’]
)
### 使用表:
Change the type and length of the field: alter table 表名 modify The type of field to change(长度);
Change the name, type and length of the field: alter table 表名 change 旧字段名 新字段名 类型(长度);
添加字段:alter table 表名 add 新字段名 类型(长度);
删除字段:
Alter table 表名 drop column 字段的名字;
### A single statement contains multiple modification operations
ALTER TABLE 表名 操作1, 操作2, ..., 操作n;
### Write three sentences together:
alter table 表名 modify The type of field to change(长度),change 旧字段名 新字段名 类型(长度),add 新字段名 类型(长度);
### 修改表名:
Rename table 旧表名 to 新表名;
### 设置外键:
Alter table The name of the table to set the foreign key to add constraint 外键的名字 foreign key(关联的字段名) references 关联的表名(关联的字段名)
### 删除外键:
Alter table The name of the table whose foreign key is to be dropped drop foreign key 外键的名字;
### 删除字段:
Alter table 表名 drop column 字段的名字;
### 删除表:
Drop table 表名;
DML:
DML(Data Manipulation Language)数据操纵语言:
适用范围:对数据库中的数据进行一些简单操作,如insert,delete,update,select等.
DML操作是需要手动控制事务的开启、提交(commit)和回滚的.
DML代表数据操作语言,The schema it creates(表)Use data manipulation language to populate.DDLPopulate the rows of the table,each line is calledTuple.使用DML,You can insert,修改,Delete and retrieve information from tables.DMLCommands help manage the data stored in the database.但是,DMLCommands are not automatically committed.因此,Changes are not permanent.因此,The operation can be rolled back.
一些DML命令包括insert,update,delete和select.insertCommands help store new records or rows into a table;updateCommands help modify existing records in a table;deleteCommand allows to delete a record or a group of records from a table;selectCommands allow to retrieve specific records from one or more tables
### 插入数据:
1)All fields are inserted:insert into 表名 values(字段值1,字段值2…);
2)Select the appropriate insert:insert into 表名(字段1,字段2) values(字段值1,字段值2…);
3)插入多行数据:insert into 表名 values(字段值1,字段值2…),(字段值1,字段值2…)…;
### INSERT语句注意事项:
to string typechar、varchar、textAnd when inserting data into a date field,Field values are enclosed in single quotes.
To auto-incrementauto_increment字段插入数据时,Insertion is recommendedNULL值,The next number will be inserted into the auto-incrementing field.
When inserting data into a default value constraint field,Field values can be useddefault关键字,Indicates that the default value of the field is inserted.
When inserting a new record,Need to pay attention to the foreign key constraint relationship between tables,In principle first(父)表插入数据,Then again from(子)表插入数据.
### 修改数据:
Update 表名 set 要修改的字段名1=新的值,要修改的字段名2=新的值… where 主键的字段名=The primary key value corresponding to the value to be modified
### 删除数据:
删除一条:delete from 表名 where 主键的字段名=The primary key value corresponding to the value to delete
删除所有:delete from 表名
### 复制表:
Create table 新表名 like 旧表名;
### 复制表数据:
Insert into 新表名 select * from 旧表名;
To delete associated foreign key data:
Set to when delete when creating a foreign key:set null /cascade
修改列排列位置
If we feel there is something wrong with the order of the current columns,You can use the following statements to modify:
### Make column the first column of the table:
ALTER TABLE 表名 MODIFY 列名 列的类型 列的属性 FIRST;
例如:ALTER TABLE first_table MODIFY second_column1 VARCHAR(2) FIRST;
### Put the column after the specified column:
ALTER TABLE 表名 MODIFY 列名 列的类型 列的属性 AFTER 指定列名;
例如:ALTER TABLE first_table MODIFY second_column1 VARCHAR(2) AFTER first_column;
边栏推荐
- Redis 统计用户新增和留存
- type_traits metaprogramming library learning
- 《DeepJIT: An End-To-End Deep Learning Framework for Just-In-Time Defect Prediction》论文笔记
- TCP和UDP详解
- 扫雷游戏(c语言写)
- Key Technologies of Interface Testing
- MATLAB/Simulink&&STM32CubeMX工具链完成基于模型的设计开发(MBD)(三)
- 日志级别 和 打印log注意
- 三子棋的代码实现
- Summary of Huawei Distributed Storage FusionStorage Knowledge Points [Interview]
猜你喜欢
(五)final、抽象类、接口、内部类
Regarding the primary key id in the mysql8.0 database, when the id is inserted using replace to be 0, the actual id is automatically incremented after insertion, resulting in the solution to the repea
A brief introduction to the CheckboxListTile component of the basic components of Flutter
What skills do I need to learn to move from manual testing to automated testing?
Know the showTimePicker method of the basic components of Flutter
Daily practice of LeetCode - 138. Copy a linked list with random pointers
Unity2D 自定义Scriptable Tiles的理解与使用(四)——开始着手构建一个基于Tile类的自定义tile(下)
idea工程明明有依赖但是文件就是显示没有,Cannot resolve symbol ‘XXX‘
(八)Math 类、Arrays 类、System类、Biglnteger 和 BigDecimal 类、日期类
Postgresql 15 source code analysis (5) - pg_control
随机推荐
RESTful api接口设计规范
LeetCode每日一练 —— OR36 链表的回文结构
Can't load /home/Iot/.rnd into RNG
Detailed explanation of TCP and UDP
[Paper reading] Mastering the game of Go with deep neural networks and tree search
"A daily practice, happy water problem" 1331. Array serial number conversion
SIP Protocol Standard and Implementation Mechanism
IDEA comment report red solution
(tree) Last Common Ancestor (LCA)
强化学习:从入门到入坑再到拉屎
进程间通信
[CV project debugging] CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT problem
MySQL修改root账号密码
Port inspection steps - 7680 port analysis - Dosvc service
IDEA common shortcut keys and plug-ins
安全20220709
Daily practice of LeetCode - palindrome structure of OR36 linked list
LocalDate addition and subtraction operations and comparison size
(六)枚举、注解
VS QT - ui does not display newly added members (controls) || code is silent