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

被折叠的 条评论
为什么被折叠?



