CREATE TABLE
USE practice
CREATE TABLE IF NOT EXISTS district_tw(
district VARCHAR(50) NOT NULL
);
CREATE TABLE IF NOT EXISTS status_affect(
affect VARCHAR(10) NOT NULL
);
LOAD DATA LOCAL INFILE 'd:/webchain/district-taiwan.csv' INTO TABLE district_tw
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
LOAD DATA LOCAL INFILE 'd:/webchain/status-affect.csv' INTO TABLE status_affect
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
ALTER TABLE district_tw ADD id TINYINT NOT NULL AUTO_INCREMENT FIRST,
ADD PRIMARY KEY (id);
ALTER TABLE district_tw ADD UNIQUE KEY district (district);
ALTER TABLE status_affect ADD id TINYINT NOT NULL AUTO_INCREMENT FIRST,
ADD PRIMARY KEY (id),
ADD UNIQUE KEY affect (affect);
SHOW COLUMNS FROM district_tw;
+----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+----------------+ | id | tinyint(4) | NO | PRI | NULL | auto_increment | | district | varchar(50) | NO | UNI | NULL | | +----------+-------------+------+-----+---------+----------------+
SHOW CREATE TABLE district_tw\G
SHOW COLUMNS FROM status_affect;
+--------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+----------------+ | id | tinyint(4) | NO | PRI | NULL | auto_increment | | affect | varchar(10) | NO | UNI | NULL | | +--------+-------------+------+-----+---------+----------------+
SHOW CREATE TABLE status_affect\G
SELECT * FROM district_tw ORDER BY id;
+----+----------+ | id | district | +----+----------+ | 1 | 台北市 | | 2 | 新北市 | | 3 | 基隆市 | | 4 | 桃園縣 | | 5 | 新竹市 | | 6 | 新竹縣 | | 7 | 苗栗縣 | | 8 | 台中市 | | 9 | 彰化縣 | | 10 | 南投縣 | | 11 | 嘉義市 | | 12 | 嘉義縣 | | 13 | 雲林縣 | | 14 | 台南市 | | 15 | 高雄市 | | 16 | 屏東縣 | | 17 | 宜蘭縣 | | 18 | 花蓮縣 | | 19 | 台東縣 | | 20 | 澎湖縣 | | 21 | 金門縣 | | 22 | 連江縣 | +----+----------+
SELECT * FROM status_affect ORDER BY id;
+----+------------+ | id | affect | +----+------------+ | 1 | 單身 | | 2 | 已婚 | | 3 | 穩定交往中 | | 4 | 離婚 | +----+------------+
CREATE TABLE IF NOT EXISTS ac_basic (
surname VARCHAR(30) NOT NULL,
given VARCHAR(30) NOT NULL,
email VARCHAR(120) NOT NULL,
password VARBINARY(18) NOT NULL,
nick VARCHAR(30)
);
LOAD DATA LOCAL INFILE 'd:/webchain/account-basic.csv' INTO TABLE ac_basic
/* FIELDS TERMINATED BY '\t' */
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
ALTER TABLE ac_basic ADD district TINYINT,
ADD affect TINYINT;
ALTER TABLE ac_basic ADD id INT NOT NULL AUTO_INCREMENT FIRST,
ADD PRIMARY KEY (id);
ALTER TABLE ac_basic ADD UNIQUE KEY email (email),
ADD KEY password (password),
ADD KEY nick (nick);
ALTER TABLE ac_basic ADD CONSTRAINT district FOREIGN KEY (district) REFERENCES district_tw (id),
ADD CONSTRAINT affect FOREIGN KEY (affect) REFERENCES status_affect (id);
SHOW COLUMNS FROM ac_basic;
+----------+---------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | surname | varchar(30) | NO | | NULL | | | given | varchar(30) | NO | | NULL | | | email | varchar(120) | NO | UNI | NULL | | | password | varbinary(18) | NO | MUL | NULL | | | nick | varchar(30) | YES | MUL | NULL | | | district | tinyint(4) | YES | MUL | NULL | | | affect | tinyint(4) | YES | MUL | NULL | | +----------+---------------+------+-----+---------+----------------+
SHOW CREATE TABLE ac_basic\G
SELECT * FROM ac_basic ORDER BY id;
+----+---------+-------+-------------------------+----------+------+----------+--------+ | id | surname | given | email | password | nick | district | affect | +----+---------+-------+-------------------------+----------+------+----------+--------+ | 1 | 林 | 火旺 | peter.lin@yadoo.kom | 1111 | 阿旺 | NULL | NULL | | 2 | 張 | 火旺 | paul.zhang@goople.kom | 1111 | 旺旺 | NULL | NULL | | 3 | 陳 | 火旺 | bob.c@goople.kom | 1111 | 阿火 | NULL | NULL | | 4 | 李 | 火旺 | paul.li@alinana.kom | 1111 | 阿旺 | NULL | NULL | | 5 | 張 | 雅婷 | peggy.zhang@alinana.kom | 1111 | 婷婷 | NULL | NULL | | 6 | 吳 | 雅婷 | grace.wu@goople.kom | 1111 | 雅雅 | NULL | NULL | | 7 | 郭 | 雅婷 | nancy.guo@example.kom | 1111 | 小婷 | NULL | NULL | | 8 | 張 | 雅婷 | amy.zhang@yadoo.kom | 1111 | 婷婷 | NULL | NULL | | 9 | 吳 | 雅惠 | lucy.w@goople.kom | 1111 | 小惠 | NULL | NULL | | 10 | 楊 | 雅惠 | sara.yang@example.kom | 1111 | 阿惠 | NULL | NULL | | 11 | 楊 | 雅惠 | grace.yang@goople.kom | 1111 | 阿惠 | NULL | NULL | | 12 | 李 | 志豪 | jack.li@yadoo.kom | 1111 | 豪哥 | NULL | NULL | | 13 | 吳 | 志豪 | bob.wu@example.kom | 1111 | 大豪 | NULL | NULL | | 14 | 高 | 俊宏 | paul.gao@alinana.kom | 1111 | 阿宏 | NULL | NULL | | 15 | 方 | 俊宏 | jay.fang@goople.kom | 1111 | 俊俊 | NULL | NULL | | 16 | 黃 | 淑芬 | amy.h@alinana.kom | 1111 | 小芬 | NULL | NULL | | 17 | 張 | 淑芬 | peggy.zhang@yadoo.kom | 1111 | 淑芬 | NULL | NULL | | 18 | 黃 | 淑芬 | grace.huang@goople.kom | 1111 | 淑芬 | NULL | NULL | +----+---------+-------+-------------------------+----------+------+----------+--------+
CREATE TABLE ac_clone LIKE ac_basic;
#SHOW CREATE TABLE ac_clone;
#DROP TABLE IF EXISTS ac_clone
;
CREATE TABLE ac_clone SELECT * FROM ac_basic;
SELECT * FROM ac_basic;
SELECT * FROM ac_clone;
#DROP TABLE IF EXISTS ac_clone
;
CREATE TABLE ac_clone SELECT * FROM ac_basic WHERE id<6;
SELECT * FROM ac_basic;
SELECT * FROM ac_clone;
#DROP TABLE IF EXISTS ac_clone
ALTER TABLE tableName DROP fieldName1,
DROP fieldName2;
ALTER TABLE tableName MODIFY fieldName fieldDefinition;
ALTER TABLE tableName CHANGE oldName newName fieldDefinition;
DROP INDEX indexName ON tableName;
DROP INDEX `PRIMARY` ON tableName;
DROP TABLE IF EXISTS tableName;
TRUNCATE TABLE tableName;
有欄位被其它表參照 (FOREIGN KEY
) 的資料表,不能處理。
更新日期: