一、基本
show databases; --陈列数据库 create database 新库名 --创建数据库 drop database 数据库名 --删除数据库 use 数据库名; --使用数据库 show tables; --陈列数据表 create table 新表名(列名 数据类型, 列名 数据类型...); --创建数据表 drop table 表名 --删除表 select * from 表名 --列出表中所有数据
二、增删改查
以下以建一个通讯录为例,建表代码如下:
create table phone_book( ID int, Name varchar(20), Sex varchar(10), Number varchar(20), Birth datetime);
表的性质如下(可通过describe 表名得) +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | ID | int(11) | YES | | NULL | | | Name | varchar(20) | YES | | NULL | | | Sex | varchar(10) | YES | | NULL | | | Number | varchar(20) | YES | | NULL | | | Birth | datetime | YES | | NULL | | +--------+-------------+------+-----+---------+-------+
1.增
insert into 表名 values( 数据1,数据2,数据3,数据4 );
insert into phone_book values(1,'Tom','male','13534556889','1998-05-02'); insert into phone_book values(2,'Jenny','female','13534556889','1998-11-12'); insert into phone_book values(3,'Bob','male','13954556889','1997-01-12'); -- 插入三条记录 select * from phone_book; +------+-------+--------+-------------+---------------------+ | ID | Name | Sex | Number | Birth | +------+-------+--------+-------------+---------------------+ | 1 | Tom | male | 13534556889 | 1998-05-02 00:00:00 | | 2 | Jenny | female | 13534556889 | 1998-11-12 00:00:00 | | 3 | Bob | male | 13954556889 | 1997-01-12 00:00:00 | +------+-------+--------+-------------+---------------------+ 3 rows in set (0.00 sec)
2.删
delete from 表名 where 条件;
delete from phone_book where ID=1; --删掉ID为1的记录 select * from phone_book; +------+-------+--------+-------------+---------------------+ | ID | Name | Sex | Number | Birth | +------+-------+--------+-------------+---------------------+ | 2 | Jenny | female | 13534556889 | 1998-11-12 00:00:00 | | 3 | Bob | male | 13954556889 | 1997-01-12 00:00:00 | +------+-------+--------+-------------+---------------------+ 2 rows in set (0.00 sec)
3.改
update 表名 set 列x表头 = 要改的x列数据 条件
update phone_book set Number = '10086' where ID =2; --将2号的电话号码改成10086 select * from phone_book; +------+-------+--------+-------------+---------------------+ | ID | Name | Sex | Number | Birth | +------+-------+--------+-------------+---------------------+ | 2 | Jenny | female | 10086 | 1998-11-12 00:00:00 | | 3 | Bob | male | 13954556889 | 1997-01-12 00:00:00 | +------+-------+--------+-------------+---------------------+ 2 rows in set (0.00 sec)
4.查
select * from 表名 条件
select * from phone_book where Sex = 'female'; --找出性别为女的记录 +------+-------+--------+--------+---------------------+ | ID | Name | Sex | Number | Birth | +------+-------+--------+--------+---------------------+ | 2 | Jenny | female | 10086 | 1998-11-12 00:00:00 | +------+-------+--------+--------+---------------------+ 1 row in set (0.00 sec)
三、建表约束
1.主键约束
确保一张表中某列元素不重复且不为空(插入重复值或空值会出错)
关键字:primary key
实例1(关键字加在列名的数据类型后面): create table Test1(ID int primary key, Name varchar(20)); describe Test1; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | ID | int(11) | NO | PRI | NULL | | | Name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) 实例2(写完列和数据类型后,以关键字加圆括号方式表明约束) create table Test2(ID int,Name varchar(20), primary key(ID)); (如果要在圆括号里,以逗号隔开写上Name,就是联合主键,相当于只有一个重复值时没问题,但两个同时重复就不能插入)
2.自增约束
当该列元素插入值为空时,会自增。
关键字:primary key auto_increment
create table Test3(ID int primary key auto_increment, Name varchar(20)); insert into Test3(Name) values('Jack'); insert into Test3(Name) values('Bob'); select * from Test3; +----+------+ | ID | Name | +----+------+ | 1 | Jack | | 2 | Bob | +----+------+ 2 rows in set (0.00 sec)
3.外键约束
当副表中某列的元素都是来自主表中的某列时,可添加此约束,添加约束后,该列的值只能来自主表,不能插入主表没有的值。以下以班级表和学生表为例
关键字:foreign key
create table Classes(ID int primary key auto_increment,Name varchar(20)); 插入四条记录后,班级表如下,有四个班Magic、Design、PhotoShop、Actor +----+-----------+ | ID | Name | +----+-----------+ | 1 | Magic | | 2 | Design | | 3 | PhotoShop | | 4 | Actor | +----+-----------+ 学生表建立时,班别这一列使用外键约束 create table Students(Stu_ID varchar(20) primary key,Name varchar(20),Class_ID int,foreign key(Class_ID) references Classes(ID)); +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | Stu_ID | varchar(20) | NO | PRI | NULL | | | Name | varchar(20) | YES | | NULL | | | Class_ID | int(11) | YES | MUL | NULL | | +----------+-------------+------+-----+---------+-------+ 此时,可以插入 insert into Students values('2017052432','Jim',1); 但不可以 insert into Students values('2017052450','Jenny',10); 因为主表(班级没有10)
4.唯一约束
约束修饰的字段的值不可重复(可为空)
关键字:unique
create table Test4(ID int, name varchar(20),unique(ID)); desc Test4; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | ID | int(11) | YES | UNI | NULL | | | name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+
5.非空约束
插入值不能为空
关键字:not null
create table Test5(ID int not null, name varchar(20)); desc Test5; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | ID | int(11) | NO | | NULL | | | name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+
6.默认约束
如果插入时没传值,则使用默认值
关键字:default
create table Test6( ID int, Name varchar(20), Age int default 20); insert into Test6 values(2,'Ben',22); insert into Test6(ID,Name) values(1,'Tony'); select * from Test6; +------+------+------+ | ID | Name | Age | +------+------+------+ | 2 | Ben | 22 | | 1 | Tony | 20 | +------+------+------+ 2 rows in set (0.00 sec)
7.添加约束、删除约束
添加约束三种方法:
①建表时添加
②alter table 表名 modify 列名 数据类型 约束
③alter table 表名 add 约束(列名)
① 如前文所述 ② 先建表,再添加约束: create table Test7(ID int,Name varchar(20)); alter table Test7 modify ID int primary key; ③ alter table Test7 add unique(Name);
删除约束:
alter table 表名 drop index 列名
alter table Test7 drop index Name;
四、数据表设计
第一范式: 字段拆分到不可拆分
第二三范式:在满足第一范式的前提下,除主键外的每一列都必须依赖全部主键,无传递依赖(感觉就是一张表里每列的信息应该不能推断出其它列的信息,否则就拆分成多张表)