首页 > 代码库 > MySQL5.5.21学习教程之二

MySQL5.5.21学习教程之二

     现在来学习一下关于表的基本操作!主要是建立表和基本的约束,之后会继续讲解介绍索引的问题!


#列--也称为属性列,在具体创建表的时候,必须指定列的名字和数据类型
#索引--是指根据指定的数据库列表列建立起来的顺序,提供了快速访问数据的途径
#------可监督表的数据,使其索引所指向的列中的数据不重复
#触发器--是指用户定义的命令的集合,当对一个表中的数据进行插入,更新或者删除时这组命令就会自动
#--------自动执行,可以用来确保数据的完整性和安全性

create database company;
show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| company            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
use company;
create table t_dept(deptno INTEGER,dname VARCHAR(20),loc VARCHAR(40));
describe t_dept;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| deptno | int(11)     | YES  |     | NULL    |       |
| dname  | varchar(20) | YES  |     | NULL    |       |
| loc    | varchar(40) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+

show create table t_dept \G
*************************** 1. row ***************************
       Table: t_dept
Create Table: CREATE TABLE `t_dept` (
  `deptno` int(11) DEFAULT NULL,
  `dname` varchar(20) DEFAULT NULL,
  `loc` varchar(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8

drop table t_dept;
show tables;
Empty set (0.00 sec)

alter table t_dept rename tab_dept;
show tables;
+-------------------+
| Tables_in_company |
+-------------------+
| tab_dept          |
+-------------------+

alter table tab_dept rename t_dept;
desc t_dept;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| deptno | int(11)     | YES  |     | NULL    |       |
| dname  | varchar(20) | YES  |     | NULL    |       |
| loc    | varchar(40) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+

alter table t_dept add descri VARCHAR(20);
#在表的首部添加一个字段
#alter table t_dept add descri VARCHAR(20) first;
#在表的某个字段后面添加一个字段
#alter table t_dept add descri VARCHAR(20) after deptno;

desc t_dept;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| deptno | int(11)     | YES  |     | NULL    |       |
| dname  | varchar(20) | YES  |     | NULL    |       |
| loc    | varchar(40) | YES  |     | NULL    |       |
| descri | varchar(20) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+


alter table t_dept drop descri;
desc t_dept;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| deptno | int(11)     | YES  |     | NULL    |       |
| dname  | varchar(20) | YES  |     | NULL    |       |
| loc    | varchar(40) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+


alter table t_dept modify deptno VARCHAR(20);
desc t_dept;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| deptno | varchar(20) | YES  |     | NULL    |       |
| dname  | varchar(20) | YES  |     | NULL    |       |
| loc    | varchar(40) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+

alter table t_dept modify deptno INTEGER;
desc t_dept;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| deptno | int(11)     | YES  |     | NULL    |       |
| dname  | varchar(20) | YES  |     | NULL    |       |
| loc    | varchar(40) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+


#alter table table_name change 旧属性名 新属性名 旧数据类型
#alter table table_name change 旧属性名 新属性名 新数据类型
alter table t_dept change loc location VARCHAR(40);
desc t_dept;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| deptno   | int(11)     | YES  |     | NULL    |       |
| dname    | varchar(20) | YES  |     | NULL    |       |
| location | varchar(40) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+


alter table t_dept modify location VARCHAR(40) first;
desc t_dept;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| location | varchar(40) | YES  |     | NULL    |       |
| deptno   | int(11)     | YES  |     | NULL    |       |
| dname    | varchar(20) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+


MySQL软件支持的完整性约束
NOT NULL--设置约束字段不能为空
DEFAULT--设置字段的默认值
UNIQUE KEY--约束字段的值唯一
PRIMARY KEY--约束字段为表的主键,可以作为该表记录的唯一约束
AUTO_INCREMENT--约束字段的值为自动增加
FOREIGN KEY--约束字段为表的外键

alter table t_dept modify deptno INTEGER NOT NULL;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| location | varchar(40) | YES  |     | NULL    |       |
| deptno   | int(11)     | NO   |     | NULL    |       |
| dname    | varchar(20) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+

alter table t_dept modify location VARCHAR(40) default 'NWPU';
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| location | varchar(40) | YES  |     | NWPU    |       |
| deptno   | int(11)     | NO   |     | NULL    |       |
| dname    | varchar(20) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+

alter table t_dept modify dname VARCHAR(20) unique;
desc t_dept;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| location | varchar(40) | YES  |     | NWPU    |       |
| deptno   | int(11)     | NO   |     | NULL    |       |
| dname    | varchar(20) | YES  | UNI | NULL    |       |
+----------+-------------+------+-----+---------+-------+


drop table t_dept;
show tables;


如果想给字段dname上的UK约束设置一个名字,可以执行SQL语句constraint
下面是创建表t_dept的语句:
create table t_dept(
deptno INTEGER,
dname VARCHAR(20),
loc VARCHAR(40),
constraint uk_dname unique(dname)
);

在具体的设置主键约束时,必须满足主键字段的值是唯一的、非空的。
由于主键可以是单一字段,也可以是多个字段,因此分为单字段主键和多字段主键
create table t_dept(
deptno INTEGER primary key,
dname VARCHAR(20),
loc VARCHAR(40),
constraint uk_dname unique(dname)
);
设置多字段主键
create table t_dept(
deptno INTEGER,
dname VARCHAR(20),
loc VARCHAR(40),
constraint uk_dname unique(dname),
constraint pk_dname_depno primary key(deptno,dname)
);
show tables;
desc t_dept;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| deptno | int(11)     | NO   | PRI | 0       |       |
| dname  | varchar(20) | NO   | PRI |         |       |
| loc    | varchar(40) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+


drop table t_dept;

设置字段值自动增加
create table t_dept(
deptno INTEGER auto_increment,
dname VARCHAR(20),
loc VARCHAR(40),
constraint uk_dname unique(dname),
constraint pk_dname_depno primary key(deptno,dname)
);
desc t_dept;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| deptno | int(11)     | NO   | PRI | NULL    | auto_increment |
| dname  | varchar(20) | NO   | PRI |         |                |
| loc    | varchar(40) | YES  |     | NULL    |                |
+--------+-------------+------+-----+---------+----------------+

设置外键约束通常能表示多个表之间的参照性的完整性约束,即构建于两个
表的两个字段之间的参照关系
设置外键约束的两个表之间会具有父子关系,即子表中某个字段的取值范围由
父表决定,表示一种部门和雇员关系,即每个部分有多少雇员。
首先应该有两个表:部门表和雇员表,雇员表中有一个字段表示部门编号的字段deptno
其依赖于部门表的主键,这样字段deptno就是雇员表的外键,通过该字段部门编号的字段deptno
其依赖于部门表的主键,这样字段deptno就是雇员表的外键。


create table table_name (
属性名 数据类型,
属性名 数据类型,
......
constraint 外键约束名 foreign key (属性名1)
	references 表明(属性名2)
);

create table t_employee(
empno INTEGER primary key,
ename VARCHAR(20),
job VARCHAR(20),
MGR INTEGER,
Hiredate date,
sal double(10,2),
comm double(10,2),
deptno INTEGER,
constraint fk_deptno foreign key(deptno) references t_dept(deptno)
);
show tables;
+-------------------+
| Tables_in_company |
+-------------------+
| t_dept            |
| t_employee        |
+-------------------+
desc t_employee;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| empno    | int(11)      | NO   | PRI | NULL    |       |
| ename    | varchar(20)  | YES  |     | NULL    |       |
| job      | varchar(20)  | YES  |     | NULL    |       |
| MGR      | int(11)      | YES  |     | NULL    |       |
| Hiredate | date         | YES  |     | NULL    |       |
| sal      | double(10,2) | YES  |     | NULL    |       |
| comm     | double(10,2) | YES  |     | NULL    |       |
| deptno   | int(11)      | YES  | MUL | NULL    |       |
+----------+--------------+------+-----+---------+-------+

MySQL5.5.21学习教程之二