当前位置:网站首页>SQL operation database syntax
SQL operation database syntax
2022-07-02 08:10:00 【Wang Ziliang】
SQL Concept understanding and usage of
What is? SQL:
Structured Query Language: Structured query language ,
In fact, it defines the rules for operating all relational databases .
Relational database :
Reational DBMS;
SQL The general syntax of :
· SQL Statements can be written in one or more lines , It ends with a semicolon .
· You can use spaces and indents to enhance the readability of the language .
· MySql Database SQL Statement is case insensitive . It is recommended to use uppercase .
SELECT * FROM student
· Three notes W
Single-line comments : – The comment or # The comment (Mysql specific ) Multiline comment /* notes */
SQL The classification of
SQL The classification of :
1): DDL(Data Definition Language) Data definition language
Used to define database objects : database , surface , Column , etc. . keyword : caeate, drop, alter etc.
2): DML(Data Manipulation Language) Data operation language
It is used to add, delete and modify data in the database . keyword : insert, delete, update etc.
3): DQL(Data Query Language) Data query language
Used to query the records of tables in the database ( data ). keyword : select, where etc. .
4): DCL(Data Control Language) Data control language ( understand )
Used to define database access and security levels , And create users . keyword : GRANT, REVOKE etc. .
The first point DDL: Operating the database _ Create a query
DDL: Operating the database 、 surface :
1. Operating the database : CRUD
C(Create): establish R(Retrieve): Inquire about U(Update): modify D(Delete): Delete Using a database
C:
* Create database :
* create database Database name ;
* Determine whether the database exists . If it doesn't exist, I'm creating , Existence contrary .
* create database if not exists Database name ;
* Change the character set when creating the database ( Default utf-8)
* create database Database name character set Character set ;
* subject : establish db4 database , Judge whether it exists , And set the character set to gbk;
* create database if not exists db4 character set gbk;
R:
* Find the names of all databases :
* show databases;
* View the character set of the corresponding database : *( View the creation statement of a database )
* show create database mysql; SHOW CREATE DATABASE mysql; (mysql Is the corresponding database , You can change to the database you want to view .)
U:
* Modify the character set of the database :
* alter database Database name character set Character set name ;
D:
* Delete database :
* drop database Database name ;
* Judge its existence Delete if it exists . On the contrary, inaction :
* drop database if exists Database name ;
Using a database :
* Using a database
* use Database name ;
* Query the name of the database currently in use
* select database();
DDL Operation table _ Inquire about _ establish _ modify _ Delete
C(Create): establish
* create table Table name (
Name 1 data type 1,
Name 2 data type 2,
...
Name n data type n
);
*** SQL Common data types :
int Integer types ;
double Decimal type ;
double(5 ,2); // The maximum number of decimals is five , Keep two decimal places .
date The date type , The date type only includes year, month and day . yyyy-MM-dd
datetime The date type , contain Mm / DD / yyyy HHM / S yyyy-MM-dd HH:mm:ss;
timestamp Timestamp type ; contain Mm / DD / yyyy HHM / S yyyy-MM-dd HH:mm:ss;
* If you don't assign a value to this field in the future , Or assign it to null; The current system time is used by default , To automatically assign .
varchar; Character type
* name varchar(20); // The maximum character length is 20 position ;
R(Retrieve): Inquire about
* Query all table names in a database :
* show tables;
* Query table structure :
* desc Table name ;
U(Update): modify
* Modify the name of the table : alter table Table name rename to The new name of the table ;
* View the character set of the table : show create table Table name ;
* Modify the character set of the table : alter table Table name character set Character set ;
* Add a column : alter table Table name add Name data type ;
* Modify the name and type of the column : alter table Table name change Old column names New column names data type ;
* Just change the type : alter table Table name modify Name data type ;
* Delete column : alter table Table name drop Name ;
D(Delete): Delete
* Delete table :
* drop table Table name ;
* Judge whether it exists in delete ; On the contrary, inaction :
* drop tabele if exists Table name ;
Copy table : create table New table like The copied table ;
SQL Common methods ( Some are continuously updated ):
It is recommended that all statements be capitalized !
*** SQL Common data types :
int Integer types ;
double Decimal type ;
double(5 ,2); // The maximum number of decimals is five , Keep two decimal places .
date The date type , The date type only includes year, month and day . yyyy-MM-dd
datetime The date type , contain Mm / DD / yyyy HHM / S yyyy-MM-dd HH:mm:ss;
timestamp Timestamp type ; contain Mm / DD / yyyy HHM / S yyyy-MM-dd HH:mm:ss;
* If you don't assign a value to this field in the future , Or assign it to null; The current system time is used by default , To automatically assign .
varchar; Character type
* name varchar(20); // The maximum character length is 20 position ;
Create database :
create database Database name ;
* Judge whether there is . If it doesn't exist, I'm creating , Existence contrary .
* create database if not exists Database name ;
* Change the character set when creating the database ( Default utf-8)
* create database Database name character set Character set ;
Delete database :
drop database Database name ;
* Judge its existence Delete if it exists . On the contrary, inaction :
* drop database if exists Database name ;
Using a database :( It can also be said to enter the database )
* use Database name ;
* Query the name of the database currently in use
* select database();
Query all databases :
show databases; SHOW DATABASES;
View the character set of the corresponding database : *( View the creation statement of a database )
show create database mysql; SHOW CREATE DATABASE mysql; (mysql Is the corresponding database , You can change to the database you want to view .)
Modify the character set of the database :
alter database Database name character set Character set name ;
Operation of the watch ;
Copy table : create table New table like The copied table ;
C(Create): establish
* create table Table name (
Name 1 data type 1,
Name 2 data type 2,
...
Name n data type n
);
R(Retrieve): Inquire about
* Query all table names in a database :
* show tables;
* Query table structure :
* desc Table name ;
D(Delete): Delete
* Delete table :
* drop table Table name ;
* Judge whether it exists in delete ; On the contrary, inaction :
* drop tabele if exists Table name ;
U(Update): modify
* Modify the name of the table : alter table Table name rename to The new name of the table ;
* View the character set of the table : show create table Table name ;
* Modify the character set of the table : alter table Table name character set Character set ;
* Add a column : alter table Table name add Name data type ;
* Modify the name and type of the column : alter table Table name change Old column names New column names data type ;
* Just change the type : alter table Table name modify Name data type ;
* Delete column : alter table Table name drop Name ;
DML: Add or delete data in the table
边栏推荐
猜你喜欢
w10升级至W11系统,黑屏但鼠标与桌面快捷方式能用,如何解决
In the era of short video, how to ensure that works are more popular?
用于类别增量学习的动态可扩展表征 -- DER
Replace self attention with MLP
CVPR19-Deep Stacked Hierarchical Multi-patch Network for Image Deblurring论文复现
樂理基礎(簡述)
Jetson nano installation tensorflow stepping pit record (scipy1.4.1)
Replace convolution with full connection layer -- repmlp
Use of OpenCV 6.4 median filter
乐理基础(简述)
随机推荐
将恶意软件嵌入到神经网络中
[learning notes] matlab self compiled Gaussian smoother +sobel operator derivation
Several methods of image enhancement and matlab code
How to back up the configuration before the idea when reinstalling the idea
High school mathematics compulsory one
高中数学必修一
业务架构图
力扣方法总结:查找类
Simply test the two different data transmission methods of content length and chunked
Rhel7 operation level introduction and switching operation
最长等比子序列
AR系统总结收获
常量指针和指针常量
Opencv3 6.3 reduced pixel sampling with filters
SQL server如何卸载干净
JVM instructions
Carsim-問題Failed to start Solver: PATH_ID_OBJ(X) was set to Y; no corresponding value of XXXXX?
Learn to write article format
Replace convolution with full connection layer -- repmlp
使用Matplotlib绘制图表初步