当前位置:网站首页>What are the SQL add / delete / modify queries?
What are the SQL add / delete / modify queries?
2022-06-30 09:04:00 【Have a promising future】
SQL Can not do without adding, deleting, modifying and checking , Part of this article SQL It may not belong to “ Additions and deletions ” Range , therefore , This article only represents the personal idea that “ Additions and deletions ” To summarize
One 、 increase
database
- Create database
CREATE DATABASE Database name ;
View
- Create a single table view
CREATE VIEW View name AS SELECT Data table fields FROM Data table name ;
- Create a multi table view
CREATE VIEW View name AS SELECT surface 1. Field , surface 2. Field FROM surface 1, surface 2 WHERE Conditions ;
- Insert a piece of data
INSERT INTO View name VALUES ( Insert value );
- Insert multiple data
INSERT INTO View name ( Insert sub segment ) VALUES ( Insert value ),( Insert value );
Data sheet
- Create data table
CREATE TABLE Table name (
Column name 1 data type ,
Column name 2 data type ,
.......
);
- Add fields
ALTER TABLE Table name Field name Field data type ;
- Insert a piece of data
INSERT INTO Table name VALUES ( Insert value );
- Insert the value of the corresponding field
INSERT INTO Table name ( Field ) VALUE ( Insert value );
- Insert multiple data
INSERT INTO Table name ( Field ) VALUES ( Insert value ),( Insert value );
Two 、 Delete
database
- Delete database
DROP DATABASE Table name ;
View
- Delete data
DELETE FROM View name WHERE Conditions ;
Data sheet
- Delete data table
DROP TABLE Table name ;
- Delete field
ALTER TABLE Table name DROP COLUMN Field name ;
- Delete data ( Remember to add WHERE Clause , nothing WHERE Clause is to delete all the data in the table by default )
DELETE FROM Table name WHERE Conditions ;
- Delete table data
TRUNCATE TABLE Table name ;
DROP、TRUNCATE、DELETE The difference between
3、 ... and 、 Change
database
- Modify the database name
ALTER DATABASE Original name MODIFY NAME = New name ;
View
- Modify view fields
CREATE OR REPLACE VIEW View name AS SELECT * FROM Data table name ;
or
ALTER VIEW View name AS SELECT * FROM Data table name ;
- Modify view data
UPDATE View name SET Fields that need to be modified = ' Modified value ';
Data sheet
- Modify the name of the table
ALTER TABLE Original table name RENAME TO The new name of the table ;
- Modify field name
ALTER TABLE Table name CHANGE Original field name new field name New field data type ;
- Modify field data type
ALTER TABLE Table name ALTER COLUMN Field data type ;
- Modify the data of a field in the table , Multiple fields are separated by commas ( Remember to add WHERE Clause , nothing WHERE Clause is to modify all contents of the field by default )
UPDATE Table name SET Field = '' WHERE Conditions ;
Four 、 check
database
- view the database
SHOW DATABASES;
View
- View view structure
DESC View name ;
or
DESCRIBE View name ;
- Check out the SQL sentence
SHOW CREATE VIEW View ;
- View view
SELECT * FROM View name ;
or
SHOW CREATE VIEW View name ;
or
SHOW TABLE STATUS LIKE ' View name ';
Data sheet
- View table structure
DESC Table name ;
or
DESCRIBE Table name ;
- View the SQL sentence
SHOW CREATE TABLE Table name ;
- View table data
SELECT * FROM Table name ;
- View the specified field data
SELECT Specify the field name FROM Table name ;
- View matching fields 1 Indexes ( Conditions ) The data of
SELECT * FROM Table name WHERE Conditions ;
- View the matching fields 1、 Field 2 Indexes ( Conditions ) The data of , Intermediate use AND Connected to a
SELECT * FROM Table name WHERE Conditions 1 AND Conditions 2;
- View matching fields 1、 Field 2 One of the indexes ( Conditions ) The data of , Intermediate use OR Connected to a
SELECT * FROM The name of the table WHERE Conditions 1 OR Conditions 2;
- View matching fields 1 Indexes , And match the field 2、 Field 3 One of the indexes ( Conditions ) The data of , use AND While connecting, you should also OR Enclosed in brackets
SELECT * FROM The name of the table WHERE Conditions 1 AND ( Conditions 2 OR Conditions 3);
- Internal connection (INNER )JOIN
SELECT * FROM surface 1, surface 2 WHERE surface 1. Field = surface 2. Field
or
SELECT * FROM surface 1 JOIN surface 2 ON surface 1. Field = surface 2. Field
or
SELECT * FROM surface 1 INNER JOIN surface 2 ON surface 1. Field = surface 2. Field
-- Use... For multiple conditions And Connected to a
SELECT * FROM surface 1 INNER JOIN surface 2 ON Conditions 1 AND Conditions 2
- Left connection LEFT JOIN
SELECT * FROM Table name 1 LEFT JOIN Table name 2 ON Connection condition ( Multi conditional use AND or OR Connect )
- The right connection RIGHT JOIN
SELECT * FROM Table name 1 RIGHT JOIN Table name 2 ON Connection condition ( Multi conditional use AND or OR Connect )
- Group query GROUP BY
SELECT * FROM Table name GROUP BY Group query fields ;
- Group query Filter grouping data GROUP BY + HAVING
SELECT * FROM Table name GROUP BY TIME HAVING Field = '';
- Group query , Get the grouped set GROUP BY + GROUP_CONCAT
SELECT GROUP_CONCAT( Fields that require a collection ) FROM images GROUP BY Grouped fields ;
- Ascending query ASC
SELECT * FROM Table name ORDER BY Fields for ascending query ASC;
- Descending query DESC
SELECT * FROM Table name ORDER BY Fields queried in descending order DESC;
- The query returns the specified number LIMIT
SELECT * FROM Table name LIMIT Limit the number of returned entries ;
or
SELECT * FROM Table name LIMIT Limit the number of returned entries , To which article ;
- Fuzzy query LIKE
SELECT * FROM Table name WHERE Field LIKE '% value %';
- nested queries
SELECT * FROM Table name 1 WHERE Table name 1 Field = (SELECT Table name 2 Field FROM Table name 2 WHERE Conditions );
Function query
Aggregate functions
- Query average AVG
SELECT AVG( Field ) FROM Table name ;
- Number of query table rows COUNT
SELECT COUNT(*) FROM Table name ;
- Query maximum MAX
SELECT MAX( Field ) FROM Table name ;
- Query minimum MIN
SELECT MIN( Field ) FROM Table name ;
- Sum up SUM
SELECT SUM( Field ) FROM Table name ;
Date function
- Query the current date
-- MySQL usage :
SELECT NOW();
-- SQL Server usage :
SELECT GETDATE();
- Query the difference between dates
-- MYSQL usage :
SELECT DATEDIFF( date 1, date 2);
-- SQL Server
-- The return format can be year 、 quarter 、 month 、 Day, etc. , For details, please refer to :https://www.w3school.com.cn/sql/func_datediff.asp
SELECT DATEDIFF( Returns the format , date 1, date 2);
- Query the specified date year
-- result :2021
SELECT YEAR('2021-05-01');
- Query the specified date and month
-- result :5
SELECT MONTH('2021-05-01');
- Query the specified date and days
-- result :1
SELECT DAY('2021-05-01');
- Query the day of the week when a date is
-- result :4
SELECT DAYOFWEEK('2021-04-28');
Summary :
1、 Data table and view are related , When the view ( Data sheet ) Data deletion , Corresponding data sheet ( View ) Data will also be deleted .
2、 About functions , Only a few are listed here , And string functions 、 System function 、 There was no introduction to mathematical functions , If you are interested, you can find out by yourself .
High EQ : These are just what I know SQL The tip of the iceberg , It's just a simple use , complex SQL I seldom use it .
Low EQ : That's all I know !
边栏推荐
- Opencv learning notes-day9 opencv's own color table operation (colormap coloraptypes enumeration data types and applycolormap() pseudo color function)
- Find the number that appears only once in the array
- How can I get the discount for opening a securities account? Is online account opening safe?
- List set export excel table
- Implementing custom drawer component in quick application
- Flink 数据偶尔数据积压导致checkpoint失败
- [untitled]
- [protobuf] protobuf generates cc/h file through proto file
- Esp32 (IX): OTA function of function development
- Flink Sql -- toAppendStream doesn‘t support consuming update and delete changes which
猜你喜欢
![[data analysis and display]](/img/86/19260ee664769c98588d8b0783ef28.jpg)
[data analysis and display]

