首页  >  文章  >  数据库  >  MySQL如何操作数据表

MySQL如何操作数据表

醉折花枝作酒筹
醉折花枝作酒筹转载
2021-06-29 09:22:071751浏览

MySQL操作数据表的方法并不复杂,下面将为您详细介绍MYSQL添加字段、修改字段、删除字段、 获取表名等操作的实现方法,希望对您学习MySQL添加字段方面会有所帮助。

MySQL如何操作数据表

MySQL添加字段的方法并不复杂,下面将为您详细介绍MYSQL添加字段和修改字段等操作的实现方法,希望对您学习MySQL添加字段方面会有所帮助。

1添加表字段

alter table table1 add transactor varchar(10) not Null;
alter table   table1 add id int unsigned not Null auto_increment primary key

添加到特定字段后面的语句例子:

ALTER TABLE <表名> ADD <新字段名><数据类型>[约束条件];
ALTER TABLE MyTableName ADD newDBField varchar(30) NULL AFTER existDBField;
ALTER TABLE tableName001 ADD WebAdminPass varchar(30) NULL AFTER Result;

2.修改某个表的字段类型及指定为空或非空

alter table 表名称 change 字段名称 字段名称 字段类型 [是否允许非空];
alter table 表名称 modify 字段名称 字段类型 [是否允许非空];
alter table 表名称 modify 字段名称 字段类型 [是否允许非空];

3.修改某个表的字段名称及指定为空或非空

alter table 表名称 change 字段原名称 字段新名称 字段类型 [是否允许非空

4如果要删除某一字段,可用命令:

ALTER TABLE mytable DROP 字段名;

mysql SQL获取表名&字段名的查询语句

  1:查询数据库中所有表名

select table_name
  from information_schema.tables
  where table_schema=&#39;csdb&#39; and table_type=&#39;base table&#39;;
table_schema:数据库名称     
information_schema 表示系统库。   
table_type=&#39;base table‘:限定只查询基表。

  2:查询指定数据库中指定表的所有字段名column_name

   select column_name
     from information_schema.columns
     where table_schema=&#39;csdb&#39; and table_name=&#39;users&#39;;
  table_schema:数据库名   
  table_name:表名

工作用到例子:

select count(*) from information_schema.columns where table_schema=&#39;yanfa&#39; and table_name=&#39;tableName001&#39; and column_name=&#39;Result1&#39;;
 #select table_name from information_schema.tables where table_schema=&#39;yanfa&#39; and table_type=&#39;base table&#39;;

相关学习推荐:mysql教程(视频)

以上是MySQL如何操作数据表的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:csdn.net。如有侵权,请联系admin@php.cn删除