当前位置:网站首页>MySQL (III)
MySQL (III)
2022-06-26 06:43:00 【The code family】
1. A function is a program or code that can be called directly by another program .
2. String function
MySQL There are many built-in string functions , The common ones are as follows :
CONCAT(S1,S2,...Sn) String splicing , take S1, S2, Sn Stitching into - A string
LOWER(str) The string str All to lowercase
UPPER(str) The string str Capitalize all
LPAD(str,n,pad) padding-left , Use string pad Yes str Fill the left side of the , achieve n String length
RPAD(str,n,pad) Right fill , Use string pad Yes st Fill the right side of the , achieve n String length
TRIM(str) Remove the spaces at the beginning and end of the string
SUBSTRING(str,start,len) Return from string str from start It's from the position len A string of length
3. Numerical function
Common numerical functions are as follows :
CEIL(x) Rounding up
FLOOR(x) Rounding down
MOD(x,y) return x/y The mold
RAND) return 0~1 The random number in
ROUND(x,y) Find parameters x Rounded value of , Retain y Decimal place .
4. Date function
Common date functions are as follows :
CURDATE() Return current date
CURTIME() Return current time
NOW() Returns the current date and time
YEAR(date) Get specified date A year of
MONTH(date) Get specified date Month of
DAY(date) Get specified date Date
DATE_ADD(date,INTERVALLexpr type) Return a date / Time value plus a time interval expr Time value after
DATEDIFF(date1,date2) Return to start time date1 And end time date2 Days between
5, Flow function
Flow function is also a kind of commonly used function , Can be in SQL Statement to implement conditional filtering , So as to improve the efficiency of statements .
IF(value ,t,f) If value by true, Then return to t, Otherwise return to f
IFNULL(value1 ,value2) If value1 Not empty , return value1, Otherwise return to value2
CASE WHEN [val1] THEN [res1] …ELSE [default] END If val1 by true, return res1, . Incense returns default The default value is
CASE [expr] WHEN [val1] THEN [res1] …ELSE [ default] END If expr The value is equal to the val1, return res1, … Otherwise return to default The default value is
6. summary
1. Concept : Constraints are rules that act on fields in a table , Used to limit the data stored in a table .
2. Purpose : Ensure that the data in the database is correct 、 Effectiveness and integrity .
3. classification :
Non empty constraint Limit the data of this field to null NOT NULL
Unique constraint Ensure that all data in this field is unique 、 Not repeated UNIQUE
Primary key contract A primary key is the unique identifier of a row of data , It is required to be non empty and unique PRIMARY KEY
Default constraint When saving data , If the value of this field is not specified , The default value DEFAULT
Check the invitation card (8.0.16 After the version ) Ensure that the field value meets a certain condition CHECK
Foreign key invitation Used to establish a connection between the data of two tables , Ensure data consistency and integrity FOREIGN KEY
Be careful : Constraints act on fields in a table , You can create a table / Add constraints when modifying tables
7.

8.

9. Foreign key constraints
grammar
Add foreign keys
CREATE TABLE Table name (
Field name data type
…
[CONSTRAINT] [ Name of the foreign key ] FOREIGN KEY( Foreign key field name ) REFERENCES Main table ( Make a list )
);
ALTER TABLE Table name ADD CONSTRAINT Name of the foreign key FOREIGN KEY( Foreign key field name ) REFERENCES Main table ( Make a list );
Delete foreign key
ALTER TABLE Table name DROP FOREIGN KEY Name of the foreign key ;
Delete / Update behavior
NO ACTION When deleting in the parent table / When updating the corresponding record , First, check whether the record has a corresponding foreign key , If there is, it is not allowed to delete / to update .( And RESTRICT Agreement )
RESTRICT When deleting in the parent table / When updating the corresponding record , First, check whether the record has a corresponding foreign key , If there is, it is not allowed to delete / to update .( And NO ACTION- Cause )
CASCADE When deleting in the parent table / When updating the corresponding record , First, check whether the record has a corresponding foreign key , If there is , Then delete / Update the record of the foreign key in the sub table .
SET NULL When deleting the corresponding record in the parent table , First, check whether the record has a corresponding foreign key , If yes, set the external key value in the sub table to null ( This requires that the foreign key be allowed to take nulll).
SET DEFALT When the parent table changes , Sub table will
边栏推荐
猜你喜欢

Understanding of nil in go language

Mysql delete in 不走索引的
![[spark] how to implement spark SQL field blood relationship](/img/8e/96f83844b480916b2662b0cf1fdf65.jpg)
[spark] how to implement spark SQL field blood relationship

Pagoda server setup and database remote connection

MySQL delete in without index

数据湖架构之Hudi编译篇

Number of connections server database message: error number 2003can't connect to MySQL server on 'server address' (10061)

【微服务系列】Protocol buffer动态解析

遇到女司机业余开滴滴,日入500!

C nuget offline cache package installation
随机推荐
Zotero使用之自定义参考文献格式
视图简析
Vulnerability discovery - API interface service vulnerability probe type utilization and repair
Distribution operation of D
Simple use of enum type in TS
SHOW语句用法补充
Gof23 - builder mode
Thinking and summary of technical ability
LabVIEW Arduino tcp/ip remote smart home system (project part-5)
寶塔服務器搭建及數據庫遠程連接
Phantom star VR equipment product details II: dark battlefield
Unsatisfied dependency expressed through field ‘baseMapper‘; nested exceptio
Gof23 - abstract factory pattern
zip(*arg)的用法
SecureCRT运行SparkShell 删除键出现乱码的解法
Research Report on pallet handling equipment industry - market status analysis and development prospect forecast
01 golang and matlab code of knapsack problem
Gof23 - factory mode
稀疏数组sparsearray
Live broadcast Preview - fire safety instructor training "cloud class" is about to start!