473,394 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

composite key syntax. How can I fix this?

17
i wanted to create a table name booking that have 3 composite key where 2
of the composite key is referring to each another table named customer and room.

however when i wanted to create the table, it give me error. can someone tell me
how can i fix this.

Here's the command :
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE booking(
  2. bookingID INT NOT NULL AUTO_INCREMENT ,
  3. checkin DATETIME,
  4. checkout DATETIME,
  5. nights INT,
  6. totalprice INT,
  7. b_ic_no VARCHAR(30),
  8. b_room_no INT,
  9. PRIMARY KEY ( bookingID) ,
  10. PRIMARY KEY ( b_ic_no ) REFERENCES customer( ic_no ) ,
  11. PRIMARY KEY ( b_room_no ) REFERENCES room( room_no ),
  12. ON UPDATE CASCADE ON DELETE CASCADE
  13. ) ENGINE = INNODB;
  14.  
Mar 27 '10 #1

✓ answered by Atli

Adding a column to a composite primary key does not affect the way you use it to create foreign keys. The column remains unaltered, even though it is a part of the key. - The key only enforces some restrictions on the values the column can take. (Namely that they be unique when combined with the other PK columns.)

By using more than one column as a primary key, you aren't creating multiple keys. You are creating a single primary key made up of all three.

Consider a typical N:M (many-to-many) link table; a table that links rows in one table to rows in another table. It only has to have two columns, one to reference a row in either table, and both serving as the Primary Key. Like:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `link_table` (
  2.     `left_id` Int Unsigned Not Null,
  3.     `right_id` Int Unsigned Not Null,
  4.     PRIMARY KEY ( `left_id`, `right_id` ),
  5.     FOREIGN KEY ( `left_id` ) 
  6.         REFERENCES `left_table` ( `id` ),
  7.     FOREIGN KEY ( `right_id` ) 
  8.         REFERENCES `right_table` ( `id` )
  9. )ENGINE=InnoDB;
Say that we link ID #1 on the left to IDs #1 through #5 on the right. The data and the PK for this table could be displayed by doing something like:
Expand|Select|Wrap|Line Numbers
  1. mysql> SELECT *,
  2.     ->     CONCAT(`left_id`, '-', `right_id`) AS 'PRIMARY'
  3.     -> FROM `link_table`;
  4. +---------+----------+---------+
  5. | left_id | right_id | PRIMARY |
  6. +---------+----------+---------+
  7. |       1 |        1 | 1-1     |
  8. |       1 |        2 | 1-2     |
  9. |       1 |        3 | 1-3     |
  10. |       1 |        4 | 1-4     |
  11. |       1 |        5 | 1-5     |
  12. +---------+----------+---------+
  13. 5 rows in set (0.00 sec)
The PRIMARY key is separate. It doesn't affect how the actual columns are used. The columns themselves are still working as Foreign Keys for their respective tables, just like they would without the primary key restraint.

The point of a composite primary key, just like an primary key, is to make sure each row can be identified by a unique value. The only difference between a composite key and a normal key is that a composite key is based on multiple columns; the unique value that is used to identify the row is created from the values of multiple columns. - This has nothing to do with how the columns are linked to other tables. That is a completely different thing. You can create a foreign key from any column, even one that is a part of a primary key.

Hope that helps clear it up.

3 6905
Atli
5,058 Expert 4TB
Hey.

I assume you are talking about creating a composite primary key; a primary key made up of more than one column? - The syntax to do that is as follows:
Expand|Select|Wrap|Line Numbers
  1. PRIMARY KEY ( col1, col2, col3, ..., colN)
Creating foreign key restraints is done seperately, using the FOREIGN KEY syntax. For example, if you wanted to add a foreign key constraint on "col2" in a three-column primary, you might do:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `table1`(
  2.     /* Column definitions */,
  3.     PRIMARY KEY ( col1, col2, col3 ),
  4.     FOREIGN KEY ( col2 )
  5.         REFERENCES `table2` ( `some_column` )
  6. )ENGINE=InnoDB;
Note that without the "ENGINE=InnoDB" clause the FOREIGN KEY is pointless. - MyISAM does not enforce foreign keys.
Mar 27 '10 #2
digituf
17
hi atli. can you please explain further this syntax to declare composite key :

Expand|Select|Wrap|Line Numbers
  1. PRIMARY KEY ( col1, col2, col3, ..., colN)
  2.  
it seems that the primary key that i wanted to define as composite key is declare in one row 'PRIMARY KEY (col1, col2, col3)', so how can i know that which primary key that supossed to refer to the table that i wanted..

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE booking(
  2. bookingID INT NOT NULL AUTO_INCREMENT ,
  3. checkin DATETIME,
  4. checkout DATETIME,
  5. nights INT,
  6. totalprice INT,
  7. b_ic_no VARCHAR(30),
  8. b_room_no INT,
  9. PRIMARY KEY ( bookingID) ,
  10. PRIMARY KEY ( b_ic_no ) REFERENCES customer( ic_no ) ,
  11. PRIMARY KEY ( b_room_no ) REFERENCES room( room_no ) ON UPDATE CASCADE ON DELETE CASCADE
  12. ) ENGINE = INNODB
  13.  
  14.  
