您好,欢迎访问一九零五行业门户网

MySQL数据表的基本操作二:表结构查看、修改与表操作_MySQL

一、查看数据表结构
1) 查看表基本结构语句 describe
语法:describe 表名;
示例:
mysql> describe product;+--------------+--------------+------+-----+---------+----------------+| field | type | null | key | default | extra |+--------------+--------------+------+-----+---------+----------------+| product_id | int(11) | no | pri | null | auto_increment || product_name | varchar(50) | no | | null | || description | varchar(200) | yes | | null | |+--------------+--------------+------+-----+---------+----------------+
语法:desc 表名;
示例:
mysql> desc product;+--------------+--------------+------+-----+---------+----------------+| field | type | null | key | default | extra |+--------------+--------------+------+-----+---------+----------------+| product_id | int(11) | no | pri | null | auto_increment || product_name | varchar(50) | no | | null | || description | varchar(200) | yes | | null | |+--------------+--------------+------+-----+---------+----------------+
说明:null: 表示该列是否可以存储null值;key: 表示该列是否已编制索引。pri表示是表主键的一部分;uni表示该列是unique索引一部分;mul表示该列中某个给定值允许出现多次;default: 表示该列是否有默认值,如果有是多少;extra: 表示可以获取的与给定列有关的附加信息。例如auto_increment等;
2) 查看表详细结构语句 show create table
功能说明:
用来显示创建表时的语句
语法:
show create table ;
提示:该语句可以用来查看创建表的详细语句;还可以用来查看存储引擎和字符编码;加上参数'\g'后,可使显示结果更加直观,易于查看;
示例:
mysql> show create table bm\g;*************************** 1. row *************************** table: bmcreate table: create table `bm` ( `id` int(11) not null, `name` varchar(22) default null, `location` varchar(20) default null, primary key (`id`), unique key `sth` (`name`)) engine=innodb default charset=latin11 row in set (0.02 sec)
二、修改数据表1) 修改表名
语法
alter table rename [to] ;
示例
alter table bm rename department;
2) 修改字段的数据类型
语法
alter table modify ;
示例
alter table department modify id varchar(11);
3) 修改字段名
语法
alter table change ;
示例
alter table employees change location loc varchar(350);//类型可以和原来保持一样,不用修改
4) 添加字段
语法
alter table add [约束条件] [first|after已存在字段名];//默认添加到最后一列后面
示例
alter table employees add manager_id int(10);alter table employees add age int(11) not null;//非空约束alter table employees add sal float first;//在表的第一列添加alter table employees add hire_date date after manager_id;//在指定列后添加
5) 删除字段
语法
alter table drop ;
示例alter table employees drop manager_id;alter table employees drop hire_date;
6) 修改字段的排列位置
语法
alter table modify first|after ;
示例alter table employees modify name varchar(22) first;//把name移动到第一列alter table department modify location varchar(350) after department_id;//把location列移到department_id列之后
7) 更改表的存储引擎
mysql支持的主要存储引擎
引擎名 是否支持
federated 否
mrg_myisam 是
myisam 是
blackhole 是
csv 是
memory 是
archive 是
innodb 默认
performance_schema 是
语法
alter table engine=;
示例alter table department engine=myisam;
8) 删除表的外键约束
语法
alter table drop foreign key ;
示例 alter table employees drop foreign key fk_emp_dept;
三、删除数据表
1) 删除没有被关联的表
语法
drop table [if exists] 表1,表2,...表n;
示例
drop table if exists employees;
2) 删除被其他表关联的主表
说明:先要删除外键
示例 mysql> create database rm_tab;query ok, 1 row affected (0.00 sec)mysql> use rm_tab;database changedmysql> create table department -> ( -> id int(11) primary key, -> name varchar(22), -> location varchar(200) -> );query ok, 0 rows affected (0.07 sec)mysql> create table employee -> ( -> id int(11) primary key, -> name varchar(25), -> department_id int(11), -> salary float, -> constraint fk_emp_dept foreign key(department_id) references department(id) -> );query ok, 0 rows affected (0.10 sec)mysql> drop table department;error 1217 (23000): cannot delete or update a parent row: a foreign key constraint failsmysql> alter table employee drop foreign key fk_emp_dept;query ok, 0 rows affected (0.22 sec)records: 0 duplicates: 0 warnings: 0mysql> drop table department;query ok, 0 rows affected (0.04 sec)mysql> show tables;+------------------+| tables_in_rm_tab |+------------------+| employee |+------------------+1 row in set (0.00 sec)
如果您们在尝试的过程中遇到什么问题或者我的代码有错误的地方,请给予指正,非常感谢!
联系方式:david.louis.tian@outlook.com
版权@:转载请标明出处!
其它类似信息

推荐信息