当前位置:网站首页>MySQL learning summary 7: create and manage databases, create tables, modify tables, and delete tables
MySQL learning summary 7: create and manage databases, create tables, modify tables, and delete tables
2022-06-13 03:23:00 【koping_ wu】
Mysql Learning summary seven : Create and manage databases 、 Create table 、 Modify table 、 Delete table
1、 Create and manage databases
1.1 Create database
Determine whether the database already exists , If it does not exist, create the database :
CREATE DATABASE IF NOT EXISTS Database name ;
1.2 Using a database
View all current databases :
SHOW DATABASES; # There is one S, Representing multiple databases
View the database currently in use :
SELECT DATABASE(); # One used mysql Global functions in
View all tables under the specified library :
SHOW TABLES FROM Database name ;
View database creation information :
SHOW CREATE DATABASE Database name ;
Use / Switch database :
USE Database name ;
1.3 modify the database
Change the database character set :
ALTER DATABASE Database name CHARACTER SET Character set ; # such as :gbk、utf8 etc.
1.4 Delete database
Delete the specified database :
DROP DATABASE IF EXISTS Database name ;
2、 Create table
2.1 How it was created 1: Custom creation
Must have :
- CREATE TABLE jurisdiction
- Storage space
The syntax is as follows :
CREATE TABLE [IF NOT EXISTS] Table name (
Field 1, data type [ constraint condition ] [ The default value is ],
Field 2, data type [ constraint condition ] [ The default value is ],
Field 3, data type [ constraint condition ] [ The default value is ],
......
[ Table constraints ]
);
2.2 How it was created 2: Create using an existing table
- Use AS subquery Options , Combine creating tables with inserting data
- The specified column should correspond to the column in the subquery one by one
- Define columns by column names and default values
eg: Put the employee list employees Make a backup :
CREATE TABLE employees_backup AS SELECT * FROM employees;
2.3 Look at the data table structure
stay MySQL After creating the data table in , You can view the structure of the data table .MySQL Support use DESCRIBE/DESC Statement to view the data table structure , Also supports the use of SHOW CREATE TABLE Statement to view the data table structure .
SHOW CREATE TABLE Table name \G
Use SHOW CREATE TABLE Statement can not only view the detailed statement when the table is created , You can also view the storage engine and character encoding .
3、 Modify table
3.1 Append a column
The syntax is as follows :
ALTER TABLE Table name ADD 【COLUMN】 Field name Field type 【FIRST|AFTER Field name 】;
3.2 Modify a column
You can modify the data type of the column , length 、 Default values and locations , The syntax is as follows :
ALTER TABLE Table name MODIFY 【COLUMN】 Field name 1 Field type 【DEFAULT The default value is 】【FIRST|AFTER Field name
2】;
3.3 Rename a column
ALTER TABLE Table name CHANGE 【column】 Name New column names New data types ;
3.4 Delete a column
ALTER TABLE Table name DROP 【COLUMN】 Field name
4、 rename table
Mode one : Use RENAME
RENAME TABLE emp
TO myemp;
Mode two :
ALTER table dept
RENAME [TO] detail_dept; -- [TO] It can be omitted
5、 Delete table
DROP TABLE Statement cannot be rolled back
DROP TABLE [IF EXISTS] Data sheet 1 [, Data sheet 2, ..., Data sheet n];
6、 Clear the table
TRUNCATE TABLE sentence : Delete all data in the table + Free the storage space of the table .
TRUNCATE TABLE detail_dept;
TRUNCATE Statement cannot be rolled back , While using DELETE Statement delete data , You can roll back .
SET autocommit = FALSE; # Close auto submit first
DELETE FROM emp2; # Use delete Clear the table
SELECT * FROM emp2; # The data should be empty at this time
ROLLBACK; # Roll back
SELECT * FROM emp2; # The data will be restored
Ali Development Specification :
【 Reference resources 】TRUNCATE TABLE Than DELETE Fast , It also uses less system and transaction log resources , but TRUNCATE No transaction and no trigger TRIGGER, Possible accidents , It is not recommended to use this statement in development code .
explain :TRUNCATE TABLE In function and without WHERE Clause DELETE Same statement .
边栏推荐
- 2-year experience summary to tell you how to do a good job in project management
- Sparksql of spark
- Stack information, GC statistics
- Solution of Kitti data set unable to download
- [JVM Series 5] JVM tuning instance
- C# . NET ASP. Net relationships and differences
- Azure SQL db/dw series (12) -- using query store (1) -- report Introduction (1)
- brew工具-“fatal: Could not resolve HEAD to a revision”错误解决
- Using linked list to find set union
- Three ways to start WPF project
猜你喜欢
[JVM Series 5] JVM tuning instance
Summary of rust language practice
MySQL transaction isolation level experiment
Aggregation analysis of research word association based on graph data
[azure data platform] ETL tool (2) -- azure data factory "copy data" tool (cloud copy)
2-year experience summary to tell you how to do a good job in project management
Sparksql of spark
Time processing class in PHP
MASA Auth - SSO與Identity設計
MySQL 8.0 installation free configuration method
随机推荐
Masa auth - SSO and identity design
How to write product requirements documents
MASA Auth - 从用户的角度看整体设计
Supervisor -- Process Manager
Nuggets new oil: financial knowledge map data modeling and actual sharing
Wechat applet coordinate location interface usage (II) map interface
【 enregistrement pytorch】 paramètre et tampon des variables pytorch. Self. Register Buffer (), self. Register Paramètre ()
Azure SQL db/dw series (9) -- re understanding the query store (2) -- working principle
Data Governance Series 1: data governance framework [interpretation and analysis]
How to Draw Useful Technical Architecture Diagrams
Explode and implode in PHP
The most complete ongdb and neo4j resource portal in history
Use and arrangement of wechat applet coordinate position interface (I)
On the career crisis of programmers at the age of 35
C method parameter: ref
Complex network analysis capability based on graph database
Large attachment fragment upload and breakpoint continuation
Vs Code modify default terminal_ Modify the default terminal opened by vs Code
Alibaba cloud OSS access notes
A data modeling optimization solution for graph data super nodes