if you see from the above myql syntax, i wanted the 'b_ic_no' refer to the customer table that have 'ic_no' which is the primary key in the table customer...

i'm using phpmyadmin to create this table just for your info..

any explanation is really appreciated...

here's the relationship that i've created in acecss:
Attached Images
File Type: jpg relationship room.jpg (12.4 KB, 5078 views)
Mar 28 '10 #3
Atli
5,058 Expert 4TB
Adding a column to a composite primary key does not affect the way you use it to create foreign keys. The column remains unaltered, even though it is a part of the key. - The key only enforces some restrictions on the values the column can take. (Namely that they be unique when combined with the other PK columns.)

By using more than one column as a primary key, you aren't creating multiple keys. You are creating a single primary key made up of all three.

Consider a typical N:M (many-to-many) link table; a table that links rows in one table to rows in another table. It only has to have two columns, one to reference a row in either table, and both serving as the Primary Key. Like:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `link_table` (
  2.     `left_id` Int Unsigned Not Null,
  3.     `right_id` Int Unsigned Not Null,
  4.     PRIMARY KEY ( `left_id`, `right_id` ),
  5.     FOREIGN KEY ( `left_id` ) 
  6.         REFERENCES `left_table` ( `id` ),
  7.     FOREIGN KEY ( `right_id` ) 
  8.         REFERENCES `right_table` ( `id` )
  9. )ENGINE=InnoDB;
Say that we link ID #1 on the left to IDs #1 through #5 on the right. The data and the PK for this table could be displayed by doing something like:
Expand|Select|Wrap|Line Numbers
  1. mysql> SELECT *,
  2.     ->     CONCAT(`left_id`, '-', `right_id`) AS 'PRIMARY'
  3.     -> FROM `link_table`;
  4. +---------+----------+---------+
  5. | left_id | right_id | PRIMARY |
  6. +---------+----------+---------+
  7. |       1 |        1 | 1-1     |
  8. |       1 |        2 | 1-2     |
  9. |       1 |        3 | 1-3     |
  10. |       1 |        4 | 1-4     |
  11. |       1 |        5 | 1-5     |
  12. +---------+----------+---------+
  13. 5 rows in set (0.00 sec)
The PRIMARY key is separate. It doesn't affect how the actual columns are used. The columns themselves are still working as Foreign Keys for their respective tables, just like they would without the primary key restraint.

The point of a composite primary key, just like an primary key, is to make sure each row can be identified by a unique value. The only difference between a composite key and a normal key is that a composite key is based on multiple columns; the unique value that is used to identify the row is created from the values of multiple columns. - This has nothing to do with how the columns are linked to other tables. That is a completely different thing. You can create a foreign key from any column, even one that is a part of a primary key.

Hope that helps clear it up.
Mar 28 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Ken in Melbourne Australia | last post by:
If I use the curly bracket syntax (referred to as the complex syntax) within a string, how do I get to call a function within it? The php manual says that the first (or previous) character for...
7
by: Dave | last post by:
I have 2 tables, one with names, and another with addresses, joined by their CIVICID number (unique to the ADDRESSINFO table) in Oracle. I need to update a field in the NAMEINFO table for a...
3
by: mheydman | last post by:
I apologize if this has been asked before- I searched google but could not find a concrete answer. I recently inherited a database whose t-sql code is written in a format that I find difficult...
19
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $...
3
by: | last post by:
Here is an example 1) C# Programmer's Reference Old: http://msdn.microsoft.com/library/en-us/csref/html/vcrefTheClassType.asp New: http://msdn2.microsoft.com/en-us/library/0b0thckt.aspx The...
2
by: bor_kev | last post by:
Hi, First of all, i want to use the new managed class syntax and STL.NET under Microsoft Visual (C++) Studio 2005 Beta. I read in a Microsoft...
5
by: KJ | last post by:
This is kind of hard to explain but I have a The controls are created with CreateChildControls(). Now let say I click a button and an error occurs in the control. If I throw it up it goes back...
0
by: Peter Morris [Droopy eyes software] | last post by:
Hi all I need to create a composite control. This control should allow the site designer to add one or more "MyButton" instances to the list, they will then set various properties on the...
3
by: bearophileHUGS | last post by:
Probably you have already discussed this topic, but maybe you can stand touching it again briefly. Maybe properties aren't used so often to deserve a specific syntax, but I don't like their syntax...
0
by: Jacques Vandensavel | last post by:
I have a legacy table which is composed out three key fields. It is representing a bank. I'm using NHibernate in my ASP.NET application to glue everything together. I'm already searching for an...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.