Bottomsheetbehavior principle of realizing the home page effect of Gaode map

Esp32 (4): overview of the overall code architecture

Opencv learning notes -day13 pixel value statistics calculation of maximum and minimum values, average values and standard deviations (use of minmaxloc() and meanstddev() functions)

Opencv learning notes -day 11 (split() channel separation function and merge() channel merge function)

Opencv learning notes-day14 drawing of image geometry (rect class rotatedrect class, rectangle drawing rectangle circle drawing circular function line drawing line function ellipse drawing elliptic fu

Introduction to the runner of mmcv

Duplicate entry '2' for key 'primary appears in JPA‘

Interpretation of source code demand:a rotation equivariant detector for aerial object detection

Do you want the dialog box that pops up from the click?
随机推荐
Rew acoustic test (IV): test principle of rew
QT connection to Shentong database
Opencv learning notes -day 11 (split() channel separation function and merge() channel merge function)
Set, map and modularity
Circuit analysis of current probe
CUDA implements matrix replication
快应用中实现自定义抽屉组件
VIM from dislike to dependence (21) -- cross file search
Opencv learning notes-day5 (arithmetic operation of image pixels, add() addition function, subtract() subtraction function, divide() division function, multiply() multiplication function
[data analysis and display]
Deep understanding of kotlin collaboration context coroutinecontext
vim 从嫌弃到依赖(21)——跨文件搜索
挖财开户安全吗?怎么有人说不靠谱。
JVM调优相关命令以及解释
Redis design and Implementation (V) | sentinel sentry
技术管理进阶——管理者如何进行梯队设计及建设
How can we get a satisfactory salary? These routines still need to be mastered
Comparaison de deux façons d'accéder à la base de données SQL Server (sqldatareader vs sqldataadapter)
将线程绑定在某个具体的CPU逻辑内核上运行
Alcohol tester scheme: what principle does the alcohol tester measure alcohol solubility based on?