mysql數(shù)據(jù)表的基本操作之表結(jié)構(gòu)操作,字段操作實(shí)例分析
本文實(shí)例講述了mysql數(shù)據(jù)表的基本操作之表結(jié)構(gòu)操作,字段操作。分享給大家供大家參考,具體如下:
本節(jié)介紹:表結(jié)構(gòu)操作 創(chuàng)建數(shù)據(jù)表、 查看數(shù)據(jù)表和查看字段、 修改數(shù)據(jù)表結(jié)構(gòu) 刪除數(shù)據(jù)表字段操作 新增字段、 修改字段數(shù)據(jù)類型、位置或?qū)傩浴? 重命名字段 刪除字段首發(fā)時間:2018-02-18 21:31
表結(jié)構(gòu)操作創(chuàng)建數(shù)據(jù)表: 語法 :create table [if not exists] 表名(字段名字 數(shù)據(jù)類型,字段名字 數(shù)據(jù)類型)[表選項(xiàng)]; 表選項(xiàng) : 字符集:charset表中存儲數(shù)據(jù)的字符集 校對集:colloate表中用來校對數(shù)據(jù)的校對集 存儲引擎 :engine存儲數(shù)據(jù)的存儲引擎 表選項(xiàng)和庫選項(xiàng)的區(qū)別是,如果不設(shè)置表選項(xiàng)就會采用庫選項(xiàng)的設(shè)置,就好象一個“局部變量”。使用示例 :
-- 建表之前必須指定數(shù)據(jù)庫,可以使用use來指定后續(xù)的操作是基于哪個數(shù)據(jù)庫的 ,也可以使用數(shù)據(jù)庫名作為前綴來指定數(shù)據(jù)表創(chuàng)建在哪個數(shù)據(jù)庫。-- 使用數(shù)據(jù)庫名作為前綴來指定數(shù)據(jù)表創(chuàng)建在哪個數(shù)據(jù)庫。create table if not exists mydatabase.student(name varchar(20),sex varchar(20),number varchar(20),age int)charset utf8;-- 使用use來指定后續(xù)操作基于哪個數(shù)據(jù)庫use mydatabase;create table if not exists class(name varchar(20),room varchar(20))charset utf8;-- 演示不帶表選項(xiàng)的創(chuàng)建表use mydatabase;create table if not exists class(name varchar(20),room varchar(20)); 補(bǔ)充說明 : if not exists 是先檢查是否存在同名的表,如果存在,則不執(zhí)行后面的創(chuàng)建語句。 十分建議使用。如果你確定這個表不存在,那么可以不使用。 如果沒有指定表選項(xiàng),將使用默認(rèn)的,比如mysql默認(rèn)的存儲引擎是innodb。 查看數(shù)據(jù)表 :
查看數(shù)據(jù)表可以查看已有數(shù)據(jù)表、數(shù)據(jù)表的字段信息
語法 :-- 查看所有表show tables;-- 查看部分表show tables like ’模糊匹配’;-- 查看表的創(chuàng)建語句show create table 數(shù)據(jù)表名;-- 旋轉(zhuǎn)查看結(jié)構(gòu)show create table 數(shù)據(jù)表名G;-- 查看表結(jié)構(gòu):查看表中的字段信息:Desc/desc 表名;describe 表名;show columns from 表名; 模糊匹配: _匹配單個字符 %匹配多個字符 使用示例 :
show tables;show tables like ’my%’;show create table student;show create table studentG;desc student;describe student;show columns from student;
圖例:
show create table student;跟show create table sudentG;Desc/describe /show columns from 表名;
修改表只能修改表名和表選項(xiàng)。
語法 :-- 修改表名:rename table 老表名 to 新表名;--修改表選項(xiàng):Alter table 表名 表選項(xiàng) [=] 值; 使用示例 :
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;刪除數(shù)據(jù)表 : 語法 :
Drop table 表名1,表名2...; 使用示例 :
drop table demo;drop table demodata; 補(bǔ)充說明 : 刪除不可恢復(fù),刪除需謹(jǐn)慎。 字段操作 :新增字段 :
新增字段是在表存在的基礎(chǔ)上新增字段
語法 :Alter table 表名 add [column] 字段名 數(shù)據(jù)類型 [列屬性] [位置]; 使用示例 :
Alter table 表名 add [column] 字段名 數(shù)據(jù)類型 [列屬性] [位置];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; 補(bǔ)充說明 : 位置常用語法: first :表示位于第一列, after 字段名 :代表在某個字段后面; 列屬性:主鍵,空值 等; 修改字段 :
修改字段一般都是修改字段數(shù)據(jù)類型或者字段屬性
語法 :Alter table 表名 modify 字段名 數(shù)據(jù)類型 [屬性] [位置]; 使用示例 :
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; 補(bǔ)充說明 : 字段名和數(shù)據(jù)類型是必填的,屬性和位置是選填的。 如果字段本身帶著屬性的,那么必須帶上原來的,否則會被去除掉;如果需要在原有屬性的基礎(chǔ)上添加新的屬性,則在填入時,那么在帶上原有屬性的基礎(chǔ)上加上新的屬性即可 重命名字段 : 語法 :
Alter table 表名 change 舊字段 新字段 數(shù)據(jù)類型 [屬性] [位置]; 使用示例 :
alter table demo change class room varchar(10);Alter table my_student change sex gender varchar(10); 補(bǔ)充說明 : 數(shù)據(jù)類型是必填的,但可以是新的【重名字段可以順便改數(shù)據(jù)類型】 改名的同時也能修改字段的數(shù)據(jù)類型,屬性和位置。【如果字段帶有屬性,重命名字段時可以不帶上】 刪除字段 : 語法 :
Alter table 表名 drop 字段名; 使用示例 :
Alter table my_student drop age;alter table demo drop room; 補(bǔ)充說明 : 刪除需謹(jǐn)慎,刪除字段代表著將該字段下的所有數(shù)據(jù)都將刪除。
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL查詢技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲過程技巧大全》、《MySQL數(shù)據(jù)庫鎖相關(guān)技巧匯總》及《MySQL常用函數(shù)大匯總》
希望本文所述對大家MySQL數(shù)據(jù)庫計(jì)有所幫助。
