mysql資料表的基本操作之表結構操作,欄位操作例項分析
阿新 • • 發佈:2020-04-17
本文例項講述了mysql資料表的基本操作之表結構操作,欄位操作。分享給大家供大家參考,具體如下:
本節介紹:
表結構操作
- 建立資料表、
- 檢視資料表和檢視欄位、
- 修改資料表結構
- 刪除資料表
欄位操作
- 新增欄位、
- 修改欄位資料型別、位置或屬性、
- 重新命名欄位
- 刪除欄位
首發時間:2018-02-18 21:31
表結構操作
建立資料表:
-
語法 :
create table [if not exists] 表名( 欄位名字 資料型別,欄位名字 資料型別 )[表選項];
-
表選項 :
- 字符集:charset表中儲存資料的字符集
- 校對集:colloate表中用來校對資料的校對集
- 儲存引擎 :engine儲存資料的儲存引擎
- 表選項和庫選項的區別是,如果不設定表選項就會採用庫選項的設定,就好象一個“區域性變數”。
-
使用示例 :
-- 建表之前必須指定資料庫,可以使用use來指定後續的操作是基於哪個資料庫的 ,也可以使用資料庫名作為字首來指定資料表建立在哪個資料庫。
-- 使用資料庫名作為字首來指定資料表建立在哪個資料庫。 create table if not exists mydatabase.student( name varchar(20),sex varchar(20),number varchar(20),age int )charset utf8;-- 使用use來指定後續操作基於哪個資料庫 use mydatabase; create table if not exists class( name varchar(20),room varchar(20) )charset utf8; -- 演示不帶表選項的建立表 use mydatabase; create table if not exists class( name varchar(20),room varchar(20) );
- 補充說明 :
- if not exists 是先檢查是否存在同名的表,如果存在,則不執行後面的建立語句。 十分建議使用。如果你確定這個表不存在,那麼可以不使用。
- 如果沒有指定表選項,將使用預設的,比如mysql預設的儲存引擎是innodb。
檢視資料表 :
檢視資料表可以檢視已有資料表、資料表的欄位資訊
- 語法 :
-- 檢視所有表 show tables; -- 檢視部分表 show tables like '模糊匹配'; -- 查看錶的建立語句 show create table 資料表名; -- 旋轉檢視結構 show create table 資料表名\G; -- 查看錶結構:查看錶中的欄位資訊: Desc/desc 表名; describe 表名; show columns from 表名;
- 模糊匹配:
- _匹配單個字元
- %匹配多個字元
- 使用示例 :
show tables;
show tables like 'my%';
show create table student;
show create table student\G;
desc student; describe student; show columns from student;
圖例:
- show create table student;跟show create table sudent\G;
Desc/describe /show columns from 表名;
修改資料表結構 :
修改表只能修改表名和表選項。
- 語法 :
-- 修改表名: rename table 老表名 to 新表名; --修改表選項: Alter table 表名 表選項 [=] 值;
- 使用示例 :
rename table student to my_student; rename table class to my_class; -- Alter table my_student charset gbk; Alter table my_collation_bin collate =utf8_bin;
刪除資料表 :
- 語法 :
Drop table 表名1,表名2...;
- 使用示例 :
drop table demo; drop table demodata;
- 補充說明 :
- 刪除不可恢復,刪除需謹慎。
欄位操作 :
新增欄位 :
新增欄位是在表存在的基礎上新增欄位
- 語法 :
Alter table 表名 add [column] 欄位名 資料型別 [列屬性] [位置];
- 使用示例 :
Alter table 表名 add [column] 欄位名 資料型別 [列屬性] [位置]; Alter table demo add column id int first; Alter table demo add id int; Alter table demo add class int after age; Alter table demo add number int not null after age;
- 補充說明 :
- 位置常用語法:
- first :表示位於第一列,
- after 欄位名 :代表在某個欄位後面;
- 列屬性:主鍵,空值 等;
- 位置常用語法:
修改欄位 :
修改欄位一般都是修改欄位資料型別或者欄位屬性
- 語法 :
Alter table 表名 modify 欄位名 資料型別 [屬性] [位置];
- 使用示例 :
Alter table my_student modify number char(10) after id; Alter table demo modify number int null ; -- alter table student modify name varchar(20) not null; -- alter table student modify name varchar(20) not null primary key;
- 補充說明 :
- 欄位名和資料型別是必填的,屬性和位置是選填的。
- 如果欄位本身帶著屬性的,那麼必須帶上原來的,否則會被去除掉;如果需要在原有屬性的基礎上新增新的屬性,則在填入時,那麼在帶上原有屬性的基礎上加上新的屬性即可
重新命名欄位 :
- 語法 :
Alter table 表名 change 舊欄位 新欄位 資料型別 [屬性] [位置];
- 使用示例 :
alter table demo change class room varchar(10); Alter table my_student change sex gender varchar(10);
- 補充說明 :
- 資料型別是必填的,但可以是新的【重名欄位可以順便改資料型別】
- 改名的同時也能修改欄位的資料型別,屬性和位置。【如果欄位帶有屬性,重新命名欄位時可以不帶上】
刪除欄位 :
- 語法 :
Alter table 表名 drop 欄位名;
- 使用示例 :
Alter table my_student drop age; alter table demo drop room;
- 補充說明 :
- 刪除需謹慎,刪除欄位代表著將該欄位下的所有資料都將刪除。
更多關於MySQL相關內容感興趣的讀者可檢視本站專題:《MySQL查詢技巧大全》、《MySQL事務操作技巧彙總》、《MySQL儲存過程技巧大全》、《MySQL資料庫鎖相關技巧彙總》及《MySQL常用函式大彙總》
希望本文所述對大家MySQL資料庫計有所幫助。