Mysql 15: Foreign key
Written by: maffelu , 2009-04-16 21:23:01
CREATE TABLE memberorders
(
o_ID int NOT NULL AUTO_INCREMENT,
o_Number int NOT NULL,
o_Name varchar(255) NOT NULL,
p_ID int,
PRIMARY KEY (o_ID),
FOREIGN KEY (p_ID) REFERENCES members(m_ID)
)
+--------------------+--------------------+--------------------+--------------------+
|m_ID |username |password |email |
+____________________+____________________+____________________+____________________+
|1 Billy mysecret hatemail@hotmail.com|
|2 Goran abcdefg SexyButLonely@ho... |
|3 Nitro Balubaluba GetDown@manlymai... |
+-----------------------------------------------------------------------------------+
+--------------------+--------------------+--------------------+--------------------+
|o_ID |o_Number |o_Name |p_ID |
+____________________+____________________+____________________+____________________+
|1 295 Stylish Guestbook 2 |
|2 156 Book of Doom 1 |
|3 156 Slugfest: Ultimatum 1 |
|4 468 Underneath the r... 2 |
+-----------------------------------------------------------------------------------+
SELECT
o_ID as 'Order ID',
o_Number as 'Article Number',
o_Name as 'Member name',
p_ID as 'Member ID'
FROM memberorders
+--------------------+--------------------+--------------------+--------------------+
|Order ID |Article Number |Member name |Member ID |
+____________________+____________________+____________________+____________________+
|1 295 Stylish Guestbook 2 |
|2 156 Book of Doom 1 |
|3 156 Slugfest: Ultimatum 1 |
|4 468 Underneath the r... 2 |
|5 783 The Little Book ... 3 |
+-----------------------------------------------------------------------------------+
INSERT INTO memberorders
(o_Number, o_Name, p_ID)
VALUES
(783, 'The Little Book Of Chaos', 3)
+--------------------+--------------------+--------------------+--------------------+
|o_ID |o_Number |o_Name |p_ID |
+____________________+____________________+____________________+____________________+
|1 295 Stylish Guestbook 2 |
|2 156 Book of Doom 1 |
|3 156 Slugfest: Ultimatum 1 |
|4 468 Underneath the r... 2 |
|5 783 The Little Book ... 3 |
+-----------------------------------------------------------------------------------+
There are no comments on this article.
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.