sql語句(已在Oracle中測試,之后有添加內容放在評論中)


1增

1.1【創建一張表】

create table 表名(列名 類型);

例:

create table stu(姓名 varchar2(25),性別 varchar2(25),出生日期 date);
create table newstu(name varchar2(25),sex varchar2(25),出生日期 date);

 

1.2【插入單行】
insert [into] <表名> (列名) values (列值)
例:

insert into stu (姓名,性別,出生日期) values ('張三','',to_date('1993-1-1','yyyy-mm-dd'));


1.3【將現有表數據添加到一個已有表】
insert into <已有的新表> (列名) select <原表列名> from <原表名>

列名可以不同,但是對應列的類型必須相同。
例:

insert into newstu(name,sex,出生日期) select 姓名,性別,出生日期 from stu; 
--insert into newstu(name,sex,出生日期) select *from stu; --也是可以的

 

1.4【直接拿現有表數據創建一個新表並填充】
create table <新表> as select *from <現有表>
例:

create table nnstu as select *from newstu;

 

1.5【使用union關鍵字合並數據進行插入多行】
insert <表名> (列名) select <列值> union select <列值>
例:

在newstu表中多插入幾條數據:

insert into newstu(name,sex,出生日期) values('李四','',to_date('1995-12-25','yyyy-mm-dd'));
insert into newstu(name,sex,出生日期) values('王五','',to_date('1996-3-8','yyyy-mm-dd'));

向stu表中插入多行數據:

insert into stu(姓名,性別,出生日期) 
select '李四','',to_date('1995-12-25','yyyy-mm-dd') from newstu union 
select '王五','',to_date('1996-3-8','yyyy-mm-dd') from newstu;

 

1.6【添加約束】

 例:主鍵(id):alter table <表名> add  constraint con_<表名>_id_pk primary key(id) ;

--添加主鍵
alter table tbl_account add constraint con_tbl_account_id_pk primary key(id);
--添加非空
alter table tbl_account add constraint con_tbl_account_uname_ck check(username is not null);
--添加范圍>
alter table tbl_account add constraint con_tbl_account_bal_ck check(balance>=0);
--添加范圍in
alter table tbl_account add constraint con_tbl_account_gender_ck check(gender in ('',''));
--添加范圍and
alter table tbl_account add constraint con_tbl_account_age_ck check(age >= 0 and age < 170);
--添加唯一
alter table tbl_account add constraint con_tbl_account_age_uk unique key(age);
--添加外鍵
alter table tbl_account add constraint con_tbl_account_age_fk foreign key(age) references 關聯的表(關聯的字段)
--添加索引
alter table cp_record add index index_cp_record_mem_id ( `mem_id` );

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2刪

2.1【刪除<滿足條件的>行】
delete from <表名> [where <刪除條件>]
例:

delete from stu where 姓名='張三';


2.2【刪除所有行】
truncate table <表名>
例:

truncate table stu;

注意:刪除表的所有行,但表的結構、列、約束、索引等不會被刪除;不能用語有外建約束引用的表

 

2.3【刪除整張表】

drop table <表名>

例:

drop table stu;


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

3改

update <表名> set <列名=更新值> [where <更新條件>]
例:

update newstu set name='胡八',sex='' where name='張三'; 


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

4查

4.1條件查詢

4.1.1【按條件查詢,按順序展示】
select <列名> from <表名> [where <查詢條件表達試>] [order by <排序的列名>[asc或desc]]

例:

原字段為:name,sex,出生日期,更改列名as可加可不加

--性別為女且按出生日期降序排序
select name 姓名,sex 性別,出生日期 from newstu where sex='' order by 出生日期 desc

說明:升序為asc

4.1.2【查詢空行】
例:

select name 姓名,sex 性別,出生日期 from newstu where 出生日期 is null;

說明:查詢表newstu中出生日期為空的所有行,並顯示所有列;SQL語句中用is null或者is not null來判斷是否為空行

4.1.3【在查詢中使用常量】
例:

select name 姓名,sex 性別,出生日期,'浙江' as 地址 from newstu;

