数据表的操作
创建数据表
查看当前数据库中所有表
- show tables;
创建表
create table 数据表名字(字段 类型 约束[,字段 类型 约束])
- create table xxxx(id int, name varchar(30));
- create table yyyy(id int primary key not null auto_increment, name varchar(30));
create table zzzz(
id int primary key not null auto_increment,
name varchar(30)
);
注:
- auto_increment表示自动增长
- not null 表示不能为空
- primary key可以表示主键
- default 默认值
创建students表(id、name、age、high、gender、cls_id)
create table students(
id int unsigned not null auto_increment primary key,
name varchar(30),
age tinyint unsigned,
high decimal(5,2),
gender enum("男","女","中性","保密") default "保密",
cls_id int unsigned
);
insert into students values (0,"张三",18,188.88,"男",0)
select * from students;
创建class表(id、name)
create table students(
id int unsigned not null auto_increment primary key,
name varchar(30),
);
insert into class values (0,"python12")
select * from students;
查看表结构
desc 数据表的名字;
- desc xxxx;
查看表的创建语句
show create table 表名字;
- show create table students;
表的增删改查
修改表-添加字段
alter table 表名 add 列名 类型;
- alter table students add birthday datetime;
- alter table students add birthday datetime [after 字段名]; 字段名后添加
- alter table students add birthday datetime [first]; 在第一个位置添加
注:[]表示可有可无
修改表-修改字段:不重命名版
alter table 表名 modify 列名 类型及约束;
- alter table students modify birthday date;
修改表-修改字段:重命名版
alter table 表名 change 原名 新名 类型及约束;
- alter table students change birthday birth date default “2000-01-01”;
修改表-修改字段-删除字段
alter table 表名 drop 列名;
- alter table students drop high";
删除表
drop table 表名;
drop table xxxxx;
注:drop database 数据库;
- drop table 数据表;
重命名表名
rename table 旧表名 to 新表明