mysql 語法入門 mysql基礎語法

2021-10-18 11:18:03 字數 2647 閱讀 1430

1、dml-增刪改查

(1)select - 獲取資料(select * from 表名 where 條件)

(2)update - 更新資料(update 表名 set (欄位名=值,欄位名=值) where 條件)

(3)delete - 刪除資料(delete from 表名 where 條件)

(4)insert into - 插入資料(insert into 表名(字段) values(值))

2、ddl-建立與檢視

(1)create - 建立(create database/table (if not exists) 庫名/表名 character set utf8)

(2)show - 檢視(show databases/tables 檢視所有庫或表)

3、alter-修改定義

(1)修改庫或表預設字符集格式 - (alert database/table 庫名/表名 character set utf8)

(2)表名重新命名 - (alter table 舊表名 rename to 新錶名)

(2)rename table - 本語句用於對乙個或多個表進行重新命名(rename table (舊表名 to 新錶名)/[舊表名 to 新錶名,舊表名 to 新錶名] )

(3)欄位名修改 - (alter table 表名 change 舊欄位名 新欄位名 integer)

(4)字段型別修改 - (alter table 表名 change 舊欄位名 新欄位名 字段型別 )

(5)新增字段 - (alter table 表名 add column title varchar(20) not null after id)

(6)刪除字段 - (alter table 表名 drop column title)

4、drop-刪除庫/表

(drop database/table (if exists) 庫名/表名)

5、constraint - 約束

(1)非空約束(not null)(alter table 表名 modify 欄位名 型別 null)

(2)唯一約束(unique)(alter table 表名 drop index 唯一約束名)

constraint 唯一約束名 unique(欄位名,欄位名)

(3)主鍵約束(主鍵自增模式auto_increment)(alter table 表名 drop primary key)

欄位名 primary key

constraint 主鍵約束名 primary key(主鍵欄位名)

(4)外來鍵約束(alter table 表名 drop foreign key 外來鍵約束名)

constraint 外來鍵約束名 foreign key(外來鍵欄位名)references 主鍵的表名(主鍵欄位名)

(5)check約束

6、view - 檢視

create view 檢視名 as sql語句(不能包含子查詢)

drop view 檢視名

7、transaction - 事務

start transaction

sql語句

commit - 提交

rollback - 回滾

8、procedure - 儲存過程

儲存過程(引數型別有(1)in (2)out (3)in和out同時有)

create procedure 命名()

begin

sql語句

endcall 命名(實參)(in)

call 命名(@實參)(out)

call 命名(實參,@實參)(in和out同時有)

select @實參

用@符號加變數名的方式定義乙個變數(set @s=10)

drop procedure (if exists) 命名

9、index - 索引

(1)普通索引

create index index_name on 表名(column(length))

alter table 表名 add index index_name on (column(length))

index index_name (column(length))

(2)唯一索引

create unique index indexname on 表名(column(length))

alter table 表名 add unique indexname on (column(length))

unique indexname (title(length))

(3)全文索引

create fulltext index 索引名字 on 表名(字段)

alter table 表名 add fulltext index_content(字段)

fulltext (content)

(4)單列索引和多列索引

多個單列索引與單個多列索引的查詢效果不同,因為執行查詢時,mysql只能使用乙個索引,會從多個索引中選擇乙個限制最為嚴格的索引。

(5)組合索引

alter table 表名 add index index_titme_time (title(50),time(10))

建立這樣的組合索引,其實是相當於分別建立了下面兩組組合索引:

–title,time

–title

mysql的基本操作語法 MySQL操作基本語法

建立表 create table student id number 8 primary key not null,name varchar 20 not null,address varchar2 50 default 位址不詳 插入資料 insert into student id,name v...

mysql資料庫名語法 MySQL資料庫基本語法

1,檢視資料庫 show databases 2,選擇要操作的資料庫 use 資料庫名 3,建立資料庫 create database 資料庫名稱 4,刪除資料庫 drop database 資料庫名稱 5,建立表 create table 表名 列名 列型別,6,檢視當前資料庫所有表 show t...

mysql 語法 mysql的刪除語法

刪除資料庫 drop database test 刪除表 drop table ifexists test 刪除表資料 delete from test where id 1 不會減少表或索引所占用的空,可以加where條件,可以進行rollback回滾 truncate table test 執行...