目录
1、创建数据库campany,在库中创建两个表offices和employess表
2、查看该库下几个表以及查看两张表结构
3、将表employees的mobile字段修改到officeCode字段后面
4、将表emplouees的birth字段改名为employee_birth
5、修改sex字段,数据类型为CHAR(1),非空约束
6、/*删除字段note*/
7、增加字段名favoriate_activity,数据类型为VARCHAR(100)
8、删除表offices
9、将表employees名称改为employess_info
/*创建company数据库*/
CREATE DATABASE company;
/*创建offfices表*/
CREATE TABLE offices(
officeCode INT(10) PRIMARY KEY NOT NULL UNIQUE,
city VARCHAR(50) NOT NULL,
address VARCHAR(50),
country VARCHAR(50) NOT NULL,
postalCode VARCHAR(15) UNIQUE
);ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*创建employees表*/
CREATE TABLE employees(
employeeNumeber INT(11) PRIMARY KEY NOT NULL UNIQUE AUTO_INCREMENT,
lastName VARCHAR(50) NOT NULL,
firstName VARCHAR(50) NOT NULL,
mobile VARCHAR(25) UNIQUE,
officeCode INT(10) NOT NULL,
jobTitle VARCHAR(50) NOT NULL,
birth DATETIME NOT NULL,
note VARCHAR(255),
sex VARCHAR(5)
);ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*将empioyees表中的officeCode设置位外键*/
ALTER TABLE employees ADD CONSTRAINT officeCode_pid FOREIGN KEY(officeCode) REFERENCES offices(officeCode);
执行结果:
/*查看company数据库下的表*/
SHOW TABLES FROM company;
/*查看company数据库的表结构*/
DESC offices;
DESC employees;
执行结果:
/*将表employees的mobile字段修改到officeCode字段后面*/
ALTER TABLE employees MODIFY mobile VARCHAR(25) AFTER officeCode;
执行结果:
/*将表emplouees的birth字段改名为employee_birth*/
ALTER TABLE employees CHANGE birth employee_birth DATETIME;
执行结果:
/*修改sex字段,数据类型为CHAR(1),非空约束*/
ALTER TABLE employees MODIFY sex CHAR(1) NOT NULL;
执行结果:
/*删除字段note*/
ALTER TABLE employees DROP note;
执行结果:
/*增加字段名favoriate_activity,数据类型为VARCHAR(100)*/;
ALTER TABLE employees ADD favoriate_activity VARCHAR(100);
执行结果:
/*删除表offices*/
ALTER TABLE employees DROP FOREIGN KEY officeCode_pid; #将外键删除,否则无法删除此表#
DROP TABLE offices;
执行结果:
/*将表employees名称改为employess_info*/
RENAME TABLE employees TO employees_info;
执行结果: