常用mysql語句

2021-09-10 17:09:06 字數 2780 閱讀 8765

1.檢視資料庫列表

show databases;

2.建立資料庫

create database [資料庫名稱];

3.刪除資料庫

drop database [資料庫名稱];

4.進入指定資料庫

use [資料庫名稱];

5.建立表

create table [表名] ([列名][列型別](型別長度)[是否非空]) ;

6.刪除表

drop table [表名];

7.插入**資料

insert into [表名] (資料1,資料2,資料3);

注:**有多少列,插入時就要有多少個資料,如無資料則根據資料型別填寫0或null。

8.查詢**中的資料

select * from [表名];

select * from [表名] where [查詢條件];

9.修改**中的資料

update [表名] set [要修改的字段] = [新的字段];

update [表名] set [要修改的字段] = [新的字段] where [查詢條件];

10.刪除**中的資料

delete from [表名];            注:此操作將刪除**中所有記錄,但不會刪除**。

delete from [表名] where [條件];    注:刪除**中指定位置。

11.模糊查詢

select * from [表名] where like [欄位名稱];        注:模糊查詢全表中某一欄位。

select * from [表名] where [列名] like [欄位名稱];        注:模糊查詢**中某一列字段。

12.排序**中的資料

select * from [表名] order by [列名] desc;

13.**分組

select [列名] from [表名] where [條件] group bu [列名];

示例:select [列名] from [表名] where [條件:列名=『欄位名稱』] group by [列名];

14.內聯查詢

①左連線        獲取左表所有資料和右表與左表相匹配的記錄。

select [表名1,表名2] from [表名1] 1 left join [表名2] 2 on 1.[列名] = 2.[列名];

②右連線        獲取右表所有資料和左表與右表相匹配的記錄。

select [表名1,表名2] from [表名1] 1 right join [表名2] 2 on 1.[列名] = 2.[列名];

③內連線        獲取左表與右表相交集的記錄。

select [表名1,表名2] from [表名1] 1 inner join [表名2] 2 on 1.[列名] = 2.[列名];

15.查詢列中的空值

select * from [表名] where [列名] is null;

16.查詢正規表示式

select [列名] from [表名] where [列名] regexp [正則模式];

17.alter命令的使用

①刪除表中某一欄位

alter table [表名] drop [欄位名稱];

②增加表中某一欄位

alter table [表名] add [欄位名稱][字段型別];

注:新增成功後,會出現在**末尾。

③修改字段型別

alter table [表名] modify [欄位名稱][字段型別];

④修改字段型別及名稱

alter table [表名] change [欄位名稱1][欄位名稱2][欄位名稱2型別];

18.建立索引

create index [索引名稱] on [表名]([列名](length))

注:如果是char,varchar型別,length可以小於字段實際長度;如果是blob和text型別,必須指定 length。

19.索引——修改表結構

alter [表名] add index [索引名稱] on ([列名](length))

20.刪除索引

drop index [索引名稱] on [表名];

21.新增主鍵

alter table [表名] add primary key (列名);

示例:alter [表名] add primary key (列名);

注:主鍵預設不為空

22.刪除主鍵

alter table [表名] drop primary key;

常用mysql語句 常用MySql語句

新建資料表 drop table if exists ga game way create table ga game way id int 11 unsigned not null auto increment comment id primary key id using btree,主鍵 un...

常用MySQL語句

1.庫的操作 2.表的操作 3.索引的操作 4.檢視的操作 5.資料的操作 建立資料庫 create database database name 檢視資料庫 show databases 選擇資料庫 use database name 刪除資料庫 drop database database na...

常用MySql語句

alter table rbac user add name varchar 20 not null 在乙個表中增加一條字段 alter table rbac user drop name 刪除乙個字段 insert into rbac user1 select from rbac user 插入多...