If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > CREATE TABLE help (unnamed index???)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-03-10, 02:51
krice krice is offline
Registered User
 
Join Date: Jun 2010
Posts: 4
Question CREATE TABLE help (unnamed index???)

Hello, I need help understanding what 'index (foreignkey)' does below:

Code:
CREATE TABLE user (id integer not null primary key auto_increment,
    email varchar(255) not null,
    entered datetime,
    modified timestamp,
    uniqid varchar(255),
    unique (email),
    password varchar(255),
    foreignkey varchar(100),
    index (foreignkey),
    index idx_uniqid (uniqid),
    index emailidx (email),
    index enteredindex (entered) )
As you can see, the last four lines are all in the form: index index-name (column-name)

Except the fourth line up 'index (foreignkey)' does not have any "index-name".

Does this line actually create an index like the three lines below it? Or, does it perform some other function? If it creates an index, how is it there is no name?

I am trying to understand what this one line does. Much thanks!
Reply With Quote
  #2 (permalink)  
Old 06-03-10, 06:02
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by krice View Post
Does this line actually create an index like the three lines below it?
yes


Quote:
Originally Posted by krice View Post
If it creates an index, how is it there is no name?
the user-supplied index name is optional

do a SHOW CREATE TABLE for this table to see what name the index gets if you don't supply one
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-03-10, 11:40
krice krice is offline
Registered User
 
Join Date: Jun 2010
Posts: 4
Interesting. I would think for an index to be useful you'd have to be able to refer to it by name. Need to learn more...

MS-SQL requires a name so this syntax was confusing to me, as well as the:

unique (email)

which adds a 'unique' constraint, as opposed to

unique idx_email (email)

which adds an index, I think???

Still confused, but getting better. Thanks for all help!
Reply With Quote
  #4 (permalink)  
Old 06-03-10, 11:45
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by krice View Post
unique idx_email (email)

which adds an index, I think???
that's correct, a unique index

all unique constraints are implemented "under the covers" as unique indexes (i believe this is the case in sql server too)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 06-03-10, 12:49
krice krice is offline
Registered User
 
Join Date: Jun 2010
Posts: 4
You've been greatly helpful! I'm actually porting the above CREATE TABLE over to MS-SQL which doesn't apparently allow unnamed indexes--this I suppose I can just use a "dummy" index name like so:

Code:
CREATE INDEX dummy (foreignkey)
I'm still unclear on why an unnamed index is useful in MySQL. Gee, maybe I should read a book on the subject!!

The MS-SQL syntax for a unique constraint is a bit different (and done in the column definition):

Code:
CREATE TABLE x (email varchar(255) not null UNIQUE)
I think a unique constraint in SQL Server is different from a unique index which is:

Code:
CREATE UNIQUE INDEX name ON table (email)
Thanks again!
Reply With Quote
  #6 (permalink)  
Old 06-03-10, 13:53
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
actually, in any database an index name is useless to anyone but the DAB team. You are not going to select data from an index. Even if you have index only access on a particular query, it is from the table that you query. The DBA staff have a different picture of the database and would on occasion need to know the name of an index.
Dave
Reply With Quote
  #7 (permalink)  
Old 06-03-10, 18:07
krice krice is offline
Registered User
 
Join Date: Jun 2010
Posts: 4
Quote:
Originally Posted by dav1mo View Post
actually, in any database an index name is useless to anyone but the DAB team. You are not going to select data from an index.
Thanks Dave. I suppose that just leaves me wondering why the code above names three of the indexes but not the fourth.

Code:
    index (foreignkey),
    index idx_uniqid (uniqid),
    index emailidx (email),
    index enteredindex (entered)
I will provide a "dummy" name for MS-SQL and see if anything breaks, then! Thanks to all.
Reply With Quote
  #8 (permalink)  
Old 06-04-10, 10:03
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
Quote:
Originally Posted by krice View Post
Thanks Dave. I suppose that just leaves me wondering why the code above names three of the indexes but not the fourth.
Sloppy coding practices maybe?
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On