本章使用的数据库如下:
employee(employeeID, name, job, departmentID )
department(departmentID, name)
employeeSkills(employeeID, skill)
client(clientID, name, address, contactPerson, contactNumber)
assignment(clientID, employeeID , workdate, hours)
Mysql不区分大小写,所有数据库系统中对SQL的关键字不区分大小写
实例:
drop database if exists employee;create database employee; use employee; create table department( departmentID int not null auto_increment primary key, name varchar(30)) type=InnoDB; create table employee( employeeID int not null auto_increment primary key, name varchar(80), job varchar(30), departmentID int not null references department(departmentID)) type=InnoDB; create table employeeSkills( employeeID int not null references employee(employeeID), skill varchar(15) not null, primary key (employeeID, skill)) type=InnoDB; create table client( clientID int not null auto_increment primary key, name varchar(40), address varchar(100), contactPerson varchar(80), contactNumber char(12)) type=InnoDB; create table assignment( clientID int not null references client(clientID), employeeID int not null references employee(employeeID), workdate date not null, hours float, primary key (clientID, employeeID, workdate)) type=InnoDB;
Varchar使用的是动态存储,
默认使用的是MyISAM表类型,单个列作为主键可以如下表示:
employeeID int not null auto_increment primary key,
MyISAM 或许要5.1版本才支持外键.
Create 的语法(略)
有3种基本类型:数字类型,字符串类型,时间日期类型.
数字类型
TINYINT 8位
SMALLINT 16
MEDIUMINT 32
BIGINT 64
FLOAT
DOUBLE
字符串类型
CHAR VARCHAR
BLOB用二进制形式存储,区分大小写.
TINYTEXT or TINYBLOB 8位
TEXT or BLOB 16
MEDIUMTEXT or MEDUIMBLOB 24
LONGTEXT or LONGBLOB 32
ENUM
SET
时间日期类型
DATE
TIME
DATETIME
TIMESTAMP
YEAR
PRIMARY KEY, KEY, UNIQUE, or INDEX 都可索引有关
创建索引:
create index name on employee(name);
选项有UNIQUE FULLTEXT 等.针对VARCHAR可以只提取前面的几个字母.
删除操作. 文本索引往往没有数字的速度快.
DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name,...]drop index part_name on employee;
修改表结构
alter table employeeadd index name (name);
详细的说明略