MySQL基本操作

2021-10-07 23:56:40 字數 1700 閱讀 1731

1.資料庫基本操作:

檢視資料庫:show databases;

建立資料庫:create database database_name;

檢視建立好的資料庫:show create database database_name\g

刪除資料庫:drop database database_name;

2.資料表的基本操作:

建立資料表:

create table 《表名》

( 欄位名1

: 資料型別 [列級別的約束條件]

[預設值]

, 欄位名2

: 資料型別 [列級別的約束條件]

[預設值],.

....

..[表級別約束條件]);

單字段主鍵:

欄位名 資料型別 primary key [預設值]

[constraint 《約束名》

] primary key [欄位名]

多欄位聯合主鍵:

primary key [欄位1

, 欄位2,.

.., 欄位n]

外來鍵約束:

[constraint 《外鍵名》

] foreign key 欄位1

[, 欄位2,.

..] references 《主表名》 主鍵列1

[, 主鍵2,.

..]非空約束:

欄位名 資料型別 not null

唯一約束:

欄位名 資料型別 unique

預設約束:

欄位名 資料型別 default 預設值

表屬性值自動增加:

欄位名 資料型別 auto_increment

檢視所有表:

show tables;

檢視表結構:

describe 表名; 或者 desc 表名;

檢視表詳細結構:

show create table 《表名\g>

;修改表名:

alter table 《舊表名》 rename [to]

《新錶名》

;修改字段資料型別

alter table 《表名》 modify 《欄位名》

《資料型別》

修改欄位名:

alter table 《表名》 changer 《舊欄位名》

《新欄位名》

《新資料型別》

;新增字段:

alter table 《表名》 add 《新欄位名》

《資料型別》

[約束條件]

[first | after 已存在字段]

;刪除字段:

alter table 《表名》 drop 《欄位名》

;修改欄位的排列位置:

alter table 《表名》 modify 《欄位1

>

《資料型別》 first|after 《欄位2

>

;更改表的儲存引擎:

alter table 《表名》 engine=

《更改後的儲存引擎名》

;刪除表的外來鍵約束:

alter table 《表名》 drop foreign key 《外來鍵約束名》

刪除資料表:

drop table [if exists] 表1

, 表2,.

..表n;

mysql基本操作 MySQL基本操作

mysql中新增使用者,新建資料庫,使用者授權,刪除使用者,修改密碼 注意每行後邊都跟個 表示乙個命令語句結束 1.新建使用者 1.1 登入mysql mysql u root p 密碼 1.2 建立使用者 mysql insert into mysql.user host,user,passwor...

mysql 基本操作 mysql基本操作

mysql 建立表,並設定主鍵自增 create table log logid int 4 primary key not null auto increment,logtitle varchar 32 not null logcontent varchar 160 not null logtim...

mysql基本操作

1,檢視資料庫狀態 及啟動停止 etc init.d mysqld status etc init.d mysqld start etc init.d mysqld stop 2,給使用者配置初始密碼123456 mysqladmin u root password 123456 3,修改root使...