当前位置:网站首页>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
边栏推荐
- Typescript type
- Container with the most water
- Load balancer does not have available server for client: userservice problem solving
- 连接数服务器数据库报:错误号码2003Can‘t connect to MySQL server on ‘服务器地址‘(10061)
- Go language learning notes 1.1
- Play with a variety of application scenarios and share secrets with Kwai MMU
- Decompile Android applications, interview Android
- Self attention and multi head self attention (MSA) in transformer
- DPDK——TCP/UDP协议栈服务端实现(一)
- JS download pictures
猜你喜欢

Interviewer: what is the difference between a test plan and a test plan?

Kotlin compose state recovery remembersaveable and remember

数据湖架构之Hudi编译篇

直播预告丨消防安全讲师培训“云课堂”即将开讲!

同步通信和异步通信的区别以及优缺点

浅析一道经典题

I caught a 10-year-old Alibaba test developer in the company. After chatting with him, I realized everything

MySQL基础用法01

Mysql delete in 不走索引的

遇到女司机业余开滴滴,日入500!
随机推荐
Decision tree learning notes
The four cores of the browser: Trident, gecko, WebKit, blink
Pychart cannot run designer Exe (this application failed to start because no Qt platform plugin could be I appears)
zip(*arg)的用法
vs code 使用 prettier 格式化 js 的时候, 函数定义的名称和括号之间有一个空格, 而 eslit 又不允许这个空格.
Thinking skills of technical leaders
Web components series (10) -- realize the basic layout of mycard
Zotero使用之自定义参考文献格式
个人博客系统需求分析
If you meet a female driver who drives didi as an amateur, you can earn 500 yuan a day!
SQL中空值的判断
Gof23 - prototype mode
Everything is a vector. The service practice of iqiyi online vector recall project
Research Report on market supply and demand and strategy of Chinese amyl cinnamaldehyde (ACA) industry
宝塔服务器搭建及数据库远程连接
Gof23 - factory mode
Experience the new features of Milvus 2.0 together
China micronutrient market trend report, technical innovation and market forecast
Dpdk - tcp/udp protocol stack server implementation (I)
稀疏数组sparsearray