細數MySQL中SQL語句的分類

2022-09-25 23:48:10 字數 3904 閱讀 2382

1:資料定義語言(ddl)

用於建立、修改、和刪除資料庫內的資料結構,如:1:建立和刪除資料庫(create database || drop  database);2:建立、修改、重新命名、刪除表(create  table || alter table|| rename table||drop  table);3:建立和刪除索引(createindex  || drop index)

2:資料查詢語言(dql)

從資料庫中的乙個或多個表中查詢資料(select)

3:資料操作語言(dml)

修改資料庫中的資料,包括插入(insert)、更新(update)和刪除(delete)

4:資料控制語言(dcl)

用於對資料庫的訪問,如:1:給使用者授予訪問許可權(grant);2:取消使用者訪問許可權(remoke)

問:什麼時候使用drop?什麼時候使用delete?

答:由上可看出,對於結構刪除,如資料庫刪除、表刪除、索引刪除等當使用drop;而對於資料的刪除則是delete.

管理資料庫和表

1:建立資料庫-----create  database  資料庫名

例如:createdatabase instant

建立表-----create table 表名(列名  列資料型別  列約束條件》);

例如:create table student(

id  int  primart key;

name varchar(50));

注意:如建立本地臨時表(僅僅在建立臨時表的連線中可見),在表名前加#;如建立全域性臨時表(對所有連線可見),在表名前加##;

建立索引-----create index 索引名 on 表名 (列名。。。);

例如:(非唯一索引)create index nameindex on friend (nmae);

(唯一索引)create  unique index nameindex on friend (name );

2:連線資料庫---use 資料庫名

例如:useinstant;

3:刪除資料庫-----drop database 資料庫名

例如:dropdatabase instant;

刪除表-----drop table 表名

例如:drop table student;

刪除索引---drop indexfriend.phonenoindex;程式設計客棧(需指定表名和索引名)

4:複製表-----select * from my_friends from friends;(此複製並不能複製表的約定)

複製表結構不複製資料:select * from my_friendsfrom friends where 1=0;

5:修改表-----

1:新增新列。。altertable friends add address varchar(50);

2:更改定義...altertable friends modify phone default('筆者就哦');

3:刪除列。。altertable friends drop cloumn phoneno;

保證資料完整性

一:分類:

1:實體完整性;

2:域完整性;

3:應用完整性;

4:使用者自定義完整性;

二:實現:

1:建立非空約束------not null

2:設定主鍵約束------primary key

3:設定唯一約束-------unique

4:指定預設約束-------default

5:設定檢查約束-------check

6:自動編號列----------identity

7:外來鍵約束----------foreign key

使用dml語句更改資料

1:插入資料:(單行)insert into 表名  列名 values  列值;

例如:insert intostudent (id,name) values  (1,'張三');

(多行)insert into 表名 列名 select (語句)

例如:insert intostudent(id,name ) select id+2,name from students;

2:表複製:select 列名 into 新錶名  from 表名;

例如:select  *  intostudent2  from  student;

3:更新資料:update 表名 set 列n = 新值。。 where (過濾條件);

例如:update student set id = 2,age =20 where name = '張三'

更新來刪除資料:update student age= nullwhere name = '張三'

4:刪除資料:delete from 表名 where (過濾條

例如:delete fromstudent where name = '張三

注:刪除全表資料除去過濾條件即可,也可使用truncate table 表名

簡單資料查詢

&1:查詢:select 列名 from 表名;

例如:select id from student;

查詢全表資料:select * from student;

2: 表名字首:select student.id from student;

3:列表別名:select t.列 a  as  a,t.列b as b,t.列 c as c from 表 as t;

4:計算列:select  id , mark*2 as marksfrom student;

select firstname +  '  ' +lastname as fullname from student;

5:排除重複資料:select distinct 列a from 表名;\

例如:select  distinct name from student;

6:限定行數查詢:select  top  rowcount columa,columb from table;

例如: select top5 id , name from student;

7:條件查詢 where

8:範圍查詢 between

9:定義集合關係 in

10:模糊查詢 like(單個字元_  多個字元%)

11:空值資料控制:select 列a ,列b from  表名 where 列c is not null;

select 列a,列b from 表名 where 列c is null;

12:排序:公升序。。asc

降序。。。desc

聚合函式與分組

1:select count(規範) from 表名;

a:數目 count

b:總和 sum

c:平均值**g

d:最大值max

f:最小值 min

2:分組 group by..

3:結果集處理:select studentid,**g(mark) as **eragemark from studentexam group bystudentid h**ing **g(mark) <50 or **g(mark)>70;

4:exists;

5:all;

6:any;

7:union;

8:保留重複行:union all;

9:交集和差分:intersect except

聯接1:內聯接:join

2:程式設計客棧外連線:1:左外聯:left join或 left  outer join

2:右外聯:rightjoin 或 right outerjoin

3:全外聯:full join 或 full outer join

本文標題: 細數mysql中sql語句的分類

本文位址:

mysql語句查 mysql中SQL語句查

show tables 檢視或顯示所有表名 show databases 檢視或顯示所有資料庫名 desc 表名 檢視表結構 select form 表名 查詢表中所有欄位的值 select from 表名 order by 指定欄位名 desc 按照指定字段降序查詢表中資料資訊 select fr...

MySQL中SQL語句的分類

用於建立 修改 和刪除資料庫內的資料結構,如 1 建立和刪除資料庫 create database drop database 2 建立 修改 重新命名 刪除表 create table alter table rename table drop table 3 建立和刪除索引 createinde...

MySQL 中sql 語句 優化

參考文章 首先這個問題一直存在,但是在實際開發中還是寫出各種效能差sql。今天就一點點的來梳理一下,恰好我目前工作中的sql 寫的很慢,這篇文章剛好可以幫我去優化一下我的 1.乙個最大的原則就是能夠使得sql用到索引。怎麼用到索引,我們就來分析一下explain的用法 我關注的有乙個字段就是rows...