Using a Join to Create a Lookup Table from Descriptive Labels
mysql>
mysql> CREATE TABLE coin
-> (
-> id INT UNSIGNED NOT NULL AUTO_INCREMENT,
-> date CHAR(5) NOT NULL, # 4 digits + mint letter
-> denom CHAR(20) NOT NULL, # denomination (e.g., Lincoln cent)
-> PRIMARY KEY (id)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> CREATE TABLE denom
-> (
-> denom_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
-> PRIMARY KEY (denom_id)
-> )
-> SELECT DISTINCT denom AS name FROM coin;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
mysql>
mysql> INSERT INTO coin (date,denom) VALUES('1944s','Lincoln cent');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO coin (date,denom) VALUES('1977','Roosevelt dime');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO coin (date,denom) VALUES('1955d','Lincoln cent');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO coin (date,denom) VALUES('1938','Jefferson nickel');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO coin (date,denom) VALUES('1964','Kennedy half dollar');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO coin (date,denom) VALUES('1959','Lincoln cent');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO coin (date,denom) VALUES('1945','Jefferson nickel');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO coin (date,denom) VALUES('1905','Buffalo nickel');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO coin (date,denom) VALUES('1924','Mercury head dime');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO coin (date,denom) VALUES('2010','Roosevelt dime');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO coin (date,denom) VALUES('1937','Mercury head dime');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO coin (date,denom) VALUES('1977','Kennedy half dollar');
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> INSERT INTO denom (name) SELECT DISTINCT denom FROM coin;
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql>
mysql> drop table denom;
Query OK, 0 rows affected (0.00 sec)
mysql> drop table coin;
Query OK, 0 rows affected (0.00 sec)
Related examples in the same category