473,513 Members | 2,358 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with foreign keys to datetime field

Hello *,

i write you with an urgent problem. I would like to create forign keys
to datetime field, but it doesn't seem to work. I get [Error Code:
1005, SQL State: HY000] Can't create table
'.\sampleDB\student_module.frm' (errno: 150) error.

the tables look like
CREATE TABLE IF NOT EXISTS sampleDB.student (
student.from1 DATETIME not null,
student.from2 DATETIME not null,
PRIMARY KEY (from1))

CREATE TABLE IF NOT EXISTS gruppex.student_module(
student_module.to1 DATETIME not null,
student_module.to2 DATETIME not null,
PRIMARY KEY (to1),
FOREIGN KEY (to1) REFERENCES student (from1),
FOREIGN KEY (to2) REFERENCES student (from2))

I know that i could do this with use of ids fields, but in this case i
need to use of datetime. It works when i use only first foreign key
statemetnt. In case of two it doesn't work even when i make
student.from2 also as primary key
Please help!!

Thanks in Advance
Przemek

Sep 15 '05 #1
3 5200
heroe wrote:
Hello *,

i write you with an urgent problem. I would like to create forign keys
to datetime field, but it doesn't seem to work. ...
It works when i use only first foreign key
statemetnt. In case of two it doesn't work even when i make
student.from2 also as primary key


You can make a foreign key reference only to an indexed column in
another table, and if the referenced column is part of a multi-column
index, it must be the first column in that index.
See http://dev.mysql.com/doc/mysql/en/in...nstraints.html

You can only have one primary key in a table, but you can have multiple
indexes defined for different columns. Creating a primary key
implicitly creates an index.

So you can have your primary key on student.from1, but you should define
a simple index for from2. See
http://dev.mysql.com/doc/mysql/en/create-index.html

Then you should be able to define the foreign key to reference the
indexed column.

Regards,
Bill K.
Sep 15 '05 #2
Hello,

I don't agree that you can have only one key in the table.
i create f.e.
CREATE TABLE IF NOT EXISTS sampleDB.student (
student.from1 DATETIME not null,
student.from2 DATETIME not null,
PRIMARY KEY (from1,from2 )) and it creates the table when both columns
are signed as primary key.

I actually have found that the second create is possible but it has to
look like

create table sampleDB.student_module(
to1 DATETIME not null,
to2 DATETIME not null,
primary key (datum1),
FOREIGN KEY (to1, to2) REFERENCES student (from1, from2))

Now it comes howewer problem for more tables for example, the trd table
student_exam with same statement as the first one.
when i now try to create with
create table sampleDB.student_exam(
to1 DATETIME not null,
to2 DATETIME not null,
primary key (datum1),
FOREIGN KEY (to1) REFERENCES student (from1),
FOREIGN KEY (to2) REFERENCES student (to2))

i get the same error like before...

Regards
Przemek

Sep 15 '05 #3
heroe wrote:
Hello,

I don't agree that you can have only one key in the table.
i create f.e.
CREATE TABLE IF NOT EXISTS sampleDB.student (
student.from1 DATETIME not null,
student.from2 DATETIME not null,
PRIMARY KEY (from1,from2 )) and it creates the table when both columns
are signed as primary key.
That's called a "compound key," when it is defined over multiple
columns. It still counts as one key, and it is permitted. What I meant
was that it is _not_ permitted to do this:

CREATE TABLE IF NOT EXISTS sampleDB.student (
student.from1 DATETIME not null,
student.from2 DATETIME not null,
PRIMARY KEY (from1),
PRIMARY KEY (from2))

That would constitute two primary keys, and it is illegal (and
nonsensical according to relational database theory).
Now it comes howewer problem for more tables for example, the trd table
student_exam with same statement as the first one.
when i now try to create with
create table sampleDB.student_exam(
to1 DATETIME not null,
to2 DATETIME not null,
primary key (datum1),
FOREIGN KEY (to1) REFERENCES student (from1),
FOREIGN KEY (to2) REFERENCES student (to2))


Again, read:
http://dev.mysql.com/doc/mysql/en/in...straints.html:

"In the referenced table, there must be an index where the referenced
columns are listed as the first columns in the same order."

In your student_exam table, the to2 column illegally references
student(from2) (I assume your usage of `to2` as the referenced column is
an error in typing, since no column by that name exists in the student
table).

Anyway, the from2 column does not qualify as the first column in any
index, so you can't make a foreign key reference to it. You can
reference student(from1,from2), or student(from1). The only way you can
reference student(from2) is if you create an additional index with from2
as the first (or only) column.

Regards,
Bill K.
Sep 15 '05 #4

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

Similar topics

8
9212
by: Bri | last post by:
Greetings, I'm having a very strange problem in an AC97 MDB with ODBC Linked tables to SQL Server 7. The table has an Identity field and a Timestamp field. The problem is that when a new record...
14
2104
by: Lars Netzel | last post by:
A little background: I use three Datagrids that are in a child parent relation. I Use Negative Autoincrement on the the DataTables and that's workning nice. My problem is when I Update these...
2
2506
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...
1
1275
by: Grabi | last post by:
Hello. I'm using SQL2000, and I have a design problem. I know that every time I make a primary key, sql server makes it by default a clustered index. Since I have a large composite key in the table...
2
8598
by: clickon | last post by:
I am using ASP.net 2.0 and trying to take advantage of the updated data editing facilities provided through the SQLDataSource control and the DetailsView control. The data is a record from a...
6
4934
by: Jeff North | last post by:
I'm using Microsoft SQL Server Management Studio Express 9.00.2047.00 and expriencing problems with setting referential integrity on a link table. The tables' schema is as follows:...
9
3892
by: sonal | last post by:
Hi all, I hv started with python just recently... and have been assigned to make an utility which would be used for data validations... In short we take up various comma separated data files for...
1
3245
by: rbarber | last post by:
I have to synchronize 2 databases hourly but am having difficulty maintaining foreign key relations. These tables use auto-increment columns as primary keys, with child records in other tables...
6
1409
by: Fritz Franz | last post by:
Hello! I have a table A with fields id,startdate and other fields. id and startdate are in the primary key. In the table B I want to introduce a Foreign key to field id of table A. Is this...
0
7267
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
7391
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
7553
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7120
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
7542
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...
0
5697
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4754
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3235
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
466
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.