一些SQL操作语句(4),使用ALTER TABLE操作命令

返回
复写TABLE CREATE TABLE a002336 LIKE a002335; INSERT INTO a002336 SELECT * FROM a002335; mysql> CREATE TABLE test_result -> ( -> id INT UNSIGNED NOT NULL, -> test_num INT NOT NULL, -> date DATE, -> result INT -> ); Query OK, 0 rows affected (0.16 sec) mysql> INSERT INTO test_result (id,test_num,date,result) -> SELECT id,1,date1,result1 FROM test_subject WHERE result1 IS NOT NULL; Query OK, 3 rows affected (0.08 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> INSERT INTO test_result (id,test_num,date,result) -> SELECT id,2,date2,result2 FROM test_subject WHERE result2 IS NOT NULL; Query OK, 3 rows affected (0.03 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM test_result; +----+----------+------------+--------+ | id | test_num | date | result | +----+----------+------------+--------+ | 1 | 1 | 2001-07-13 | 78 | | 2 | 1 | 2001-07-12 | 79 | | 3 | 1 | 2001-07-16 | 82 | | 1 | 2 | 2001-07-14 | 85 | | 2 | 2 | 2001-07-14 | 82 | | 3 | 2 | 2001-07-18 | 95 | +----+----------+------------+--------+ 6 rows in set (0.00 sec) mysql> ALTER TABLE test_result ADD PRIMARY KEY (id, test_num); Query OK, 6 rows affected (0.47 sec) Records: 6 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM test_result; +----+----------+------------+--------+ | id | test_num | date | result | +----+----------+------------+--------+ | 1 | 1 | 2001-07-13 | 78 | | 1 | 2 | 2001-07-14 | 85 | | 2 | 1 | 2001-07-12 | 79 | | 2 | 2 | 2001-07-14 | 82 | | 3 | 1 | 2001-07-16 | 82 | | 3 | 2 | 2001-07-18 | 95 | +----+----------+------------+--------+ 6 rows in set (0.09 sec) mysql> ALTER TABLE test_subject DROP date1, DROP result1, DROP date2, DROP resul t2; Query OK, 3 rows affected (0.27 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> SELECT test_result.id, test_subject.name, AVG(test_result.result) -> FROM test_subject, test_result -> WHERE test_subject.id = test_result.id -> GROUP BY test_result.id; +----+--------+-------------------------+ | id | name | AVG(test_result.result) | +----+--------+-------------------------+ | 1 | Fred | 81.5000 | | 2 | Barry | 80.5000 | | 3 | Portia | 88.5000 | +----+--------+-------------------------+ 3 rows in set (0.03 sec) mysql> CREATE TABLE test_subject -> ( -> id INT UNSIGNED NOT NULL AUTO_INCREMENT, -> name CHAR(20) NOT NULL, -> date1 DATE, -> result1 INT, -> date2 DATE, -> result2 INT, -> PRIMARY KEY (id) -> ); Query OK, 0 rows affected (0.48 sec) mysql> INSERT TABLE test_subject (name,date1,result1,date2,result2) -> VALUES("Fred","2001-07-13",78,"2001-07-14",85); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TABLE test_subject (name,date1,result1,date2,result2) VALUES("Fred","2001-07-13' at line 1 mysql> INSERT INTO test_subject (name,date1,result1,date2,result2) -> VALUES("Fred","2001-07-13",78,"2001-07-14",85); Query OK, 1 row affected (0.05 sec) mysql> INSERT TABLE test_subject (name,date1,result1,date2,result2) -> VALUES("Barry","2001-07-12",79,"2001-07-14",82); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TABLE test_subject (name,date1,result1,date2,result2) VALUES("Barry","2001-07-1' at line 1 mysql> INSERT INTO test_subject (name,date1,result1,date2,result2) -> VALUES("Barry","2001-07-12",79,"2001-07-14",82); Query OK, 1 row affected (0.03 sec) mysql> INSERT INTO test_subject (name,date1,result1,date2,result2) -> VALUES("Portia","2001-07-16",82,"2001-07-18",95); Query OK, 1 row affected (0.01 sec) mysql> SELECT * FROM test_subject; +----+--------+------------+---------+------------+---------+ | id | name | date1 | result1 | date2 | result2 | +----+--------+------------+---------+------------+---------+ | 1 | Fred | 2001-07-13 | 78 | 2001-07-14 | 85 | | 2 | Barry | 2001-07-12 | 79 | 2001-07-14 | 82 | | 3 | Portia | 2001-07-16 | 82 | 2001-07-18 | 95 | +----+--------+------------+---------+------------+---------+ 3 rows in set (0.02 sec) mysql> SELECT id, name, (result1 + result2) / 2 FROM test_subject; +----+--------+-------------------------+ | id | name | (result1 + result2) / 2 | +----+--------+-------------------------+ | 1 | Fred | 81.5000 | | 2 | Barry | 80.5000 | | 3 | Portia | 88.5000 | +----+--------+-------------------------+ 3 rows in set (0.02 sec)
mysql> use cookbook Database changed mysql> CREATE TAbLE client_billing -> ( -> id INT UNSIGNED NOT NULL, -> name CHAR(20) NOT NULL, -> address CHAR(20) NOT NULL, -> date DATE NOT NULL, -> minutes INT NOT NULL, -> description CHAR(60) NOT NULL -> ); Query OK, 0 rows affected (0.41 sec) mysql> INSERT client_billing (id,name,address,date,minutes) VALUES(21,John,"46 N orth Ave.","2001-07-15",48,"consult by phone"); ERROR 1136 (21S01): Column count doesn't match value count at row 1 mysql> INSERT INTO client_billing (id,name,address,date,minutes) VALUES(21,John, "46 North Ave.","2001-07-15",48,"consult by phone"); ERROR 1136 (21S01): Column count doesn't match value count at row 1 mysql> INSERT INTO client_billing (id,name,address,date,minutes,description) VAL UES(21,John,"46 North Ave.","2001-07-15",48,"consult by phone"); ERROR 1054 (42S22): Unknown column 'John' in 'field list' mysql> INSERT INTO client_billing (id,name,address,date,minutes,description) VAL UES(21,"John","46 North Ave.","2001-07-15",48,"consult by phone"); Query OK, 1 row affected (0.05 sec) mysql> INSERT INTO client_billing (id,name,address,date,minutes,description) VAL UES(21,"John","46 North Ave.","2001-07-19",120,"court appearance"); Query OK, 1 row affected (0.03 sec) mysql> INSERT INTO client_billing (id,name,address,date,minutes,description) VAL UES(43,"Toby","123 Elm St.","2001-07-13",12,"office visit"); Query OK, 1 row affected (0.05 sec) mysql> INSERT INTO client_billing (id,name,address,date,minutes,description) VAL UES(43,"Toby","123 Elm St.","2001-07-14",60,"draft proposal"); Query OK, 1 row affected (0.05 sec) mysql> INSERT INTO client_billing (id,name,address,date,minutes,description) VAL UES(43,"Toby","123 Elm St.","2001-07-16",180,"present proposal"); Query OK, 1 row affected (0.03 sec) mysql> SELECT * FROM client_billing\G *************************** 1. row *************************** id: 21 name: John address: 46 North Ave. date: 2001-07-15 minutes: 48 description: consult by phone *************************** 2. row *************************** id: 21 name: John address: 46 North Ave. date: 2001-07-19 minutes: 120 description: court appearance *************************** 3. row *************************** id: 43 name: Toby address: 123 Elm St. date: 2001-07-13 minutes: 12 description: office visit *************************** 4. row *************************** id: 43 name: Toby address: 123 Elm St. date: 2001-07-14 minutes: 60 description: draft proposal *************************** 5. row *************************** id: 43 name: Toby address: 123 Elm St. date: 2001-07-16 minutes: 180 description: present proposal 5 rows in set (0.00 sec) mysql> SELECT * FROM client_billing; +----+------+---------------+------------+---------+------------------+ | id | name | address | date | minutes | description | +----+------+---------------+------------+---------+------------------+ | 21 | John | 46 North Ave. | 2001-07-15 | 48 | consult by phone | | 21 | John | 46 North Ave. | 2001-07-19 | 120 | court appearance | | 43 | Toby | 123 Elm St. | 2001-07-13 | 12 | office visit | | 43 | Toby | 123 Elm St. | 2001-07-14 | 60 | draft proposal | | 43 | Toby | 123 Elm St. | 2001-07-16 | 180 | present proposal | +----+------+---------------+------------+---------+------------------+ 5 rows in set (0.00 sec) mysql> CREATE TABLE client_info -> ( -> id INT UNSIGNED NOT NULL, -> name CHAR(20) NOT NULL, -> address CHAR(20) NOT NULL -> ); Query OK, 0 rows affected (0.08 sec) mysql> CREATE TABLE bill_item -> ( -> id INT UNSIGNED NOT NULL, -> date DATE NOT NULL, -> minutes INT NOT NULL, -> description CHAR(60) NOT NULL -> ); Query OK, 0 rows affected (0.08 sec) mysql> INSERT INTO client_info (id,name,address) -> SELECT id,name,address FROM client_billing; Query OK, 5 rows affected (0.06 sec) Records: 5 Duplicates: 0 Warnings: 0 mysql> INSERT INTO bill_item (id,date,minutes,description) -> SELECT id,date,minutes,descriptin FROM client_billing; ERROR 1054 (42S22): Unknown column 'descriptin' in 'field list' mysql> SELECT id,date,minutes,description FROM client_billing; +----+------------+---------+------------------+ | id | date | minutes | description | +----+------------+---------+------------------+ | 21 | 2001-07-15 | 48 | consult by phone | | 21 | 2001-07-19 | 120 | court appearance | | 43 | 2001-07-13 | 12 | office visit | | 43 | 2001-07-14 | 60 | draft proposal | | 43 | 2001-07-16 | 180 | present proposal | +----+------------+---------+------------------+ 5 rows in set (0.00 sec) mysql> INSERT INTO bill_item (id,date,minutes,description) -> SELECT id,date,minutes,description FROM client_billing; Query OK, 5 rows affected (0.06 sec) Records: 5 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM client_info; +----+------+---------------+ | id | name | address | +----+------+---------------+ | 21 | John | 46 North Ave. | | 43 | Toby | 123 Elm St. | +----+------+---------------+ 2 rows in set (0.00 sec) mysql> SELECT * FROM bill_item; +----+------------+---------+------------------+ | id | date | minutes | description | +----+------------+---------+------------------+ | 21 | 2001-07-15 | 48 | consult by phone | | 21 | 2001-07-19 | 120 | court appearance | | 43 | 2001-07-13 | 12 | office visit | | 43 | 2001-07-14 | 60 | draft proposal | | 43 | 2001-07-16 | 180 | present proposal | +----+------------+---------+------------------+ 5 rows in set (0.00 sec) mysql> DROP TABLE client_billing; Query OK, 0 rows affected (0.03 sec) mysql> SELECT client_info.id, client_info.name, client_info.address, -> SUM(bill_item.minutes) AS 'total minutes' -> FROM client_info, bill_item -> WHERE client_info.id = bill_item.id -> GROUP BY client_info.id; +----+------+---------------+---------------+ | id | name | address | total minutes | +----+------+---------------+---------------+ | 21 | John | 46 North Ave. | 168 | | 43 | Toby | 123 Elm St. | 252 | +----+------+---------------+---------------+ 2 rows in set (0.01 sec)
删除重复的项目 mysql> INSERT INTO mytbl (i,c) VALUES(1,'a'),(1,'a'),(1,NULL),(1,NULL), -> (2,'a'),(2,'a'),(2,'b'),(2,'b'); Query OK, 8 rows affected (0.13 sec) Records: 8 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM mytbl; +------+------+ | i | c | +------+------+ | 1 | a | | 1 | a | | 1 | NULL | | 1 | NULL | | 2 | a | | 2 | a | | 2 | b | | 2 | b | +------+------+ 8 rows in set (0.02 sec) mysql> ALTER IGNORE TABLE mytbl ADD UNIQUE(i,c); Query OK, 8 rows affected (0.45 sec) Records: 8 Duplicates: 3 Warnings: 0 mysql> SELECT * FROM mytbl; +------+------+ | i | c | +------+------+ | 1 | NULL | | 1 | NULL | | 1 | a | | 2 | a | | 2 | b | +------+------+ 5 rows in set (0.02 sec) Adding or Dropping Indexes mysql> SHOW INDEX FROM quo\G *************************** 1. row *************************** Table: quo Non_unique: 0 Key_name: PRIMARY Seq_in_index: 1 Column_name: id Collation: A Cardinality: 23 Sub_part: NULL Packed: NULL Null: Index_type: BTREE Comment: 1 row in set (0.00 sec) mysql> ALTER TABLE quo DROP PRIMARY KEY; ERROR 1075 (42000): Incorrect table definition; there can be only one auto colum n and it must be defined as a key mysql> CREATE TABLE mytbl -> ( -> i INT, -> c CHAR(1) -> ); Query OK, 0 rows affected (0.22 sec) mysql> SHOW INDEX FROM mytbl; Empty set (0.00 sec) mysql> ALTER TABLE mytbl ADD INDEX (c); Query OK, 0 rows affected (0.33 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> SHOW INDEX FROM mytbl; +-------+------------+----------+--------------+-------------+-----------+------ -------+----------+--------+------+------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardi nality | Sub_part | Packed | Null | Index_type | Comment | +-------+------------+----------+--------------+-------------+-----------+------ -------+----------+--------+------+------------+---------+ | mytbl | 1 | c | 1 | c | A | 0 | NULL | NULL | YES | BTREE | | +-------+------------+----------+--------------+-------------+-----------+------ -------+----------+--------+------+------------+---------+ 1 row in set (0.00 sec) mysql> SHOW INDEX FROM mytbl\G *************************** 1. row *************************** Table: mytbl Non_unique: 1 Key_name: c Seq_in_index: 1 Column_name: c Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment: 1 row in set (0.00 sec) mysql> ALTER TABLE mytbl ADD INDEX (c,i); Query OK, 0 rows affected (0.19 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> SHOW INDEX FROM mytbl\G *************************** 1. row *************************** Table: mytbl Non_unique: 1 Key_name: c Seq_in_index: 1 Column_name: c Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment: *************************** 2. row *************************** Table: mytbl Non_unique: 1 Key_name: c_2 Seq_in_index: 1 Column_name: c Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment: *************************** 3. row *************************** Table: mytbl Non_unique: 1 Key_name: c_2 Seq_in_index: 2 Column_name: i Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment: 3 rows in set (0.00 sec) mysql> ALTER TABLE mytbl DROP PRIMARY KEY; ERROR 1091 (42000): Can't DROP 'PRIMARY'; check that column/key exists mysql> ALTER TABLE mytbl DROP INDEX c, DROP INDEX c_2; Query OK, 0 rows affected (0.19 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> SHOW INDEX FROM mytbl\G Empty set (0.00 sec) mysql> Table Type mysql> SHOW TABLE STATUS LIKE 'quo1'\G *************************** 1. row *************************** Name: quo1 Engine: InnoDB Version: 10 Row_format: Compact Rows: 23 Avg_row_length: 712 Data_length: 16384 Max_data_length: 0 Index_length: 0 Data_free: 0 Auto_increment: 24 Create_time: 2008-01-22 16:33:30 Update_time: NULL Check_time: NULL Collation: utf8_general_ci Checksum: NULL Create_options: Comment: InnoDB free: 147456 kB 1 row in set (1.23 sec) 给table改名 mysql> ALTER TABLE quo1 RENAME TO quo; Query OK, 0 rows affected (0.13 sec) 改变列的缺省值(Changing a Column's Default Value) mysql> show columns from quo1; +-----------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+---------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | time | date | YES | | NULL | | | makerref | text | YES | | NULL | | | money | int(11) | YES | | NULL | | | discount0 | int(11) | YES | | NULL | | | discount | text | YES | | NULL | | | price | text | YES | | NULL | | | total | text | YES | | NULL | | | test | text | YES | | NULL | | | memo | text | YES | | NULL | | +-----------+---------+------+-----+---------+----------------+ 10 rows in set (0.41 sec) mysql> select id,discount0 from quo1; +----+-----------+ | id | discount0 | +----+-----------+ | 1 | 100 | | 2 | 100 | | 3 | 100 | | 4 | 100 | | 5 | 100 | | 6 | 100 | | 7 | 100 | | 8 | 100 | | 9 | 100 | | 10 | 100 | | 11 | 100 | | 12 | 100 | | 13 | 100 | | 14 | 100 | | 15 | 100 | | 16 | 100 | | 17 | 100 | | 18 | 100 | | 19 | 100 | | 20 | 100 | | 21 | 100 | | 22 | 100 | | 23 | 85 | +----+-----------+ 23 rows in set (0.02 sec) mysql> ALTER TABLE quo1 ALTER discount0 SET DEFAULT 1000; Query OK, 0 rows affected (0.06 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> select id,discount0 from quo1; +----+-----------+ | id | discount0 | +----+-----------+ | 1 | 100 | | 2 | 100 | | 3 | 100 | | 4 | 100 | | 5 | 100 | | 6 | 100 | | 7 | 100 | | 8 | 100 | | 9 | 100 | | 10 | 100 | | 11 | 100 | | 12 | 100 | | 13 | 100 | | 14 | 100 | | 15 | 100 | | 16 | 100 | | 17 | 100 | | 18 | 100 | | 19 | 100 | | 20 | 100 | | 21 | 100 | | 22 | 100 | | 23 | 85 | +----+-----------+ 23 rows in set (0.00 sec) mysql> show columns from quo1; +-----------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+---------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | time | date | YES | | NULL | | | makerref | text | YES | | NULL | | | money | int(11) | YES | | NULL | | | discount0 | int(11) | YES | | 1000 | | | discount | text | YES | | NULL | | | price | text | YES | | NULL | | | total | text | YES | | NULL | | | test | text | YES | | NULL | | | memo | text | YES | | NULL | | +-----------+---------+------+-----+---------+----------------+ 10 rows in set (0.02 sec) mysql> show columns from t200; +-------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | text | YES | | NULL | | +-------+---------+------+-----+---------+----------------+ 2 rows in set (0.48 sec) mysql> ALTER TABLE t200 MODIFY name INT NOT NULL DEFAULT 100; Query OK, 421 rows affected (0.59 sec) Records: 421 Duplicates: 0 Warnings: 0 mysql> show columns from t200; +-------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | int(11) | NO | | 100 | | +-------+---------+------+-----+---------+----------------+ 2 rows in set (0.01 sec) 列改名 mysql> show columns from quo1; +-----------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+---------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | time | date | YES | | NULL | | | makerref | text | YES | | NULL | | | money | int(11) | YES | | NULL | | | discount0 | int(11) | YES | | NULL | | | discount | text | YES | | NULL | | | price | text | YES | | NULL | | | total | text | YES | | NULL | | | testno | int(11) | YES | | NULL | | | memo | text | YES | | NULL | | +-----------+---------+------+-----+---------+----------------+ 10 rows in set (0.56 sec) mysql> ALTER TABLE quo1 CHANGE testno test TEXT; Query OK, 23 rows affected (0.48 sec) Records: 23 Duplicates: 0 Warnings: 0 mysql> show columns from quo1; +-----------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+---------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | time | date | YES | | NULL | | | makerref | text | YES | | NULL | | | money | int(11) | YES | | NULL | | | discount0 | int(11) | YES | | NULL | | | discount | text | YES | | NULL | | | price | text | YES | | NULL | | | total | text | YES | | NULL | | | test | text | YES | | NULL | | | memo | text | YES | | NULL | | +-----------+---------+------+-----+---------+----------------+ 10 rows in set (0.00 sec) mysql> show columns from quo1; +-----------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+---------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | time | date | YES | | NULL | | | makerref | text | YES | | NULL | | | money | int(11) | YES | | NULL | | | discount0 | int(11) | YES | | NULL | | | discount | text | YES | | NULL | | | price | text | YES | | NULL | | | D_FEE | text | YES | | NULL | | | total | text | YES | | NULL | | | memo | text | YES | | NULL | | +-----------+---------+------+-----+---------+----------------+ 10 rows in set (0.00 sec) 删除一列操作 mysql> ALTER TABLE quo1 DROP D_FEE; Query OK, 23 rows affected (0.70 sec) Records: 23 Duplicates: 0 Warnings: 0 追加一列操作(同时指定位置) mysql> ALTER TABLE quo1 ADD testno INT AFTER total; Query OK, 23 rows affected (0.19 sec) Records: 23 Duplicates: 0 Warnings: 0 mysql> show columns from quo1; +-----------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+---------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | time | date | YES | | NULL | | | makerref | text | YES | | NULL | | | money | int(11) | YES | | NULL | | | discount0 | int(11) | YES | | NULL | | | discount | text | YES | | NULL | | | price | text | YES | | NULL | | | total | text | YES | | NULL | | | testno | int(11) | YES | | NULL | | | memo | text | YES | | NULL | | +-----------+---------+------+-----+---------+----------------+ 10 rows in set (0.00 sec)
返回