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 : -
CREATE TABLE booking(
-
bookingID INT NOT NULL AUTO_INCREMENT ,
-
checkin DATETIME,
-
checkout DATETIME,
-
nights INT,
-
totalprice INT,
-
b_ic_no VARCHAR(30),
-
b_room_no INT,
-
PRIMARY KEY ( bookingID) ,
-
PRIMARY KEY ( b_ic_no ) REFERENCES customer( ic_no ) ,
-
PRIMARY KEY ( b_room_no ) REFERENCES room( room_no ),
-
ON UPDATE CASCADE ON DELETE CASCADE
-
) ENGINE = INNODB;
-
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: - CREATE TABLE `link_table` (
-
`left_id` Int Unsigned Not Null,
-
`right_id` Int Unsigned Not Null,
-
PRIMARY KEY ( `left_id`, `right_id` ),
-
FOREIGN KEY ( `left_id` )
-
REFERENCES `left_table` ( `id` ),
-
FOREIGN KEY ( `right_id` )
-
REFERENCES `right_table` ( `id` )
-
)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: - mysql> SELECT *,
-
-> CONCAT(`left_id`, '-', `right_id`) AS 'PRIMARY'
-
-> FROM `link_table`;
-
+---------+----------+---------+
-
| left_id | right_id | PRIMARY |
-
+---------+----------+---------+
-
| 1 | 1 | 1-1 |
-
| 1 | 2 | 1-2 |
-
| 1 | 3 | 1-3 |
-
| 1 | 4 | 1-4 |
-
| 1 | 5 | 1-5 |
-
+---------+----------+---------+
-
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: - 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: - CREATE TABLE `table1`(
-
/* Column definitions */,
-
PRIMARY KEY ( col1, col2, col3 ),
-
FOREIGN KEY ( col2 )
-
REFERENCES `table2` ( `some_column` )
-
)ENGINE=InnoDB;
Note that without the "ENGINE=InnoDB" clause the FOREIGN KEY is pointless. - MyISAM does not enforce foreign keys.
hi atli. can you please explain further this syntax to declare composite key : -
PRIMARY KEY ( col1, col2, col3, ..., colN)
-
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.. -
CREATE TABLE booking(
-
bookingID INT NOT NULL AUTO_INCREMENT ,
-
checkin DATETIME,
-
checkout DATETIME,
-
nights INT,
-
totalprice INT,
-
b_ic_no VARCHAR(30),
-
b_room_no INT,
-
PRIMARY KEY ( bookingID) ,
-
PRIMARY KEY ( b_ic_no ) REFERENCES customer( ic_no ) ,
-
PRIMARY KEY ( b_room_no ) REFERENCES room( room_no ) ON UPDATE CASCADE ON DELETE CASCADE
-
) ENGINE = INNODB
-
-
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:
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: - CREATE TABLE `link_table` (
-
`left_id` Int Unsigned Not Null,
-
`right_id` Int Unsigned Not Null,
-
PRIMARY KEY ( `left_id`, `right_id` ),
-
FOREIGN KEY ( `left_id` )
-
REFERENCES `left_table` ( `id` ),
-
FOREIGN KEY ( `right_id` )
-
REFERENCES `right_table` ( `id` )
-
)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: - mysql> SELECT *,
-
-> CONCAT(`left_id`, '-', `right_id`) AS 'PRIMARY'
-
-> FROM `link_table`;
-
+---------+----------+---------+
-
| left_id | right_id | PRIMARY |
-
+---------+----------+---------+
-
| 1 | 1 | 1-1 |
-
| 1 | 2 | 1-2 |
-
| 1 | 3 | 1-3 |
-
| 1 | 4 | 1-4 |
-
| 1 | 5 | 1-5 |
-
+---------+----------+---------+
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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 $...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
| |