資料庫操作練習5

2021-08-07 10:27:20 字數 3374 閱讀 7603

題目描述

將id=5以及emp_no=10001的行資料替換成id=5以及emp_no=10005,其他資料保持不變,使用replace實現。

create table if not exists titles_test (

id int(11) not null primary key,

emp_no int(11) not null,

title varchar(50) not null,

from_date date not null,

to_date date default null);

insert into titles_test values ('1', '10001', 'senior engineer', '1986-06-26', '9999-01-01'),

('2', '10002', 'staff', '1996-08-03', '9999-01-01'),

('3', '10003', 'senior engineer', '1995-12-03', '9999-01-01'),

('4', '10004', 'senior engineer', '1995-12-03', '9999-01-01'),

('5', '10001', 'senior engineer', '1986-06-26', '9999-01-01'),

('6', '10002', 'staff', '1996-08-03', '9999-01-01'),

('7', '10003', 'senior engineer', '1995-12-03', '9999-01-01');

sql1:

不用 replace 實現,直接修改id等於5處的emp_no值即可:  update titles_test set emp_no = 10005 where id = 5

sql2:

用replace直接將emp_no為10001處的值替換為10005即可(replace(x,y,z)函式。其中x是要處理的字串,y是x中將要被替換的字串,z是用來替換y的字串,最終返回替換後的字串):update titles_test set emp_no=replace(emp_no,10001,10005)

sql3:

將id等於5處的整個一行都替換掉:replace into titles_test values (5, 10005, 'senior engineer', '1986-06-26', '9999-01-01')

題目描述

分頁查詢employees表,每5行一頁,返回第2頁的資料

create table `employees` (

`emp_no` int(11) not null,

`birth_date` date not null,

`first_name` varchar(14) not null,

`last_name` varchar(16) not null,

`gender` char(1) not null,

`hire_date` date not null,

primary key (`emp_no`));

sql1:

使用 limit 關鍵字。在 limit x,y 中,y代表返回幾條記錄,x代表從第幾條記錄開始返回(第一條記錄序號為0),因此:select * from employees limit 5,5

sql2:

利用 limit 和 offset 關鍵字。limit 後的數字代表返回幾條記錄,offset 後的數字代表從第幾條記錄開始返回(第一條記錄序號為0),也可理解為跳過多少條記錄後開始返回,因此:select * from employees limit 5 offset 5

題目描述

獲取employees中的first_name,查詢按照first_name最後兩個字母,按照公升序進行排列

create table `employees` (

`emp_no` int(11) not null,

`birth_date` date not null,

`first_name` varchar(14) not null,

`last_name` varchar(16) not null,

`gender` char(1) not null,

`hire_date` date not null,

primary key (`emp_no`));

輸出格式:

first_name

chirstian

tzvetan

bezalel

duangkaew

georgi

kyoichi

anneke

sumant

mary

parto

saniya

sql1:

本題考查 substr(x,y,z) 或 substr(x,y) 函式的使用。其中x是要擷取的字串。y是字串的起始位置(注意第乙個字元的位置為1,而不為0),取值範圍是±(1~length(x)),當y等於length(x)時,則擷取最後乙個字元;當y等於負整數-n時,則從倒數第n個字元處擷取。z是要擷取字串的長度,取值範圍是正整數,若z省略,則從y處一直擷取到字串末尾;若z大於剩下的字串長度,也是擷取到字串末尾為止。

select first_name from employees order by substr(first_name,-2)

sql2:

substr(string,start,length)

例如:select substr('abcdefg',3,4) from dual; 結果是cdef        select substr('abcdefg',-3,4) from dual; 結果efg

注意:字串中的第乙個位置始終為1。以下兩個sql查詢的結果相同:

例如:select substr('abcdefg',0,3) from dual; 結果是abc         select substr('abcdefg',1,3) from dual; 結果是abc

select first_name from employees order by substr(first_name,length(first_name)-1,2)或者

select first_name from employees order by substr(first_name,length(first_name)-1) 

資料庫表操作練習

1 建立成績表,字段包括 學生姓名,語文成績,數學成績,英語成績 向表中插入多條資料 查詢 1 查詢所有學生的數學成績和總成績 2 查詢所有學生的語文和數學成績和,按從高到低排序 3 查詢班級總成績最高的學生姓名 4 查詢班裡所有姓李學生的總成績最高的姓名 建立表 create table exam...

資料庫練習題5

1 事務的原子性是指 a 事務中包括的所有操作要麼都做,要麼都不做 b 事務一旦提交,對資料庫的改變是永久的 c 乙個事務內部的操作及使用的資料對併發的其他事務是隔離的 d 事務必須是使資料庫從乙個一致性狀態變到另乙個一致性狀態 2 事務的一致性是指 a 事務中包括的所有操作要麼都做,要麼都不做 b...

5 資料庫查詢操作

如果繼承guzzbasedao,可以使用basedao中提供的常用查詢方法進行查詢。readonlytransession為guzz對外提供的查詢操作入口,獲取方法 transactionmanager tm guzzcontext.gettransactionmanager readonlytra...