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

边栏推荐
- Delayed message center design
- Exercise 7-3 store the numbers in the array in reverse order (20 points)
- Container cloud notes
- 【Day2】 convolutional-neural-networks
- Number of relationship models
- Hands on deep learning (40) -- short and long term memory network (LSTM)
- Debug:==42==ERROR: AddressSanitizer: heap-buffer-overflow on address
- Normal vector point cloud rotation
- If you don't know these four caching modes, dare you say you understand caching?
- 什么是 DevSecOps?2022 年的定义、流程、框架和最佳实践
猜你喜欢

leetcode1-3

leetcode842. Split the array into Fibonacci sequences

【Day1】 deep-learning-basics

BGP advanced experiment

用数据告诉你高考最难的省份是哪里!

Dynamic address book

Normal vector point cloud rotation

A little feeling

Debug:==42==ERROR: AddressSanitizer: heap-buffer-overflow on address

uniapp 处理过去时间对比现在时间的时间差 如刚刚、几分钟前,几小时前,几个月前
随机推荐
Go context basic introduction
Hands on deep learning (38) -- realize RNN from scratch
Kotlin 集合操作汇总
Hands on deep learning (46) -- attention mechanism
Reprint: summation formula of proportional series and its derivation process
Kotlin:集合使用
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
Idea SSH channel configuration
Latex learning insertion number - list of filled dots, bars, numbers
[200 opencv routines] 218 Multi line italic text watermark
Occasional pit compiled by idea
Velodyne configuration command
智慧路灯杆水库区安全监测应用
Exercise 9-4 finding books (20 points)
Today's sleep quality record 78 points
Normal vector point cloud rotation
How to teach yourself to learn programming
Rhcsa - day 13
Realsense d435 d435i d415 depth camera obtains RGB map, left and right infrared camera map, depth map and IMU data under ROS
Leetcode48. Rotate image