MySQL 列的操作

1.建表语句

create table 表名(
列名称 列类型 [列属性] [默认值],   ---》列声明
列名称 列类型 [列属性] [默认值],
列名称 列类型 [列属性] [默认值]
);

create table stu (name varchar(10),class varchar(10),score tinyint);

insert stu
(name,class,score)
values
("zhangsan","shuxue",50),
("zhangsan","dili",40),
("zhangsan","shuxue",90),
("lisi","yuwen",55),
("lisi","zhengzhi",45),
("wangwu","zhengzhi",30);

#把    列名称 列类型 [列属性] [默认值]    封装成    列声明
后面就用“ 列声明 ”来代替“ 列名称 列类型 [列属性] [默认值] ”

2.增加一个列

alter table 表名 add 列声明;
alter table stu add height tinyint unsigned not null default 180;
增加的列默认是在表的最后一列

可以用after 来声明新增的列在哪一些后面:
alter table 表名 add 列声明 after 列名;
alter table stu add age tinyint unsigned not null default 18 after score;

如果要放在表的最前面:
alter table 表名 add 列声明 first;
alter table stu add id int primary key auto_increment first;
primary key auto_increment(设为主键,并自动增加)

3.修改列

alter table 表名 change 被修改的列 新的列声明
alter table stu change height height smallint unsigned not null default 180;
alter table stu change height weight smallint unsigned not null default 180;

4.删除列

alter table 表名 drop 列名
alter table stu drop age;

猜你喜欢

转载自blog.csdn.net/hk_jy/article/details/80429092