473,385 Members | 2,044 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,385 software developers and data experts.

Creating Foreign Key Constraint

bk3
I'm having a bit of a problem creating foreign keys on a MySQL database.
The tables create and everything seems to be fine. But I just wanted to
test out adding a row to one of the child tables without having anything in
the parent table and it lets me insert the row, which it shouldn't
obviously. How do i create it so that the constraints work?

Thanks,
Bill

Here is my create table code:

CREATE TABLE tblPhotographs
(PhotoID INT NOT NULL AUTO_INCREMENT,
Photo BLOB,
PhotoDate DATETIME,
PRIMARY KEY(PhotoID))
TYPE=InnoDB;

CREATE TABLE tblPeople
(PhotoID INT NOT NULL,
PersonID INT NOT NULL AUTO_INCREMENT,
Person VARCHAR(50),
INDEX FK_PhotoID(PhotoID),
FOREIGN KEY(PhotoID) REFERENCES tblPhotographs(PhotoID),
PRIMARY KEY(PersonID))
TYPE=InnoDB;

CREATE TABLE tblPlaces
(PhotoID INT NOT NULL,
PlaceID INT NOT NULL AUTO_INCREMENT,
Place VARCHAR(50),
INDEX FK_PhotoID(PhotoID),
FOREIGN KEY(PhotoID) REFERENCES tblPhotographs(PhotoID),
PRIMARY KEY(PlaceID))
TYPE=InnoDB;
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.772 / Virus Database: 519 - Release Date: 10/1/2004
Jul 20 '05 #1
3 2106
RP
bk3 stuurde op 4-10-2004 8:28 het volgende bericht:
I'm having a bit of a problem creating foreign keys on a MySQL database.
The tables create and everything seems to be fine. But I just wanted to
test out adding a row to one of the child tables without having anything in
the parent table and it lets me insert the row, which it shouldn't
obviously. How do i create it so that the constraints work?

Thanks,
Bill

Here is my create table code:

CREATE TABLE tblPhotographs
(PhotoID INT NOT NULL AUTO_INCREMENT,
Photo BLOB,
PhotoDate DATETIME,
PRIMARY KEY(PhotoID))
TYPE=InnoDB;

CREATE TABLE tblPeople
(PhotoID INT NOT NULL,
PersonID INT NOT NULL AUTO_INCREMENT,
Person VARCHAR(50),
INDEX FK_PhotoID(PhotoID),
FOREIGN KEY(PhotoID) REFERENCES tblPhotographs(PhotoID),
PRIMARY KEY(PersonID))
TYPE=InnoDB;

CREATE TABLE tblPlaces
(PhotoID INT NOT NULL,
PlaceID INT NOT NULL AUTO_INCREMENT,
Place VARCHAR(50),
INDEX FK_PhotoID(PhotoID),
FOREIGN KEY(PhotoID) REFERENCES tblPhotographs(PhotoID),
PRIMARY KEY(PlaceID))
TYPE=InnoDB;


try adding the constraints ;-) :

FOREIGN KEY(PhotoID) REFERENCES tblPhotographs(PhotoID) ON DELETE
RESTRICT ON UPDATE CASCADE,

RP

Jul 20 '05 #2
bk3
Oh....I thought the FOREIGN KEY was the constraint...heh. Thanks. I'll
give that a try. :)
"RP" <do**@mail.me.please> wrote in message
news:cj**********@news1.zwoll1.ov.home.nl...
bk3 stuurde op 4-10-2004 8:28 het volgende bericht:
I'm having a bit of a problem creating foreign keys on a MySQL database.
The tables create and everything seems to be fine. But I just wanted to
test out adding a row to one of the child tables without having anything
in the parent table and it lets me insert the row, which it shouldn't
obviously. How do i create it so that the constraints work?

Thanks,
Bill

Here is my create table code:

CREATE TABLE tblPhotographs
(PhotoID INT NOT NULL AUTO_INCREMENT,
Photo BLOB,
PhotoDate DATETIME,
PRIMARY KEY(PhotoID))
TYPE=InnoDB;

CREATE TABLE tblPeople
(PhotoID INT NOT NULL,
PersonID INT NOT NULL AUTO_INCREMENT,
Person VARCHAR(50),
INDEX FK_PhotoID(PhotoID),
FOREIGN KEY(PhotoID) REFERENCES tblPhotographs(PhotoID),
PRIMARY KEY(PersonID))
TYPE=InnoDB;

CREATE TABLE tblPlaces
(PhotoID INT NOT NULL,
PlaceID INT NOT NULL AUTO_INCREMENT,
Place VARCHAR(50),
INDEX FK_PhotoID(PhotoID),
FOREIGN KEY(PhotoID) REFERENCES tblPhotographs(PhotoID),
PRIMARY KEY(PlaceID))
TYPE=InnoDB;


try adding the constraints ;-) :

FOREIGN KEY(PhotoID) REFERENCES tblPhotographs(PhotoID) ON DELETE
RESTRICT ON UPDATE CASCADE,

RP

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.772 / Virus Database: 519 - Release Date: 10/1/2004
Jul 20 '05 #3
bk3 wrote:
I'm having a bit of a problem creating foreign keys on a MySQL database.
The tables create and everything seems to be fine. But I just wanted to
test out adding a row to one of the child tables without having anything in
the parent table and it lets me insert the row, which it shouldn't
obviously. How do i create it so that the constraints work?


If you use MyISAM table types, referential integrity declarations are
parsed, but not enforced (or even stored).

See:
http://dev.mysql.com/doc/mysql/en/AN...eign_Keys.html

To get working constraints, you must use InnoDB table types.

Regards,
Bill K.
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jeremiah Jacks | last post by:
I just upgraded to MySQL 4.0.14-standard for RedHat Linux and am using = the pre-compiled binaries. I have a database with INNODB tables. When I insert a row into one of the child tables, I get...
31
by: Robert Brown | last post by:
Let's say I have a type hierarchy: (just an example) the general entity customer: CREATE TABLE customer(customer_id int, customer_name varchar(250), customer_type int) three specific...
2
by: adammitchell | last post by:
How can you indicate that a FOREIGN KEY constraint references two columns in two different tables? "SQL Server Books Online" show an example of how to reference two columns in the SAME table:...
5
by: Barbara Lindsey | last post by:
Thank you for your help on the trigger question. The RULE worked for most of the cases I had for this, but I have one that is giving me trouble. Here are my table definitions: CREATE SEQUENCE...
8
by: Brian S. Smith | last post by:
Hi gang, Please help. I've been through the Access help, searched the Web, and I can't seem to get a straight answer. As the Subject line suggests, I want to run a fairly simple VB/Access Sub...
13
by: Tibor | last post by:
I am using PostgreSQL 7.4.1 (only through psql) I know, that the command ALTER TABLE OFFICES DROP PRIMARY KEY (CITY); and its foreign key equivalent: ALTER TABLE SALESREPS DROP CONSTRAINT
5
by: Peter Erickson | last post by:
I am running postgresql 7.4.2 and having problems creating a trigger function properly. I keep getting the following error: ERROR: OLD used in query that is not in rule I have a table called...
2
by: Ian Davies | last post by:
I have created a database with about 17 tables. I have been creating foreign keys some of which have worked but when creating others I get the message below ************************* 1005...
2
by: astolpho | last post by:
I am using a slightly outdated reference book on J2EE programming. It gives 2 methods of creating a database used in its casestudies. The first is an ANT script that gives the following output: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.