說明:查詢表newstu,顯示name列,並添加地址列,其列值都為'浙江'

4.1.4【查詢返回限制行數(關鍵字:top percent)】
例:

--oracle中,前兩條數據
select name 姓名,sex 性別,出生日期 from newstu where rownum<=2;
--oracle中,前60%數據
select name 姓名,sex 性別,出生日期 from newstu where rownum<=(select count(*)*0.6 from newstu);
--mysql中,前兩條數據
select top 2 name 姓名,sex 性別,出生日期 from newstu;
select *from newstu limit 2
--mysql中,前60%數據 select top 60 percent name from newstu;


4.2模糊查詢
4.2.1【使用like進行模糊查詢】
注意:like運算副只用於字符串,所以僅與char和varchar數據類型聯合使用
例:

select *from newstu where name like '李%';
select *from newstu where name='李四' and age=22;
select *from newstu where age=20 or age=21 or age=22;

說明:查詢顯示表newstu中,name字段第一個字為趙的記錄

4.2.2【使用between在某個范圍內進行查詢】
例:

select *from newstu where age between 20 and 22;

說明:查詢顯示表newstu中age在20到22之間的記錄

4.2.3【使用in在列舉值內進行查詢】
例:

select *from newstu where name in('李四','胡八');

說明:查詢表newstu中name值為李四或者胡八的記錄


4.3.分組查詢
4.3.1【使用group by進行分組查詢】
例:

select sex,avg(age) as 平均年齡 from newstu group by sex;

說明:在newstu表中查詢,展示按sex分組的sex和平均age字段,在這個條件下不能展示其他字段

4.3.2【使用having子句進行分組篩選】
例:

select sex,avg(age) as 平均年齡 from newstu group by sex having count(age)>2;

說明:接上面例子,顯示分組后count(age)>2的行,由於where只能在沒有分組時使用,分組后只能使用having來限制條件


4.4.多表聯接查詢

4.4.1內聯接

4.4.1.1【在where子句中指定聯接條件】
例:

select s.name,a.are from newstu s,neware a where s.name=a.name;

4.4.1.2【在from子句中使用join…on】
例:

select s.name,a.are from newstu s inner join neware a on(s.name=a.name);

內聯與外聯的區別:內聯是兩個表在連接的條件下,所有的字段相同才顯示。

4.4.2外聯接

4.4.2.1【左外聯接查詢】
例:

select s.name,a.are from newstu s left join neware a
on s.name=a.name where a.are='江蘇';

說明:查詢出江蘇地區的搜有人的姓名、所在地,newstu表的字段為name,sex,出生日期,age,neware表的字段為name,are。

4.4.2.2【右外聯接查詢】
例:

select s.name,a.are from newstu s right join neware a
on s.name=a.name where a.are='江蘇';

說明:查詢出江蘇地區的搜有人的姓名、所在地,newstu表的字段為name,sex,出生日期,age,neware表的字段為name,are。

左關聯與有關聯的區別:左聯顯示左表所有內容,右表有就顯示,沒有就顯示null;右聯顯示右表所有內容,左表有就顯示,沒有就顯示null。

neware表中name字段的值如果在newstu表中的name字段是沒有的,那么左關聯時左邊中沒有的,但是右關聯可以查出左表中沒有的。

以4.2.1和4.2.2為例:

newstu表:

name sex 出生日期 age

胡八 女 1993-01-01 00:00:00 24
李四 女 1995-12-25 00:00:00 22
王五 男 1996-03-08 00:00:00 21
趙六 男             20
陳七 男               32

neware表:

name are

胡八 江蘇
胡八 蘇州
李四 宿遷
李四 江蘇
張三 江蘇

左關聯:select s.name,a.are from newstu s left join neware a on s.name=a.name where a.are='江蘇';

  結果:name   are

      胡八     江蘇

     李四      江蘇

右關聯:select s.name,a.are from newstu s right join neware a on s.name=a.name where a.are='江蘇';

  結果:name   are

     胡八     江蘇

      李四      江蘇

     null     江蘇

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM