5、表管理语句

查看表

> show tables;
#查看当前库中所有表的具体属性信息 
> show table status\G;
#查看当前库某张表的状态
> show table status like 'ttt'\G;
#查看当前库表名类似表的状态
> show table status where name like '%tt%'\G;
#查看表结构
> desc Table_Name;
#查看表被创建时对应的sql语句
> show create table table_name;
创建表
创建表语法
CREATE TABLE [IF NOT EXISIS] tbl_name(create_definition)

示例
> create table test (id int(11), name varchar(60));
#主键约束,非空约束
> create table test1 (id int(11) key, name varchar(60) not null comment 'student name');
> create table test1 (id int(11) , name varchar(60) not null, primary key(id));
#联合主键
> create table test1 (id int, name varchar(60) not null, primary key(id,name));
#定义外键
> create table test1 (id int primary key, name varchar(60) not null, tid int, foreign key(tid) references test2(id);
#创建数据库表时创建ind_name索引
> create table test4 (id int(11) , name varchar(60), primary key(id),index ind_name(name));
> create table test4 (id int(11) , name varchar(60), primary key(id),key ind_name(name));

#other
> create table students (id int primary key auto_increment,name varchar(40) not null, age tinyint unsigned, gender enum('f','m') default 'm', index(name));
#查询的数据创建新表
> create table test4 select * from students;
#单纯的完整的复制表的结构
> create table test5 like students;

> create table tt (id int primary key auto_increment, name varchar(60) not null, index ind_name(name)) engine=InnoDB auto_increment=2  default charset=utf8;
删除表
> drop table students;
> drop table if exists tt,ttt;
修改表
#修改表名
> alter table test1 rename as test2;
#添加字段
> alter table ttt add column age int;
> alter table ttt add age int;
> alter table ttt add age int not null default 0;
> alter table ttt add column age int not null default 0;
> alter table ttt add  id int first;
> alter table ttt add  id int first;
#删除字段
> alter table tt drop stuname;
#重命名字段
> alter table testtable change name name1 char(5); 
#修改字段
> alter table testtable change  age age char(10);
> alter table testtable modify age int;

参考

猜你喜欢

转载自www.cnblogs.com/kcxg/p/10360203.html