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 > syntax question on creating index

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-19-09, 14:00
rbfree rbfree is offline
Registered User
 
Join Date: Feb 2009
Posts: 104
syntax question on creating index

In a DDL statement creating a foreign-column (within a create table statement), should the ADD INDEX statement go below the column definitions with the contraint clauses?

All my texts use examples with ALTER TABLE code. Is the syntax the same with a CREATE TABLE statement?

Code:
CREATE TABLE thistable
(
     thistableID            INTEGER     NOT NULL    PRIMARY KEY
     foreignkeynameID   INTEGER     NOT NULL
     nonkeycolumnname DECIMAL(3,2) NULL
ADD INDEX 
    fkindexname_idx (foreignkeynameID)
ADD CONSTRAINT 
     foeignkeynameID_fk
            FOREIGN KEY (foreignkeynameID)
                  REFERENCES thattable (foreignkeynameID)
                         ON DELETE RESTRICT
	                 ON UPDATE  CASCADE
)
Reply With Quote
  #2 (permalink)  
Old 03-19-09, 14:17
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
you can do it in the CREATE statement, or you can do it afterwards, with ALTER TABLE...

... but you can not, as you have done there, use the ADD keyword inside the CREATE statement

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 03-19-09, 14:35
rbfree rbfree is offline
Registered User
 
Join Date: Feb 2009
Posts: 104
redone index key

So, then, like this?

Code:
CREATE TABLE thistable
(
     thistableID            INTEGER      NOT NULL    PRIMARY KEY
     foreignkeynameID   INTEGER      NOT NULL  INDEX 
              fkindexname_idx (foreignkeynameID)
     nonkeycolumnname DECIMAL(3,2) NULL 
ADD CONSTRAINT 
     foeignkeynameID_fk
            FOREIGN KEY (foreignkeynameID)
                  REFERENCES thattable (foreignkeynameID)
                         ON DELETE RESTRICT
	                 ON UPDATE  CASCADE
)
Reply With Quote
  #4 (permalink)  
Old 03-19-09, 14:37
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by rbfree
So, then, like this?
what happened when you tested that?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 03-19-09, 14:54
rbfree rbfree is offline
Registered User
 
Join Date: Feb 2009
Posts: 104
somewhat embarrassing answer

Quote:
Originally Posted by r937
what happened when you tested that?
To be honest, I've been holding out for another text (MySQL Tutorial) that I've ordered so I can get acquainted with MySQL, which I only installed this week. I knew it was only a matter of time before you or someone else asked that question.

So, in the meantime, I was building the basic txt file with all my create table statements.

However, I see that it's time to be testing these constructions, so I'll redirect my energies for a couple of days until this text arrives and I've had the chance to digest it. (Should be here today or tomorrow.)

When I get to that point, I'll answer your question.
Reply With Quote
  #6 (permalink)  
Old 03-24-09, 13:45
rbfree rbfree is offline
Registered User
 
Join Date: Feb 2009
Posts: 104
error

Quote:
Originally Posted by r937
what happened when you tested that?
Syntax error. Placing the statement directly before the constraint works. That's just as well and it looks less clustered.
__________________
In theory, there is no difference between theory and practice. In practice there is.
Disputed Origins

Ninety-three percent of all statistics are made-up on the spot.
Ancient proverb.
Reply With Quote
  #7 (permalink)  
Old 03-25-09, 19:53
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
your foreign key was fine where it was, your error, as pointed out, was having the word ADD in your constraint.
Reply With Quote
  #8 (permalink)  
Old 03-25-09, 20:04
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
well you could of course not bother with creating your own ddl and use a tool such as MySQL Adminstrator or Heidi to do that for you. Im not knocking the creation of DDL but if theres a tool that'l do it for you why not use it?
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #9 (permalink)  
Old 03-25-09, 22:26
rbfree rbfree is offline
Registered User
 
Join Date: Feb 2009
Posts: 104
thanks

Thanks for all pointers here.

Re. why I'm writing the DDL is that I want to understand the basics. I'm also playing with the MySQL query/admin tool and HeidiSQL. The learning curve is steep right now so I'm asking lots of questions and dealing with the devil of details. My apologies for this. I'm starting to put it all together, establish some templates, habits, etc.
__________________
In theory, there is no difference between theory and practice. In practice there is.
Disputed Origins

Ninety-three percent of all statistics are made-up on the spot.
Ancient proverb.
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