1、创建表
create table customers
(
cust_id int not null auto_increment,
cust_name char(50) not null,
cust_sex char(1) not null default 0,
cust_add char(50) null,
cust_contact char(50) null,
primary key(cust_id)
);
2、修改表
a 新增列,并位于cust_sex后面。(afer只有在dos命令行才会起作用)
alter table mysql_test.customers
add column cust_city char(50) not null default 'wuhan' after cust_sex;
b、更新表
1、 修改列的名称或类型 (change)
alter table mysql_test.customes
change column cust_sex sex char(1) default 'm';
2、修改或删除表中的默认值。(alter)
alter table mysql_test.customers
alter column cust_city set default 'bj';
3、修改指定列的数据类型(modify)
//将cust_name列的字符长度改为20,并将此列设置为表的第一列
alter table mysql_test.customers
modify column cust_name char(20) first;
4、卸除多余的列(drop)
alter table mysql_test.customers
drop column cust_contact;
5、为表重新赋名(rename to)
alter table mysql_test.customers
rename to mysql_test.backup_customers;
3、重新命名表
rename table mysql_test.backup_customers to mysql_test.customers;
4、查看数据库中的表
show tables from mysql_test;
5、查看数据库中表的所有列
show columns from mysql_test.customers;