MySql:操作表的语句以及常用的字段类型

一.字段类型
字符:VARCHAR(12)
二级制大数据:VLOB
大文本:TEXT
整形:TINYINT,SMALLINT,INT,BIGINT
浮点型:FLOAT,DOUBLE
逻辑型:BIT
日期型:DATE,TIME,DATETIME,TIMESTAMP

二.表的创建
示例创建一个员工表employee:
create table employee(
id int,
name varchar(20),
gender bit,
birthday date,
entry_date date,
job varchar(40),
salary double,
resume text
);


主键约束:在创建表的时候在字段后写上primary key则为主键,主键不能重复也不能为空;
primary key   
例子:
create table employee(
id int primary key   ,
name varchar(20),
gender bit,
birthday date,
entry_date date,
job varchar(40),
salary double,
resume text
);

自增长: 由于主键不能为空不能重复,所以在插入数据的时候为了保证满足以上条件我们可以把主键设置为自增长
auto_increment;
在创建表的字段后加上这句话则该字段会自增长。
例子:
create table employee(
id int  primary key  auto_increment,
name varchar(20),
gender bit,
birthday date,
entry_date date,
job varchar(40),
salary double,
resume text
);

唯一约束:使得字段不能重复
unique
在创建表的时候,在字段后加上unique.那这个字段就不能为重复的了。
非空约束:使得字段不能为空
not null
在创建表的时候,在字段后加上 not null,那么这个字段就不能为空的了。

例子:
create table employee(
id int  primary key  auto_increment,
name varchar(20) not null,
gender bit,
birthday date,
entry_date date,
job varchar(40),
salary double unique,
resume text 
);

三.查看表的结构
desc [表名]

四.删除表
drop table [表名];

五.修改表
增加一个字段:
alter table employee add image blob;

修改 job varchar(40)的长度为45:
alter table employee modify job varchar(45);

删除一个字段:
alter table employee drop gender;

修改表名:
修改表名employee为employee2
rename table employee to employee2;

修改表的字符集:
alter table employee2 character set gbk;


修改字段名或类型:
alter table employee2  change name jobb varchar(50);





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值