当前位置:网站首页>MySQL - function and constraint commands
MySQL - function and constraint commands
2022-07-26 18:59:00 【NO.-LL】
Catalog
function
String function

# A. concat : String splicing
select concat('Hello' , ' MySQL');
# B. lower : All lowercase
select lower('Hello');
# C. upper : Capitalize all
select upper('Hello');
# D. lpad : padding-left
select lpad('01', 5, '-');
# E. rpad : Right fill
select rpad('01', 5, '-');
# F. trim : Remove the space
select trim(' Hello MySQL ');
# G. substring : Intercept substring
select substring('Hello MySQL',1,5);update emp set workno = lpad(workno, 5, '0');Numerical function

# A. ceil: Rounding up
select ceil(1.1);
# B. floor: Rounding down
select floor(1.9);
# C. mod: modulus
select mod(7,4);
# D. rand: Get random numbers
select rand();
# E. round: rounding
select round(2.344,2);select lpad(round(rand()*1000000 , 0), 6, '0');Date function

# A. curdate: The current date
select curdate();
# B. curtime: current time
select curtime();
# C. now: Current date and time
select now();
# D. YEAR , MONTH , DAY: The current year 、 month 、 Japan
select YEAR(now());
select MONTH(now());
select DAY(now());
# E. date_add: Increase the specified time interval
select date_add(now(), INTERVAL 70 YEAR );
# F. datediff: Get the number of days between the two dates
select datediff('2021-10-01', '2021-12-01');
select name, datediff(curdate(), entrydate) as 'entrydays' from emp order by entrydays
desc;Flow function

# A. if
select if(false, 'Ok', 'Error');
# B. ifnull
select ifnull('Ok','Default');
select ifnull('','Default');
select ifnull(null,'Default');
# C. case when then else end
select
name,
( case workaddress when ' Beijing ' or ' Shanghai ' then ' first-tier cities 'else
' Second-tier cities ' end ) as ' Work address '
from emp;
constraint
Primary key constraint

Provide constraints when creating tables
CREATE TABLE tb_user(
id int AUTO_INCREMENT PRIMARY KEY COMMENT 'ID Unique identification ',
name varchar(10) NOT NULL UNIQUE COMMENT ' full name ' ,
age int check (age > 0 && age <= 120) COMMENT ' Age ' ,
status char(1) default '1' COMMENT ' state ',
gender char(1) COMMENT ' Gender '
);
Foreign key constraints
Create two tables


create table dept(
id int auto_increment comment 'ID' primary key,
name varchar(50) not null comment ' Department name '
)comment ' Departmental table ';
INSERT INTO dept (id, name) VALUES (1, ' R & D department '), (2, ' The Marketing Department '),(3, ' Finance Department '), (4,
' The sales department '), (5, ' General manager office ');
create table empp(
id int auto_increment comment 'ID' primary key,
name varchar(50) not null comment ' full name ',
age int comment ' Age ',
job varchar(20) comment ' Position ',
salary int comment ' Salary ',
entrydate date comment ' Entry time ',
managerid int comment ' Direct leadership ID',
dept_id int comment ' department ID'
)comment ' The employee table ';
INSERT INTO empp (id, name, age, job,salary, entrydate, managerid, dept_id) VALUES (1, ' Jin yong ', 66, ' President ',20000, '2000-01-01', null,5),(2, ' zhang wuji ', 20, ' project manager ',12500, '2005-12-05', 1,1), (3, ' Yang Xiao ', 33, ' Development ', 8400,'2000-11-03', 2,1),(4, ' Xiangr ', 48, ' open Hair ',11000, '2002-02-05', 2,1), (5, ' chang yuchun ', 43, ' Development ',10500, '2004-09-07', 3,1),(6, ' Small zhao ', 19, ' cheng The foreman encouraged the teacher ',6600, '2004-10-12', 2,1);
1). 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 ( Name of main table ));ALTER TABLE Table name ADD CONSTRAINT Name of the foreign key FOREIGN KEY ( Foreign key field name )REFERENCES Main table ( Name of main table ) ;
alter table empp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id);2). Delete foreign key
ALTER TABLE Table name DROP FOREIGN KEY Name of the foreign key ;
alter table emp drop foreign key fk_emp_dept_id;Delete / Update behavior

grammar
ALTER TABLE Table name ADD CONSTRAINT Name of the foreign key FOREIGN KEY ( Foreign key field ) REFERENCESMain table name ( Main table field name ) ON UPDATE CASCADE ON DELETE CASCADE;
1.CASCADE
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id) on
update cascade on delete cascade ;The phenomenon is that the primary and secondary tables are synchronized
2.SET NULL
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id) on
update set null on delete set null ;We delete id by 1 The data of , It is found that the records of the parent table can be deleted normally , After deleting the data of the parent table , Then open the sub table empp, We found that the sub table empp Of dept_id Field , original dept_id by 1 The data of , Now it's all set to NULL 了 .
over
边栏推荐
- NFT数字藏品系统开发:“中国旅游日”山西首次发布古建筑数字藏品
- Interface test scheme (interface test idea)
- Tensor Rt的int8量化原理
- Utility website recommendations
- Seata 入门简介
- How to design test cases well
- The pit of mpc5744p reports an error, RTOS cannot be started, and there is a clock source problem
- rancher部署kubernetes集群
- Use notes of Lichuang EDA
- Sentinel 隔离与降级
猜你喜欢

SMMU carding

CoVOS:无需解码!利用压缩视频比特流的运动矢量和残差进行半监督的VOS加速(CVPR 2022)...

OpenSSF 基金会总经理 Brian Behlendorf :预计 2026 年将有 4.2 亿个开源

Seata 入门简介

The class jointly built by famous oarsmen is new, and Professor qiuxipeng of Fudan University broadcast it live on Tuesday!

2022年制冷与空调设备运行操作考试模拟100题及模拟考试

Module 8 job message data MySQL table design

SSM integration - functional module and interface testing

Arrangement of information security emergency plan

模板进阶(跑路人笔记)
随机推荐
[yuntu said] issue 246 digital asset chain - your God of digital asset property protection!
2022上海市安全员C证操作证考试题库模拟考试平台操作
MES系统的选择需重点考虑哪些方面?
Offer set (1)
[soft exam] soft exam tutorial + real questions over the years
工赋开发者社区 | 定了!就在7月30日!
2022年化工自动化控制仪表考题模拟考试平台操作
How to become an excellent test / development programmer? Focus on planning and then move
NFT digital collection development: digital collections help enterprise development
Microsoft silently donated $10000 to curl, which was not notified until half a year later
ALV screen input option learning
立创EDA使用笔记
NFT digital collection system development: fellow uncle first promoted the blessing series digital collection, which will be sold out immediately
Comparison of advantages and disadvantages between SD NAND and EMMC
微软默默给 curl 捐赠一万美元,半年后才通知
2022T电梯修理考试题及在线模拟考试
rancher部署kubernetes集群
测试组如何进行QA规范
MySQL - 函数及约束命令
模块八作业 - 消息数据 MySQL 表设计