创建数据库
CREATE DATABASE Market;
使用数据库;
USE Market;
创建表customers
CREATE TABLE customers(
c_num INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
c_name VARCHAR(50),
c_contact VARCHAR(50),
c_city VARCHAR(50) ,
c_birth DATETIME NOT NULL
);
#将c_contact字段插入到c_birth后面
ALTER TABLE customers MODIFY c_contact VARCHAR(50) AFTER c_birth;
DESC customers;
#将c_name字段类型改为varchar(70)
ALTER TABLE customers MODIFY c_name VARCHAR(70);
#将c_contact改名为c_phone
ALTER TABLE customers CHANGE c_contact c_phone VARCHAR(50);
#增加c_gander char(1) 字段
ALTER TABLE customers ADD c_gander CHAR(1);
将表名修改为customers_info
ALTER TABLE customers RENAME customers_info;
#删除字段c_city
ALTER TABLE customers_info DROP c_city;
DESC customers_info;
#修改表的引擎
ALTER TABLE customers_info engine=Myisam;
创建表orders;
在创建外键的时候必须要修改表customers的引擎,因为第一张表要求将引擎改为MYISAM,我们设置外键需要将它改为Innodb.
CREATE TABLE orders(
o_num INT(11) PRIMARY KEY NOT NULL UNIQUE AUTO_INCREMENT,
o_date DATE,
o_id INT(11) NOT NULL,
CONSTRAINT customers_info_fk FOREIGN KEY(o_id) REFERENCES customers_info(c_num)
);