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

原网站

版权声明
本文为[Java full stack preacher]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141420588871.html