Self-Creating Tables

I recently tried creating a new database table only to learn a table with that name exists already.

[me@mysql db] > CREATE TABLE log_search (
log_search_id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL
, search VARCHAR(255)
, created_by VARCHAR(128), created_on DATETIME)
 ENGINE=InnoDB CHARSET=utf8;
ERROR 1050 (42S01): Table 'log_search' already exists
[me@mysql db] > DESC log_search;
+---------------+---------------------+------+-----+---------------------+----------------+
| Field         | Type                | Null | Key | Default             | Extra          |
+---------------+---------------------+------+-----+---------------------+----------------+
| log_search_id | bigint(20) unsigned |      | PRI | NULL                | auto_increment | 
| search        | varchar(255)        |      |     |                     |                | 
| created_on    | datetime            |      |     | 0000-00-00 00:00:00 |                | 
| created_by    | varchar(128)        |      |     |                     |                | 
+---------------+---------------------+------+-----+---------------------+----------------+
4 rows in set (0.00 sec)

I love naming conventions!  My favorite part of programming is when I start to write a routine only to learn I already wrote it months ago in preparation for what I knew I’d be doing now.

2 thoughts on “Self-Creating Tables

  1. just pixels says:

    I tried to create that same table back in 1975, but E.F. Codd beat me to it.

    Just in passing, does mySQL support default column values? Like “created_on default now”.

  2. just pixels says:

    why does your table need a unique ID? There is (presumably) no update, just insert and done. Does MySQL require PKs?

Leave a Reply

Your email address will not be published. Required fields are marked *