女人被狂躁到高潮视频免费无遮挡,内射人妻骚骚骚,免费人成小说在线观看网站,九九影院午夜理论片少妇,免费av永久免费网址

當(dāng)前位置:首頁 > > 充電吧
[導(dǎo)讀]學(xué)習(xí)sql有一段時(shí)間了,發(fā)現(xiàn)在我建了一個(gè)用來測試的表(沒有建索引)中出現(xiàn)了許多的重復(fù)記錄。后來總結(jié)了一些刪除重復(fù)記錄的方法,在Oracle中,可以通過唯一rowid實(shí)現(xiàn)刪除重復(fù)記錄;還可以建臨時(shí)表來實(shí)

學(xué)習(xí)sql有一段時(shí)間了,發(fā)現(xiàn)在我建了一個(gè)用來測試的表(沒有建索引)中出現(xiàn)了許多的重復(fù)記錄。后來總結(jié)了一些刪除重復(fù)記錄的方法,在Oracle中,可以通過唯一rowid實(shí)現(xiàn)刪除重復(fù)記錄;還可以建臨時(shí)表來實(shí)現(xiàn)...這個(gè)只提到其中的幾種簡單實(shí)用的方法,希望可以和大家分享(以表employee為例)。

SQL> desc employee

?Name????????????????????????????????????? Null???? Type
?----------------------------------------- -------- ------------------

emp_id??????????????????????????????????????????????? NUMBER(10)
emp_name?????????????????????????????????????????? VARCHAR2(20)

salary???????????????????????????????????????????????? ?NUMBER(10,2)

?

?

可以通過下面的語句查詢重復(fù)的記錄:

SQL> select * from employee;

?

??? EMP_ID EMP_NAME????????? ????????????????????????SALARY

---------- ---------------------------------------- ----------

???????? 1 sunshine????????????????????????????????????? 10000

???????? 1 sunshine????????????????????????????????????? 10000

???????? 2 semon??????????????????? ?????????????????????20000

???????? 2 semon???????????????????????????????????????? 20000

???????? 3 xyz?????????????????????????????????????????? 30000

???????? 2 semon???????????????????????????????????????? 20000

?


SQL> select distinct * from employee;

??? EMP_ID EMP_NAME???????????????????????????????????? SALARY

---------- ---------------------------------------- ----------

???????? 1 sunshine????????????????????????????????????? 10000

???????? 2 semon???????????????????????????????????????? 20000

??? ?????3 xyz???????????????????????????????????????????? 30000

SQL>? select * from employee group by emp_id,emp_name,salary having count (*)>1

??? EMP_ID EMP_NAME???????????????????????????????????? SALARY

---------- ---------------------------------------- ----------

???????? 1 sunshine????????????????????????????????????? 10000

???????? 2 semon????????????????????????????????????????? 20000


SQL> select * from employee e1

where rowid in (select max(rowid) from employe e2
?where e1.emp_id=e2.emp_id and

? e1.emp_name=e2.emp_name and e1.salary=e2.salary);

?

??? EMP_ID EMP_NAME???????????????????????????????????? SALARY

---------- ---------------------------------------- ----------

???????? 1 sunshine????????????????????????????????????? 10000

???????? 3 xyz????? ?????????????????????????????????????? 30000

???????? 2 semon???????????????????????????????????????? 20000

?

?

2. 刪除的幾種方法:

?

(1)通過建立臨時(shí)表來實(shí)現(xiàn)

SQL>create table temp_emp as (select distinct * from employee)?

SQL> truncate table employee; (清空employee表的數(shù)據(jù))

SQL> insert into employee select * from temp_emp; ?(再將臨時(shí)表里的內(nèi)容插回來)

?

( 2)通過唯一rowid實(shí)現(xiàn)刪除重復(fù)記錄.在Oracle中,每一條記錄都有一個(gè)rowid,rowid在整個(gè)數(shù)據(jù)庫中是唯一的,rowid確定了每條記錄是在Oracle中的哪一個(gè)數(shù)據(jù)文件、塊、行上。在重復(fù)的記錄中,可能所有列的內(nèi)容都相同,但rowid不會相同,所以只要確定出重復(fù)記錄中那些具有最大或最小rowid的就可以了,其余全部刪除。

