当前位置:网站首页>MySQL explanation (II)

MySQL explanation (II)

2022-06-26 13:18:00 C chord~

Catalog

One 、SQL Statement Overview

1、SQL Language

2.SQL classification

Two . Check the database structure

1. Visit the library

2. View database information

3. See the table

4. The structure of the display table  

3、 ... and . data type

Four .DDL

1. Create database

2. Create table  

 3. Delete table

4. Delete Library

5、 ... and .DML

 1. Insert new data

 2. modify 、 Update the original data

3. Delete data  

5、 ... and .DQL

 1. Query data

6、 ... and . Modify the indication or table structure

 1. Modify the name of the table

2. Extended table structure

3、 Modify fields ( Column ) name , Add unique key

4、 Delete field 、 Primary key

summary


One 、SQL Statement Overview

1、SQL Language

  •  Structured Query Language Abbreviation , Structured query language
  •   Standard language for relational databases
  •   For maintaining the management database
    • Including data query 、 Data update 、 Access control 、 Object management and other functions

2.SQL classification

DDL	 Data definition language , Used to create database objects , Ruku 、 surface 、 Index, etc. 
DML	 Data manipulation language , It is used to manage the data in the table 
DQL	 Data query language , Used to find eligible data records from the data table 
DCL	 Data control language , Used to set or change database user or role permissions 

Two . Check the database structure

1. Visit the library

mysql -u root -p
 password 

 perhaps 
mysql -u root -p  password 

2. View database information

show  databases;
 In English ; ending 

3. See the table

use  Library name    # First enter the database where the tables are stored 
show tables;  # Display all tables in the current library 

4. The structure of the display table  

describe  database . Table name    
 or 
desc  database . Table name  

3、 ... and . data type

 type 	 explain 
int	     integer ( Define integer type data )
float	 Single precision floating point ,4 byte 32 position , Accurate to six decimal places 
double	 Double precision floating point ,8 byte 64 position 
char	 Fixed length character types , Define character class data 
varchar	 Variable length character types 
text	 Text 
image	 picture 
decimal (5,2)	5 A valid length number , After the decimal point is 2 position , Specifies the length of the array 

Four .DDL

1. Create database

create database   Database name ;

2. Create table  

create table  Table name   ( Field 1  data type , Field 2   data type  .... [primary key( Primary key name )]);

 

field	     Field 
type	     type 
not null	 Not allowed to be empty 
default 	 The default is empty. 
primary key	 Primary key ( Generally, select fields that are not duplicate and not empty )

 3. Delete table

use  Library name 
drop table  Table name ;
 or 
drop table  Library name . indicate ;

4. Delete Library

drop database  Database name ;

 

5、 ... and .DML

 1. Insert new data

insert into  Table name ( Field 1, Field 2[,...]) values( Field 1 Value , Field 2 Value ,...);

 Field , You can use  password(' password ') , Be able to use encrypted passwords 

 

 

 2. modify 、 Update the original data

update  Table name  set  Field name 1= field value 1[, Field name 2= field value 2] [where  Conditional expression ];

notes : When changing data , Need to be careful , It is recommended to use the primary key as the conditional expression option , Because the primary key is unique .

3. Delete data  

delete from  Table name  [where  Conditional expression ];

5、 ... and .DQL

 1. Query data

select  Field name 1, Field name 2[,...] from  Table name  [where  Conditional expression ];
select * from  Table name ;           # Show all 
select  Field 1, Field 2 from  Table name ;  # Show fields 1 And field 2  
select  Field 1 from  Table name \G;      # Display vertically as a list 
select * from  Table name  info limit 2;   # Show only headers 2 That's ok 
select * from  Table name  info limit 2,3; # According to the first 2 Front after row 3 That's ok 

 

 

6、 ... and . Modify the indication or table structure

 1. Modify the name of the table

alter table  The old name of the table  rename  The new name of the table ;

2. Extended table structure

 

alter table  Table name  add  Extension name   data type ;

 

3、 Modify fields ( Column ) name , Add unique key

alter table  Table name  change  Old column names   New column names   data type  [unique key];

#change  Field name can be modified 、 data type 、 Constraints, etc 

 The only key : only , But it can be empty ( Null values can only appear once )

 The primary key contains some properties of a unique key 
 A unique key cannot be used as a primary key 

4、 Delete field 、 Primary key

alter table  Table name  drop  Field name ;

 

summary

  • We do the database daily “ increase 、 Delete 、 Change 、 check ”
  • To create a primary key, you must use a unique
  • where The option suggests using a primary key , Because the only one , Not easy to make mistakes
  • sql Statement is case insensitive
  • sql Statements need to be very precise , Every wrong letter will cause the statement to fail
  • The operation of the database must be cautious

 

 

原网站

版权声明
本文为[C chord~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261227385215.html