常見SQL語句(僅供自己複習)

2021-10-07 11:27:00 字數 3444 閱讀 8761

ddl資料定義語言(表,庫,檢視…)

create    drop   alter    show

增        刪      改     查

dml 資料操作語言

insert    delete   update   select

dcl 資料控制語句(許可權)

grant   revoke

service mysqld start;  啟動 資料庫的服務

create 庫結構 表 檢視 觸發器  儲存過程

create

create  database  [if not exists] 0623;  新增乙個資料庫【不存在就建立】

drop  

drop  database [if exists]  0623; 刪除乙個資料庫

useuse  0623;使用資料庫

show

show      databases; 檢視所有的庫

show create database 0623; 檢視乙個庫的建立資訊

create table table_name(名字,型別,約束 [注釋])

create table stu( sid varchar(10),name varchar(20),*** enum             (「man」,「woman」));      建立乙個表                               括號(名字,型別,字段約束) 字段約束(主鍵(primary key) ,外來鍵(foreign key), 唯一鍵                                               (unique),非空(not null),預設(default ))

desc  stu  檢視表的字段資訊

show create tables stu;檢視建立時候表的字段資訊

show tables;   檢視所有的表

drop table stu; 刪除乙個表

alter

1.欄位型別   modify

alter table stu modify sid vachar(20);  alter 表名 modify 欄位名稱 型別

2.欄位名稱  change

alter table stu change sid  id vachar(20); 也可以修改字段型別

3.新增乙個字段  add  (after,first)

alter table stu add score float default 0;  add +名稱+型別(放在最後乙個)

alter table stu add score float default 0 after id;  放在after id後面  first  在…的前面

4.刪除字段  drop

alter table stu drop score;  刪除型別

5.修改表名  rename

alter table stu rename stua;

insert   delete   update   select

insert into stu(表名)  values(內容)(「001」,「張三」,「man」,20); 每次資料寫在()裡面用逗號隔開。  大批量  load

insert into stu(id,name,***) values(「002」,」lisi」,」woman」); 當我們輸入的資訊少於字段資訊需要制定輸入的是某些欄位信

delete from stu (where id = 「006」);  刪除表的資料(限定條件)

update stu set age = 19 (where id = 「002」); 修改資料(指定條件 )

1.普通查詢

select * from stu;//  通匹查詢

select id , name,sec,age from stu; 查詢某些欄位的資訊

select  * from stu where id = 「001」 ; 帶著過濾條件

2.去重查詢  distinct

select distinct age from  table_name;

3.排序查詢  order by   公升序 asc  降序 desc 

select distinct age from stu order by age; 放在最後  不寫的話 預設公升序

4. 分組查詢  group by   (count 統計條數,year 年份)

select id,sum(score) from result group by id; 根據 id 分組  sum 總分

5.  多表查詢

等值查詢

select stu.id,score from stu,result where stu.id = result.id and age < 20 and score < 60;(指明id 是哪的)

1.外連線查詢

1.左外連線查詢

select a.id,score

from      (select id,age from stu where age < 20) a      left join     (select id, score from result where score                                        < 60) b             on a.id = b.id          where b.score is not null;

2.右外連線查詢

select a.id,score      from       (select id,age from stu where age < 20) a    right join

(select id,score from result where score < 60) b     on a.id = b.id      where a.id is not null;

3.全外連線查詢

select a.id,score      from   (select id,age from stu where age < 20) a      full join

(select id,score from result where score < 60) b    on a.id = b.id       where a.id is not null;

2.內連線查詢

1.內連線查詢   只篩選匹配項

select a.id,score       from      (select id,age from stu where age < 20) a    inner join

(select id,score from result where score < 60) b      on a.id = b.id;

6.聚合查詢 union(去重) | union all (不去重)

select * from stu    union        select * from teach;

sql語句複習(一)

在oracle中預設大寫,如果不加雙引號的話,emp會被轉換成大寫,進行查詢 select from emp numberic 5,2 5位有效數字,2為小數部分 oracle中的不等於號 escape escape character 關鍵字,允許在字串中搜尋萬用字元,而不將其作為萬用字元 esc...

SQL 語句之Join複習

left join 左聯接 返回包括左表中的所有記錄和右表中聯結字段相等的記錄 right join 右聯接 返回包括右表中的所有記錄和左表中聯結字段相等的記錄 inner join 等值連線 只返回兩個表中聯結字段相等的行 a表 id姓名1張三 2李四3王五 b表 id成績190 270480 s...

常見sql語句操作

1 基本語法 create table testfmj id int identity 1,1 identity表示自增列的意思,而int identity 1,1 表示從1開始遞增,每次自增1。name varchar 30 default abc varchar 30 age int defau...