SQL>delete from employee e2 where rowid not in (
??????? select max(e1.rowid) from employee e1 where

??????? e1.emp_id=e2.emp_id and e1.emp_name=e2.emp_name and e1.salary=e2.salary);--這里用min(rowid)也可以。

?

SQL>delete from employee e2 where rowid <(
??????? select max(e1.rowid) from employee e1 where
??????? e1.emp_id=e2.emp_id and e1.emp_name=e2.emp_name and

????????????????? e1.salary=e2.salary);

?

(3)也是通過rowid,但效率更高。

SQL>delete from employee where rowid not in (
??????? select max(t1.rowid) from employee t1 group by

???????? t1.emp_id,t1.emp_name,t1.salary);--這里用min(rowid)也可以。

?

?

??? EMP_ID EMP_NAME???????????????????????????????????? SALARY

---------- ---------------------------------------- ----------

???????? 1 sunshine????????????????????????????????????? 10000

???????? 3 xyz???? ??????????????????????????????????????? 30000

???????? 2 semon???????????????????????????????????????? 20000

?

?

?

?

SQL> desc employee

?Name????????????????????????????????????? Null???? Type
?----------------------------------------- -------- ------------------

emp_id??????????????????????????????????????????????? NUMBER(10)
emp_name?????????????????????????????????????????? VARCHAR2(20)

salary???????????????????????????????????????????????? ?NUMBER(10,2)

?

?

可以通過下面的語句查詢重復(fù)的記錄:

SQL> select * from employee;

?

??? EMP_ID EMP_NAME????????? ????????????????????????SALARY

---------- ---------------------------------------- ----------

???????? 1 sunshine????????????????????????????????????? 10000

???????? 1 sunshine????????????????????????????????????? 10000

???????? 2 semon??????????????????? ?????????????????????20000

???????? 2 semon???????????????????????????????????????? 20000

???????? 3 xyz?????????????????????????????????????????? 30000

???????? 2 semon???????????????????????????????????????? 20000

?


SQL> select distinct * from employee;

??? EMP_ID EMP_NAME???????????????????????????????????? SALARY

---------- ---------------------------------------- ----------

???????? 1 sunshine????????????????????????????????????? 10000

???????? 2 semon???????????????????????????????????????? 20000

??? ?????3 xyz???????????????????????????????????????????? 30000

SQL>? select * from employee group by emp_id,emp_name,salary having count (*)>1

??? EMP_ID EMP_NAME???????????????????????????????????? SALARY

---------- ---------------------------------------- ----------

???????? 1 sunshine????????????????????????????????????? 10000

???????? 2 semon????????????????????????????????????????? 20000


SQL> select * from employee e1

where rowid in (select max(rowid) from employe e2
?where e1.emp_id=e2.emp_id and

? e1.emp_name=e2.emp_name and e1.salary=e2.salary);

?

??? EMP_ID EMP_NAME???????????????????????????????????? SALARY

---------- ---------------------------------------- ----------

???????? 1 sunshine????????????????????????????????????? 10000

???????? 3 xyz????? ?????????????????????????????????????? 30000

???????? 2 semon???????????????????????????????????????? 20000

?

?

2. 刪除的幾種方法:

?

(1)通過建立臨時(shí)表來實(shí)現(xiàn)

SQL>create table temp_emp as (select distinct * from employee)?

SQL> truncate table employee; (清空employee表的數(shù)據(jù))

SQL> insert into employee select * from temp_emp; ?(再將臨時(shí)表里的內(nèi)容插回來)

?

( 2)通過唯一rowid實(shí)現(xiàn)刪除重復(fù)記錄.在Oracle中,每一條記錄都有一個(gè)rowid,rowid在整個(gè)數(shù)據(jù)庫中是唯一的,rowid確定了每條記錄是在Oracle中的哪一個(gè)數(shù)據(jù)文件、塊、行上。在重復(fù)的記錄中,可能所有列的內(nèi)容都相同,但rowid不會相同,所以只要確定出重復(fù)記錄中那些具有最大或最小rowid的就可以了,其余全部刪除。

SQL>delete from employee e2 where rowid not in (
??????? select max(e1.rowid) from employee e1 where

??????? e1.emp_id=e2.emp_id and e1.emp_name=e2.emp_name and e1.salary=e2.salary);--這里用min(rowid)也可以。

?

SQL>delete from employee e2 where rowid <(
??????? select max(e1.rowid) from employee e1 where
??????? e1.emp_id=e2.emp_id and e1.emp_name=e2.emp_name and

????????????????? e1.salary=e2.salary);

?

(3)也是通過rowid,但效率更高。

SQL>delete from employee where rowid not in (
??????? select max(t1.rowid) from employee t1 group by

???????? t1.emp_id,t1.emp_name,t1.salary);--這里用min(rowid)也可以。

?

?

??? EMP_ID EMP_NAME???????????????????????????????????? SALARY

---------- ---------------------------------------- ----------

???????? 1 sunshine????????????????????????????????????? 10000

???????? 3 xyz???? ??????????????????????????????????????? 30000

???????? 2 semon???????????????????????????????????????? 20000

?

?

?

?

?

?

?

?

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

前言sql優(yōu)化是一個(gè)大家都比較關(guān)注的熱門話題,無論你在面試,還是工作中,都很有可能會遇到。如果某天你負(fù)責(zé)的某個(gè)線上接口,出現(xiàn)了性能問題,需要做優(yōu)化。那么你首先想到的很有可能是優(yōu)化sql語句,因?yàn)樗母脑斐杀鞠鄬τ诖a來說...

關(guān)鍵字: sql

無論是開發(fā)、測試,還是DBA,都難免會涉及到數(shù)據(jù)庫的操作,比如:創(chuàng)建某張表,添加某個(gè)字段、添加數(shù)據(jù)、更新數(shù)據(jù)、刪除數(shù)據(jù)、查詢數(shù)據(jù)等等。

關(guān)鍵字: 數(shù)據(jù)庫 sql

前言 上一篇總結(jié)了Mysql的鎖機(jī)制,通過讀者的反映和閱讀量顯示,總體還是不錯(cuò)的,感興趣的可以閱讀一下[大廠面試官必問的Mysql鎖機(jī)制]。 寫了那么多的Mysql文章,有讀者問我是不是dba,工作真的需要掌握那么深嗎。...

關(guān)鍵字: sql

前言 前幾天有粉絲和我聊到他找工作面試大廠時(shí)被問的問題,因?yàn)楝F(xiàn)在疫情期間,找工作也特別難找。他說面試的題目也比較難,都偏向于一兩年的工作經(jīng)驗(yàn)的面試題。 他說在一面的時(shí)候被問到Mysql的面試題,索引那塊自己都回答比較滿意...

關(guān)鍵字: sql

? ? ? ? ? ? ? ? ? ? ? ? 我所寫的項(xiàng)目是使用Maven開發(fā),在pom.xml中添加如下必要依賴: ? ? ? ? 添加com.microsoft.sqlserver的mssql-

關(guān)鍵字: server sql 存儲過程

題目:把n個(gè)骰子扔在地上,所有骰子朝上一面的點(diǎn)數(shù)之和為S。輸入n,打印出S的所有可能的值出現(xiàn)的概率。分析:骰子一共6個(gè)面,每個(gè)面上都有一個(gè)點(diǎn)數(shù),對應(yīng)的數(shù)字是1到 6之間的一個(gè)數(shù)字。所以,n個(gè)骰子的點(diǎn)數(shù)

關(guān)鍵字: delete float

DECLARE @dt datetimeSET @dt=GETDATE()DECLARE @number intSET @number=3--1.指定日期該年的第一天或最后一天--A. 年的第一天SE

關(guān)鍵字: qq sql

/**************************************************************?? SQL?Server?2012?新增的函數(shù)?? **********

關(guān)鍵字: server sql

題目:我們把只包含因子2、3和5的數(shù)稱作丑數(shù)(Ugly Number)。例如6、8都是丑數(shù),但14不是,因?yàn)樗蜃?。習(xí)慣上我們把1當(dāng)做是第一個(gè)丑數(shù)。求按從小到大的順序的第1500個(gè)丑數(shù)。 分析:

關(guān)鍵字: delete 算法

SQL即結(jié)構(gòu)化查詢語言(Structured Query Language),是一種特殊目的的編程語言,是一種數(shù)據(jù)庫查詢和程序設(shè)計(jì)語言,用于存取數(shù)據(jù)以及查詢、更新和管理關(guān)系數(shù)據(jù)庫系統(tǒng);同時(shí)也是數(shù)據(jù)庫腳本文件的擴(kuò)展名。

關(guān)鍵字: sql 基礎(chǔ)教程 數(shù)據(jù)庫 語法
關(guān)閉