Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

syntax problem for CREATE TABLE ??

 
   Database Help (Home) -> mySQL RSS
Next:  SQL Server process 100% CPU  
Author Message
Baoqiu Cui

External


Since: Feb 09, 2005
Posts: 3



(Msg. 1) Posted: Wed Feb 09, 2005 2:40 pm
Post subject: syntax problem for CREATE TABLE ??
Archived from groups: mailing>database>mysql (more info?)

Hi,

I was playing with MySQL (4.1.9) during the weekend, but noticed a minor
problem and would like someone to explain this to me.

Basically I could successfully create a table with one unique column
like this:

mysql> create table t1 (c1 int unique);
Query OK, 0 rows affected (0.08 sec)

What bothers me is that I just don't see how the CREATE TABLE syntax
allows the UNIQUE keyword to appear in a COLUMN_DEFINITION:

COLUMN_DEFINITION:
COL_NAME TYPE [NOT NULL | NULL] [DEFAULT DEFAULT_VALUE]
[AUTO_INCREMENT] [[PRIMARY] KEY] [COMMENT 'STRING']
[REFERENCE_DEFINITION]

The only explanation I can think of is that this way of declaring a
column unique was supported in some old release and the current version
of MySQL is just trying to keep the backwards compatibility (like SERIAL
column data type).

Does anyone know the real reason? Any input will be appreciated!

- Baoqiu

--
Baoqiu Cui <cbaoqiu at yahoo.com>

 >> Stay informed about: syntax problem for CREATE TABLE ?? 
Back to top
Login to vote
Bill Karwin1

External


Since: Jun 17, 2004
Posts: 42



(Msg. 2) Posted: Wed Feb 09, 2005 2:40 pm
Post subject: Re: syntax problem for CREATE TABLE ?? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Baoqiu Cui wrote:
 > mysql> create table t1 (c1 int unique);
 > Query OK, 0 rows affected (0.08 sec)
 >
 > What bothers me is that I just don't see how the CREATE TABLE syntax
 > allows the UNIQUE keyword to appear in a COLUMN_DEFINITION:

It could just be an omission from the documentation. On the doc page
for create table, I find this paragraph:

"From MySQL 4.1.0 on, the attribute SERIAL can be used as an alias for
BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE."

That sort of implies that UNIQUE can appear in that position, similar to
PRIMARY KEY.

Regards,
Bill K.

 >> Stay informed about: syntax problem for CREATE TABLE ?? 
Back to top
Login to vote
Aggro

External


Since: Aug 28, 2003
Posts: 8



(Msg. 3) Posted: Wed Feb 09, 2005 3:40 pm
Post subject: Re: syntax problem for CREATE TABLE ?? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Baoqiu Cui wrote:

 > What bothers me is that I just don't see how the CREATE TABLE syntax
 > allows the UNIQUE keyword to appear in a COLUMN_DEFINITION:
 >
 > COLUMN_DEFINITION:
 > COL_NAME TYPE [NOT NULL | NULL] [DEFAULT DEFAULT_VALUE]
 > [AUTO_INCREMENT] [[PRIMARY] KEY] [COMMENT 'STRING']
 > [REFERENCE_DEFINITION]

You seem to have copied only the lower part of the definition. Read it
all and you should find the UNIQUE also:

<a rel="nofollow" style='text-decoration: none;' href="http://dev.mysql.com/doc/mysql/en/create-table.html" target="_blank">http://dev.mysql.com/doc/mysql/en/create-table.html</a>

------------------------
create_definition:
column_definition
| [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)
| KEY [index_name] [index_type] (index_col_name,...)
| INDEX [index_name] [index_type] (index_col_name,...)
| [CONSTRAINT [symbol]] UNIQUE[INDEX]
[index_name] [index_type] (index_col_name,...)
| [FULLTEXT|SPATIAL] [INDEX] [index_name] (index_col_name,...)
| [CONSTRAINT [symbol]] FOREIGN KEY
[index_name] (index_col_name,...) [reference_definition]
| CHECK (expr)
column_definition:
col_name type [NOT NULL | NULL] [DEFAULT default_value]
[AUTO_INCREMENT] [[PRIMARY] KEY] [COMMENT 'string']
[reference_definition]
------------------------
 >> Stay informed about: syntax problem for CREATE TABLE ?? 
