数据库的操作
数据库的字段属性:
Unsigned:
- 无符号整数
- 声明了该列值不能为负数
zeroFill:
- 0填充的
- 不足位数的,使用0来填充
自增:
- 自增
- 可以自定义增加的数量和步长
非空:
- NUll not null
- 设置为非空不给赋值就会报错
默认:
- 如果不赋值则为默认设置的值
操作数据库>操作数据库的表>操作表中的数据
操作数据库:
创建数据库
create database westos;
删除数据库
drop database westos;
使用数据库
use shop;
查看所有数据库
show databases;
操作数据库的表:
创建表
create table if not exists `student`(
`id` int(4) not null auto_increment comment 'id',
`name` varchar(30) not null default '匿名' comment '姓名',
`pwd` varchar(20) not null default '123123' comment '密码',
`sex` varchar(2) not null default 'nv' comment '性别',
`birthday` datetime default null comment '出生日期',
`address` varchar(100) default null comment '住址',
`email` varchar(50) default null comment '邮箱',
primary key(`id`)
)engine=innodb default charset=utf8;
格式:
create table if not exists `表名`(
`字段名` 列类型 [属性][索引][注释],
`字段名` 列类型 [属性][索引][注释],
......
`字段名` 列类型 [属性][索引][注释]
);
删除表
drop table teacher if exists
修改表
修改表名: alter table 表名 as 新表名
alter table student as teacher
添加一个字段:alter table 表名 add 字段名 属性
alter table teacher add age int(11)
修改表的字段 类型:alter table 表名 modify 字段名 列属性
alter table teacher modify age varchar(10)
重名字:alter table 表名 change 旧名字 新名字
alter table teacher change age age1 int(1)
删除字段
alter table teacher drop age1
查看表
desc 表名