本文實(shí)例講述了mysql數(shù)據(jù)表的基本操作之表結(jié)構(gòu)操作,字段操作。分享給大家供大家參考,具體如下:
首發(fā)時(shí)間:2018-02-18 21:31
create table [if not exists] 表名( 字段名字 數(shù)據(jù)類型, 字段名字 數(shù)據(jù)類型 )[表選項(xiàng)];
-- 建表之前必須指定數(shù)據(jù)庫,可以使用use來指定后續(xù)的操作是基于哪個(gè)數(shù)據(jù)庫的 ,也可以使用數(shù)據(jù)庫名作為前綴來指定數(shù)據(jù)表創(chuàng)建在哪個(gè)數(shù)據(jù)庫。
-- 使用數(shù)據(jù)庫名作為前綴來指定數(shù)據(jù)表創(chuàng)建在哪個(gè)數(shù)據(jù)庫。 create table if not exists mydatabase.student( name varchar(20), sex varchar(20), number varchar(20), age int )charset utf8;
-- 使用use來指定后續(xù)操作基于哪個(gè)數(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) );
查看數(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 student\G;
desc student; describe student; show columns from student;
圖例:
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;
Drop table 表名1,表名2...;
drop table demo; drop table demodata;
新增字段是在表存在的基礎(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;
修改字段一般都是修改字段數(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;
Alter table 表名 change 舊字段 新字段 數(shù)據(jù)類型 [屬性] [位置];
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;
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL查詢技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲(chǔ)過程技巧大全》、《MySQL數(shù)據(jù)庫鎖相關(guān)技巧匯總》及《MySQL常用函數(shù)大匯總》
希望本文所述對(duì)大家MySQL數(shù)據(jù)庫計(jì)有所幫助。
標(biāo)簽:恩施 寧夏 秦皇島 定西 杭州 白銀 益陽 澳門
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《mysql數(shù)據(jù)表的基本操作之表結(jié)構(gòu)操作,字段操作實(shí)例分析》,本文關(guān)鍵詞 mysql,數(shù)據(jù)表,的,基本操作,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。