------ 【重點】====================== 內置函數 ===============================數據庫
-- 1. 字符串方法ide
--Left(): 返回字符串從左開始數,指定數量的字符串
select left('的事撒大大多大多所',4)
select left('的事撒大大多大多所',4)+'...'函數
--Right():返回字符串從右開始數,指定數量的字符串
select right ('的事撒大大多大多所',4)學習
--Ltrim():去左邊的空格
select '==='+LTRIM(' I Love You ')+'==='spa
--Rtrim():去左邊的空格
select '==='+RTRIM(' I Love You ')+'==='字符串
--同時去掉左右空格 ltrim(rtrim()) 【易錯題】
select '==='+RTRIM(ltrim(' I Love You '))+'==='get
--substring:截取字符串 第一個參數:字符串自己 , 第二個參數從哪開始截取(從1開始),包含從哪開始的字符, 第三個參數:截取幾個
select substring('八維紅葉物聯網學院專業階段',4,5) ----燁物聯網學數學
--str : 把其餘類型的信息轉爲字符串
declare @s int
set @s =123
select str(@s)+'456'string
--Lower() 字母變小寫
select lower('Hello Word')it
--Upper() 字母變大寫
select upper('Hello Word')
-- len() 求字符串長度 字母 數字 漢子 其餘符號都算1
select len('123456')
select len(ltrim(' 123456'))
--- 2. 數學函數
-- 絕對值: abs() 全部數的絕對值
select abs(-5)
--求算數平方根:sqrt()
select sqrt(2)
--四捨五入:round(數字,保留的小數位置)
select round(3.1415929,3)
--隨機數 rand() 默認隨機0-1之間的小數
select rand()
select round(rand() * 100000,0)
-- 3. 時間日期函數
-- getdate() 獲取當前系統日期
select getdate()
-- 顯示當前時間 格式是xxx年xx月xx日
select str(year(getdate()))+'年' + str(month(getdate()))+'月'+str(day(getdate()))+'日'
-- datediff() 計算時間的差值 三個參數
select datediff(day,'2020-1-1','2019-10-21')
------=-======================== 觸發器=======================-----------------
--建立一個數據庫
create database Unity16
use Unity16
--建表1--學生表
create table Students
(
Id int primary key identity, --編號
UserName varchar(10) not null, --姓名
UserAge int not null --年齡
)
--建表2--學生統計表(統計添加學生的數量)
create table Students_count
(
Counts int
)
insert into Students_count values(0)
select * from Students
select * from Students_count
----------------- 插入觸發器 -------------------
--create trigger 觸發器名
--on 表名
--after 觸發器種類
--as
--begin
-- 觸發器代碼
--end
--第1步 建立觸發器
create trigger xx
on Students
after insert --觸發器種類 inert 觸發器時機 after 以後觸發
as
begin
update Students_count set Counts=Counts+1
end
--第2步 使用觸發器
insert into Students values('張三',19)
insert into Students values('老王',39)
--刪除觸發器
drop trigger xx
----------------- 刪除觸發器【以後】 ------------------
--建立delete觸發器,實現:刪除學生的時候,同時減小學生人數
create trigger del_stu
on Students
after delete
as
begin
update Students_count set Counts=Counts-1
end
--使用
delete from Students where UserName='老王'
----------------- 修改觸發器 【以後】 -----------------------------
create trigger upd_stu
on Students
after update
as
begin
print'修改前的數據'
select * from deleted
print'添加前的數據'
select * from inserted
end
update Students set UserName='小雨',UserAge=25 where id=5
做者還在學習中,發現錯誤的請在評論區留言。 若是有客友以爲文章還行的話,請點波推薦哦👍。 謝謝大家!!