当前位置:网站首页>DDL statement of MySQL Foundation
DDL statement of MySQL Foundation
2022-07-04 10:21:00 【Java full stack preacher】
Explain SQL Three categories of statements and SQL Getting started .
Using the database is :MySQL 8.0.27
1.SQL classification
DDL(Data Definition Language) sentence : Data definition statement .
purpose : It's right Database objects ( database 、 surface 、 Column 、 Index, etc. ) Create 、 Delete 、 Modify etc. .
Common keywords :create、drop、alter etc. .
DML(Data Manipulation Language) sentence : Data manipulation statement .
purpose : Used to add 、 modify 、 Delete and query Database records , And check data integrity .
Common keywords :insert、update、delete、select etc. .
DCL(Data Control Language) sentence : Data control statement .
purpose : Control database 、 surface 、 Field 、 User Access and security levels .
Common keywords :grant、revoke etc.
2.DDL sentence
DDL Statements are used to manipulate database objects , The database object contains the database itself (database)、 surface (table)、 Column (column)、 Indexes (index)、 constraint (constraint)、 View (view)、 trigger (trigger)、 stored procedure (StoredProcedure) And the function (function).
This introductory course only covers databases (database) And library table (table).
Why do you always put parentheses after Chinese and English explanations ? because sql Sentence to use , Look familiar first .
Full command operation , Let attention focus more on instruction input and result output .
2.1.DDL The operation of library
Connect to the database first
1) Create database
create database mydb;
This is the simplest statement to create a database .
2) Check which databases are currently connected
show databases;
You can see that in addition to the newly created mydb There are other databases .
Briefly explain why these databases are used .
User base : It's what I just created mydb
System libraries :
-- Every user can see their own
information_schema: Storage system database object information , Contains user table information 、 Column information 、 Permission information 、 Character set information 、 Partition information, etc .
-- Ordinary users can't see
performance_schema: Dynamic parameter table related to storage system performance
-- Ordinary users can't see
sys: be based on information_schema and performance_schema above , Encapsulates a system view that is easier to tune and diagnose ( View of system layer )
-- Ordinary users can't see
mysql: User rights information of the storage system
3) Query database SQL sentence
show create database mydb \G;
4) Using a database
use mydb;
Only choose to use mydb database , Before we can start to mydb Perform operations such as creating database tables
5) Delete database
drop database mydb;
You can see mydb It has been deleted .
Only this and nothing more , You have learned how to Create database , Using a database , Check which databases are currently connected and delete databases .
2.2.DDL The operation of surface
First delete the previous mydb database , Create it back and use mydb, Check it out again mydb Is there a table in the library
No library table found
1) Create database tables
create table tablename(
Name data type constraint ,
Name data type constraint
);
establish person surface
create table person( id int primary key,name varchar(30),age int default 0 );
2) View table design
desc person;
3) Look at building tables SQL sentence
Check database and table creation SQL Statements are similar , The database objects queried are different .
"\G" The meaning of options is to arrange vertically according to fields , In order to better read the query results .
show create table person \G;
You can see , Although we didn't specify Character encoding and storage engine , But the default storage engine has been specified after the table creation statement InnoDB And character encoding utf8mb4.
4) Modify table name
keyword rename
alter table The old name of the table rename The new name of the table ;
5) Delete table
drop table people;
For subsequent operations , Then build the table back ……
6) Modify table fields
-- Modify the table field type
alter table Table name modify Name New type of column ;
-- Add table field
alter table Table name add column Name The type of the column ;
-- Delete table fields
alter table Table name drop column Name ;
-- Modify field name
alter table Table name change Old column names New column names The type of the column ;
-- Modify field order
Add... After adding table fields or modifying table fields :first or after Field name
Such as :alter table Table name add column Name The type of the column after Name 2;
Such as :alter table Table name change Old column names New column names The type of the column first;
All operations are shown in the figure in sequence
Only this and nothing more , You have learned Build table 、 Look up the table 、 Delete table 、 Change table and change table fields operation
DDL Getting started , Learned to Databases and tables Basic operation .
2.3.DDL The operation of Indexes
Index is to sort the indexed data in the database in advance , It's like a book catalog , Can optimize query speed , But it will reduce the speed of addition, deletion and modification , Take up disk space .
1) Add index
Used to create indexes when creating tables
//create Only these two indexes can be added ;
CREATE INDEX index_name ON table_name (column_list)
CREATE UNIQUE INDEX index_name ON table_name (column_list)
Used to create an index after the table is built ( Commonly used )
-- Increase the primary key index : The same value... Is not allowed
alter table tab_name add primary key (column_list);
-- Add unique index : You can't have the same value , There can be NULL value
alter table tab_name add unique (column_list);
-- Add common index : Allow the same index content
alter table tab_name add index index_name(column_list);
-- Add union index : Build multiple fields into an index , The combination of column values must be unique
alter table tab_name add index index_name (column,column,...);
2) Look at the index
-- See the table tab_name All index information for ( Such as : Is the index unique , Is it allowed to be null, Index stored data type and other information )
show index from tab_name;
3) Delete index
alter table tab_name drop index index_name;alter table tab_name drop primary key;
3. summary
up to now , Can pass DDL Statement to manipulate database objects : library 、 surface 、 Indexes .
Pay attention to the official account, learn more about the knowledge of the database and get the database free e-book. .
边栏推荐
- 如果不知道這4種緩存模式,敢說懂緩存嗎?
- Differences among opencv versions
- Hands on deep learning (38) -- realize RNN from scratch
- 【OpenCV 例程200篇】218. 多行倾斜文字水印
- What are the advantages of automation?
- Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 2
- Kotlin 集合操作汇总
- 入职中国平安三周年的一些总结
- Latex insert picture, insert formula
- Exercise 7-2 finding the maximum value and its subscript (20 points)
猜你喜欢
Intelligent gateway helps improve industrial data acquisition and utilization
Advanced technology management - how to design and follow up the performance of students at different levels
Hands on deep learning (45) -- bundle search
Matlab tips (25) competitive neural network and SOM neural network
六月份阶段性大总结之Doris/Clickhouse/Hudi一网打尽
Hands on deep learning (44) -- seq2seq principle and Implementation
Hands on deep learning (III) -- Torch Operation (sorting out documents in detail)
libmysqlclient. so. 20: cannot open shared object file: No such file or directory
Number of relationship models
Some summaries of the third anniversary of joining Ping An in China
随机推荐
Dynamic address book
Reprint: summation formula of proportional series and its derivation process
Uniapp--- initial use of websocket (long link implementation)
How can people not love the amazing design of XXL job
Matlab tips (25) competitive neural network and SOM neural network
Baidu R & D suffered Waterloo on three sides: I was stunned by the interviewer's set of combination punches on the spot
查看CSDN个人资源下载明细
Number of relationship models
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
用数据告诉你高考最难的省份是哪里!
Es advanced series - 1 JVM memory allocation
AUTOSAR from getting started to mastering 100 lectures (106) - SOA in domain controllers
Differences among opencv versions
Vs201 solution to failure to open source file HPP (or link library file)
leetcode1229. Schedule the meeting
C language pointer classic interview question - the first bullet
Rhcsa day 10 operation
Development guidance document of CMDB
Press the button wizard to learn how to fight monsters - identify the map, run the map, enter the gang and identify NPC
Servlet基本原理与常见API方法的应用