MySQL操作數(shù)據(jù)表的方法并不復(fù)雜,下面將為您詳細(xì)介紹MYSQL添加字段、修改字段、刪除字段、 獲取表名等操作的實(shí)現(xiàn)方法,希望對(duì)您學(xué)習(xí)MySQL添加字段方面會(huì)有所幫助。
MySQL添加字段的方法并不復(fù)雜,下面將為您詳細(xì)介紹MYSQL添加字段和修改字段等操作的實(shí)現(xiàn)方法,希望對(duì)您學(xué)習(xí)MySQL添加字段方面會(huì)有所幫助。
1添加表字段
alter table table1 add transactor varchar(10) not Null; alter table table1 add id int unsigned not Null auto_increment primary key
添加到特定字段后面的語(yǔ)句例子:
ALTER TABLE <表名> ADD <新字段名><數(shù)據(jù)類型>[約束條件]; ALTER TABLE MyTableName ADD newDBField varchar(30) NULL AFTER existDBField; ALTER TABLE tableName001 ADD WebAdminPass varchar(30) NULL AFTER Result;
2.修改某個(gè)表的字段類型及指定為空或非空
alter table 表名稱 change 字段名稱 字段名稱 字段類型 [是否允許非空]; alter table 表名稱 modify 字段名稱 字段類型 [是否允許非空]; alter table 表名稱 modify 字段名稱 字段類型 [是否允許非空];
3.修改某個(gè)表的字段名稱及指定為空或非空
alter table 表名稱 change 字段原名稱 字段新名稱 字段類型 [是否允許非空
4如果要?jiǎng)h除某一字段,可用命令:
ALTER TABLE mytable DROP 字段名;
mysql SQL獲取表名&字段名的查詢語(yǔ)句
1:查詢數(shù)據(jù)庫(kù)中所有表名
select table_name from information_schema.tables where table_schema='csdb' and table_type='base table'; table_schema:數(shù)據(jù)庫(kù)名稱 information_schema 表示系統(tǒng)庫(kù)。 table_type='base table‘:限定只查詢基表。
2:查詢指定數(shù)據(jù)庫(kù)中指定表的所有字段名column_name
select column_name from information_schema.columns where table_schema='csdb' and table_name='users'; table_schema:數(shù)據(jù)庫(kù)名 table_name:表名
工作用到例子:
select count(*) from information_schema.columns where table_schema='yanfa' and table_name='tableName001' and column_name='Result1'; #select table_name from information_schema.tables where table_schema='yanfa' and table_type='base table';
相關(guān)學(xué)習(xí)推薦:mysql教程(視頻)