SQL 相關筆記整理

2021-10-24 19:34:07 字數 2766 閱讀 4006

sql 按語句分類:

ddl(data definition language)資料定義語句

資料定義語句主要是定義了不同的資料段,資料庫,表,列,索引等資料庫物件。常用語句關鍵字:create,drop,alter etc.

dml(data manipulation language)資料操縱語句

資料操縱語句主要是用於新增,刪除,更新,查詢資料庫記錄,並檢查資料完整性。常用語句關鍵字:insert,delete,update,select etc.

dcl(data control language)資料控制語句

資料控制語句用於控制不同資料段直接的許可和訪問級別的語句,主要是定義了資料庫,表,字段,使用者的訪問許可權和安全級別。常用語句關鍵字:grant,revoke etc.

修改操作

修改表型別

alter table tablename modify [column] column_definition [first |after column_name]

例如: 將a表的id欄位int(5)改int(11).

alter table a modify id int(11);

增加表字段

alert table tablename add [column] column_definition [first |after column_name]

例如: 在表a中新增欄位a,型別 int(3).

alert table a add a int(3);

刪除表字段

alert table tablename drop [column] column_name

例如:刪除a表的字段a.

alert table a drop a;

修改表字段

alert table tablename change [column] old_column_name column_definition [first|after column_name]

例如:將a表的字段a修改a1,同時將字段型別更為bigint(4).

alert table a change a a1 bigint(4);

注意:change和modify都可以修改表的定義,區別是change 後需要寫兩次列名。但change的優點主要是修改列名稱,modify則不能。

補充:在ddl操作中,修改語法(add/modify/change)都有乙個可選項[first|after column_name],主要是修改欄位在表中的位置。

單條單錶更新記錄

update tablename set field1=val1,field2=val2,…fieldn =valn [where condition]

例如:將a表中a 為「100」的狀態從1更為2.

update a set status=2 where a= 100

單條多表更新記錄

update t1,t2,…tn set t1.field1=expr1,…, tn.fieldn=exprn [where condition]

例如:同時更新a表和b表中的a欄位。

update a a,b b set a.a=1*2,b.a=2 where a.id = b.aid;

單條批量更新記錄

update  order1 a 

inner join (

select date_add

((w.maxcriticaltime)

,interval 24 month)as invalid_time, w.course_order_id ,w.student_id from order1 co ,

( select a.student_id ,b.start_time, a.max_critical_time,b.booking_id , b.order_id ,b.class_status from a a

left join b b on a.student_id = b.student_id and a.maxcriticaltime = b.start_time

where b.class_status in(0

,100

,200

,300

,400

)) as w

where co.order_id = w.order_id and co.valid_time =

0 and co.order_price is not null and co.order_price >

10000

)b on a.order_id = b.order_id and a.valid_time =

0 and a.student_id = b.student_id

set a.failure_time = b.invalid_time,a.update_time=

now(

),a.update_user= $

SQL相關整理

串聯多列值 concat 引數 列或字串,多個條件用逗號分開。示例 select concat 列1 列2 as concatstr from tableif else case表示式,使用方法,case when money 100 then 大於100 else 小於100 end 示例 sel...

Bibles MySQL筆記整理 sql練習

練習1.從dw complain total這個表中列出201509的投訴總量及男女分別投訴多少?練習1.從dw complain total這個表中列出201509的投訴總量及男女分別投訴多少?注意 as用法,時間函式用法,還有求的是總量 練習2.從dw complain total這個表中列出2...

u center相關學習筆記和問題整理

1.資訊 現error txbuf alloc的錯誤資訊內容。解決方案 可能是之前配置了錯誤的內容,可以恢復預設設定,在ubx cfg中選擇revert to default configuration,應該就可以恢復正常。參考 2.nmea資訊表示的內容 gprmc 推薦定位資訊 gpgga gp...