当前位置:网站首页>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. .

边栏推荐
- 【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
- Leetcode48. Rotate image
- System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
- Dynamic memory management
- Exercise 9-5 address book sorting (20 points)
- Work order management system OTRs
- Static comprehensive experiment ---hcip1
- The time difference between the past time and the present time of uniapp processing, such as just, a few minutes ago, a few hours ago, a few months ago
- 基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 1
- Golang Modules
猜你喜欢

leetcode1-3

The future education examination system cannot answer questions, and there is no response after clicking on the options, and the answers will not be recorded

5g/4g wireless networking scheme for brand chain stores

C language pointer classic interview question - the first bullet

Three schemes of ZK double machine room
What are the advantages of automation?

Machine learning -- neural network (IV): BP neural network

Normal vector point cloud rotation

Hands on deep learning (43) -- machine translation and its data construction

Custom type: structure, enumeration, union
随机推荐
Legion is a network penetration tool
Ruby time format conversion strftime MS matching format
Button wizard business running learning - commodity quantity, price reminder, judgment Backpack
Idea SSH channel configuration
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
Application of safety monitoring in zhizhilu Denggan reservoir area
Exercise 9-1 time conversion (15 points)
Differences among opencv versions
Use C to extract all text in PDF files (support.Net core)
用数据告诉你高考最难的省份是哪里!
Baidu R & D suffered Waterloo on three sides: I was stunned by the interviewer's set of combination punches on the spot
AUTOSAR从入门到精通100讲(106)-域控制器中的SOA
Velodyne configuration command
Container cloud notes
Hands on deep learning (39) -- gating cycle unit Gru
Exercise 7-8 converting strings to decimal integers (15 points)
智慧路灯杆水库区安全监测应用
Hands on deep learning (III) -- Torch Operation (sorting out documents in detail)
六月份阶段性大总结之Doris/Clickhouse/Hudi一网打尽
MySQL case