Back to top
Login to vote
Baoqiu Cui

External


Since: Feb 09, 2005
Posts: 3



(Msg. 4) Posted: Wed Feb 09, 2005 4:40 pm
Post subject: Re: syntax problem for CREATE TABLE ?? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Aggro writes:

 > Baoqiu Cui wrote:
 >
  >> What bothers me is that I just don't see how the CREATE TABLE syntax
  >> allows the UNIQUE keyword to appear in a COLUMN_DEFINITION:
  >> COLUMN_DEFINITION:
  >> COL_NAME TYPE [NOT NULL | NULL] [DEFAULT DEFAULT_VALUE]
  >> [AUTO_INCREMENT] [[PRIMARY] KEY] [COMMENT 'STRING']
  >> [REFERENCE_DEFINITION]
 >
 > You seem to have copied only the lower part of the definition. Read it
 > all and you should find the UNIQUE also:
 >
<font color=purple> > <a rel="nofollow" style='text-decoration: none;' href="http://dev.mysql.com/doc/mysql/en/create-table.html</font" target="_blank">http://dev.mysql.com/doc/mysql/en/create-table.html</font</a>>

The first part, which contains UNIQUE, is NOT for a COLUMN_DEFINITION.
I know I can do the following CREATE TABLE:

mysql> create table t1 (c1 int, unique (c1));
Query OK, 0 rows affected (0.06 sec)

but it is different from "create table t1 (c1 int unique)". I want to
find the syntax to allow the latter.

--
Baoqiu Cui <cbaoqiu at yahoo.com>
 >> Stay informed about: syntax problem for CREATE TABLE ?? 
Back to top
Login to vote
Baoqiu Cui

External


Since: Feb 09, 2005
Posts: 3



(Msg. 5) Posted: Wed Feb 09, 2005 4:40 pm
Post subject: Re: syntax problem for CREATE TABLE ?? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Bill Karwin writes:

 > It could just be an omission from the documentation. On the doc page
 > for create table, I find this paragraph:
 >
 > "From MySQL 4.1.0 on, the attribute SERIAL can be used as an alias for
 > BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE."
 >
 > That sort of implies that UNIQUE can appear in that position, similar
 > to PRIMARY KEY.

That is what I thought. Maybe the CREATE TABLE syntax should be changed
to explicitly allow UNIQUE in COLUMN_DEFINITION, because many people
(and many exiting MySQL examples) are using UNIQUE in this way.

--
Baoqiu Cui <cbaoqiu at yahoo.com>
 >> Stay informed about: syntax problem for CREATE TABLE ?? 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Create table syntax? - Warning: Error while executing this query:CREATE TABLE "purchaseorder" ( "PurchaseOrderID" int(10) unsigned NOT NULL auto_increment, "PurchaseCost" double unsigned zerofill NOT NULL default '0000000000000000000000', &...

Mysql InnoDB PHP result problem - i have a strange problem : MySQL 4.0.21 innodb and php 4.3.9-1 all tables are in innodb type. For a simple select statement sometimes i get result and sometimes nothing (i.e mysql_num_rows()>0 or mysql_num_rows()==0). It's a random behaviour. If my t...

FLUSH TABLES hangs if table is locked - Using FLUSH TABLES via the C query API mysql_query() hangs if the table is locked already. That is to say, nothing prevents me from running a LOCK TABLES twice; it won't tell me "it's already locked, don't try to run a FLUSH". Anyone know ...

Java mysql show table status - Hi, I am using java to retrieve a resultset with "show table status" mysql command. I am having problem reading the [name] and [engine] column. In the metadata object it is showing as VARCHAR but when I do getString() it is returning me the o...

newbie: loading image into MySQL table - Hello, How can I load image to MySQL table? I need a solution for Windows and Linux. I tried LOAD_FILE() but it returns me NULL. Thanks in advance.
   Database Help (Home) -> mySQL All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]