SQL 基礎整理

  • SQL語句執行順序
  1. From
  2. 錶鏈接
  3. on鏈接條件,造成新的虛擬表
  4. where 篩選條件
  5. group by 生成新的結果集合  group by 分組列表
  6. having 分組後篩選
  7. select 選出顯示的
  8. order by 排序
  9. limit        分頁/個數限制
  • 子查詢順序

  除了exists,先執行子查詢,再外查詢函數

  exists:相關子查詢,先執行外查詢,再根據子查詢字段進行過濾。spa

  • 分頁查詢

  limit (page-1)*size,sizecode

  • 字符控制函數

substr('helloworld',1,5)  helloblog

instr('helloworld','w')      6排序

LPAD(salary,10,'*')        ****salaryit

TRIM('H' from 'Helloworld') elloworldtable

  • 視圖應用場景
    • 多個地方用到一樣的查詢結果
    • 該查詢結果會用的SQL語句較複雜
  • DML 表操做 無table
insert into tableName( column1,column2,...) values(值1,...)

 

update student
set age=18
where  name='hwj'

 

/* 表數據刪除 */
delete
from student where age=18
/* 多表 */
delete from student s1,course c1
where s1.class=c1.class
and s1.name='mm';
# 方式2
truncate 語句
truncate table student

 

  •  DDL 管理操做,對錶操做要加上字段table
# 建立
create table if not exists student(column1,...)
# 修改 字段名
Alter table student change column 舊 新字段

# 修改表名
Alter table student rename to student1

# 修改字段類型和約束
Alter table tablename modify column column_name  約束

# 添加字段
Alter table 表名 Add column 字段;

# 刪除字段
Alter table 表名 drop column 字段

# 刪除表
drop table if exists 表名
相關文章
相關標籤/搜索