2.3创建数据表

MySQL笔记2.3创建数据表_表名

create database student;/*创建名为student*/
use itcase;/*使用数据库*/
create TABLE tb_grade/*创建名为tb_gradea的数据表*/
(
	id INT(11),/*举例*/
	name VARCHER(20),
	grade FLOAT
);
show tables;/*验证数据库是否创建成功*/ 
show create table tb_greade;/*查看数据表创建时的定义语句,查看表的字符编码*/
describe tb_greade;/*查看表的字段信息,包括字段名、字段类型等信息*/

修改数据表

​ 1.修改表名;

alter table tb_grade rename(to) grade;

​ 2.修改字段名;

alter table 表名 change 旧字段名 新字段名 新数据类型;
alter table grade change name username verchar(20);/*name字段改为username,数据类型保持不变*/
desc greade;/*验证*/

​ 3.修改字段的数据类型;

alter table 表名 modify 字段名 数据类型;
alter table grade modify id int(20);/**/

​ 4.添加字段;

alter table 表名 add 新字段名 数据类型
	[约束条件][first|after 已存在的字段名]
alter table grade add age int(10);
alter table grade add age int(10) first;/*添加到第一位*/
alter table grade add age int(10) after id/*添加到id后面*/;
alter table grade add age int(10) not null first;/*不允许为空*/

​ 5.删除字段;

alter table 表名 drop 字段名;
alter table grade drop age;

​ 6.修改字段的排列位置;

alter table 表名 modify 字段名1 数据类型 first|after 字段名2;
字段名1 指要修改位置的字段;
first是可选参数,指的是将字段1修改为表的第一个字段;
after 字段名2 是将字段1插入到字段2后面;
alter table grade modify id int(10) after grade;/*将id字段插入到grade字段后面*/

删除数据表

drop table 表名;
drop table grade;
desc table grade;/*验证*/