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

本文详细介绍了SQL数据库中字段类型的定义与使用,包括字符、二进制大数据、大文本、整型、浮点型、逻辑型、日期型等常见类型。同时,文章阐述了表的创建过程,涉及主键、自增长、唯一约束、非空约束等关键概念,并提供了实例演示。此外,文章还涵盖了如何查看表结构、删除与修改表的基本操作,以及如何在数据库中实现数据的高效管理和维护。
一.字段类型
字符: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);





评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值