当前位置:网站首页>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;
边栏推荐
- log level and print log note
- Why don't you programmers make a living off your own projects?And have to work for someone else?
- Daily practice of LeetCode - palindrome structure of OR36 linked list
- SIP协议标准和实现机制
- [Swift]自定义点击APP图标弹出的快捷方式
- How Zotero removes auto-generated tags
- 《DeepJIT: An End-To-End Deep Learning Framework for Just-In-Time Defect Prediction》论文笔记
- Port inspection steps - 7680 port analysis - Dosvc service
- (5) final, abstract class, interface, inner class
- open failed: EACCES (Permission denied)
猜你喜欢

(5) final, abstract class, interface, inner class

Component pass value provide/inject

(八)Math 类、Arrays 类、System类、Biglnteger 和 BigDecimal 类、日期类

Ambiguous method call.both

LeetCode每日一练 —— OR36 链表的回文结构

Notes on the establishment of the company's official website (6): The public security record of the domain name is carried out and the record number is displayed at the bottom of the web page

type_traits metaprogramming library learning

Detailed explanation of TCP (2)

(五)final、抽象类、接口、内部类

C语言从入门到如土——数据的存储
随机推荐
Postgresql 15 source code analysis (5) - pg_control
els block to the left to move the conditional judgment
扫雷游戏(c语言写)
Safety 20220715
LocalDate加减操作及比较大小
Can‘t load /home/Iot/.rnd into RNG
"A daily practice, happy water problem" 1331. Array serial number conversion
type_traits元编程库学习
Just debuted "Fight to Fame", safety and comfort are not lost
[C language] General method of base conversion
Detailed explanation of TCP (2)
[C language] Preprocessing operation
Distributed locks and three implementation methods
mysql基础知识(二)
【小土堆补充】Pytorch学习笔记_Anaconda虚拟环境使用
Pytest电商项目实战(上)
「 每日一练,快乐水题 」1331. 数组序号转换
[C language] General method of expression evaluation
(4) Recursion, variable parameters, access modifiers, understanding main method, code block
SQL Interview Questions (Key Points)