Computer Webmaster Gaming Console Graphics Forum

Welcome to the Computer Webmaster Gaming Console Graphics Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

MK PitStop Main Earn $25 Earn Money Posting Extras Members Blogs Image Hosting User Pages
Go Back   Computer Webmaster Gaming Console Graphics Forum > Webmaster Forum > Website Coding > Database
Register FAQ/Rules Become A V.I.P. Member Search Today's Posts Mark Forums Read

Database Database problems or need to ask a question? maybe something to do with sql injections or a database software question. Database topics cover MySQL, PostgreSQL, Oracle, SQL Server or anything else related to databases.

Google
Reply
 
LinkBack Thread Tools Display Modes
Old 07-01-2007, 11:02 PM   #1
TheKeith
 
TheKeith's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default newbie question--1 table's columns to link to other tables' primary keys

I'm new to databases, so if my question doesn't make any sense, don't
hold it against me. I have three tables--one stores user information
(username, password, email, etc), another stores a list of songs--this
table uses a 2-column primary key. One of the primary key columns is
of DATETIME type and the other is a SMALLINT AUTO_INCREMENT type. My
third table stores information about each listen--by that I mean a new
record is made everytime a user listens to a song, and in that record,
is the user and the song, so naturally there would only be two columns
in this table, namely User (corresponding to only those users in the
user table) and Song (corresponding to the primary key columns in the
songs table).

My question is this. How can I link the two columns in my "listens"
table to the primary keys of the other two table, so that when and if
a user should be deleted from teh "user" table, all listens by that
user in the "listens" table would get deleted; also if any song should
get deleted from the "songs" table, all listens of that song by any
user, would get deleted from the "listens" table?

BTW, I'm using mysql 5 on godaddy web hosting.

Did this make sense? I'm not sure if I need a foreign key or what, and
if so, how do I set that up? Help would be greatly appreciated--thanks.

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Advertisements
Old 07-01-2007, 11:02 PM   #2
strawberry
 
strawberry's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default newbie question--1 table's columns to link to other tables' primary keys

On Jun 17, 6:22 pm, TheKeith <ksd...@gmail.com> wrote:
> I'm new to databases, so if my question doesn't make any sense, don't
> hold it against me. I have three tables--one stores user information
> (username, password, email, etc), another stores a list of songs--this
> table uses a 2-column primary key. One of the primary key columns is
> of DATETIME type and the other is a SMALLINT AUTO_INCREMENT type. My
> third table stores information about each listen--by that I mean a new
> record is made everytime a user listens to a song, and in that record,
> is the user and the song, so naturally there would only be two columns
> in this table, namely User (corresponding to only those users in the
> user table) and Song (corresponding to the primary key columns in the
> songs table).
>
> My question is this. How can I link the two columns in my "listens"
> table to the primary keys of the other two table, so that when and if
> a user should be deleted from teh "user" table, all listens by that
> user in the "listens" table would get deleted; also if any song should
> get deleted from the "songs" table, all listens of that song by any
> user, would get deleted from the "listens" table?
>
> BTW, I'm using mysql 5 on godaddy web hosting.
>
> Did this make sense? I'm not sure if I need a foreign key or what, and
> if so, how do I set that up? Help would be greatly appreciated--thanks.


Have a read through this:

http://dev.mysql.com/doc/refman/5.0/...nstraints.html

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 07-01-2007, 11:02 PM   #3
TheKeith
 
TheKeith's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default newbie question--1 table's columns to link to other tables' primary keys


> Have a read through this:
>
> http://dev.mysql.com/doc/refman/5.0/...ey-constraints...




Hey thanks! So I've followed all the instructions, but for some reason
I'm getting a:

ERROR 1005 (HY000): Can't create table './keithsdb/
kmfnlistens.frm' (errno: 150)

when creating the referencing table, with this syntax:

create table kmfnListens (listener varchar(20) not null, kmfnSongID
bigint not null auto_increment, index (kmfnSongID), foreign key
(kmfnSongID) references kmfnSongs(songID) on delete cascade on update
cascade) engine=innodb;

I tried creating the table without the foreign key clause, and it
works. But I can't get the foreign key to work. After creating the
table without it, I issued a:

alter table kmfnListens foreign key kmfnSongID references
kmfnSongs(songID) on delete cascade on update cascade;

but got an error 1064 syntax error--what am I doing wrong? The
referenced table.column is kmfnSongs.songID and that column is its
primary key, so I'm stumped! Help! thanks.

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 07-01-2007, 11:02 PM   #4
Captain Paralytic
 
Captain Paralytic's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default newbie question--1 table's columns to link to other tables' primary keys

On 18 Jun, 15:50, TheKeith <ksd...@gmail.com> wrote:
> > Have a read through this:

>
> >http://dev.mysql.com/doc/refman/5.0/...ey-constraints...

>
> Hey thanks! So I've followed all the instructions, but for some reason
> I'm getting a:
>
> ERROR 1005 (HY000): Can't create table './keithsdb/
> kmfnlistens.frm' (errno: 150)
>
> when creating the referencing table, with this syntax:
>
> create table kmfnListens (listener varchar(20) not null, kmfnSongID
> bigint not null auto_increment, index (kmfnSongID), foreign key
> (kmfnSongID) references kmfnSongs(songID) on delete cascade on update
> cascade) engine=innodb;
>
> I tried creating the table without the foreign key clause, and it
> works. But I can't get the foreign key to work. After creating the
> table without it, I issued a:
>
> alter table kmfnListens foreign key kmfnSongID references
> kmfnSongs(songID) on delete cascade on update cascade;
>
> but got an error 1064 syntax error--what am I doing wrong? The
> referenced table.column is kmfnSongs.songID and that column is its
> primary key, so I'm stumped! Help! thanks.


According to your first post:
"One of the primary key columns is of DATETIME type and the other is a
SMALLINT AUTO_INCREMENT type"
But in the above you have "kmfnSongID bigint not null auto_increment".

The joining fields must be identical.

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 07-01-2007, 11:02 PM   #5
TheKeith
 
TheKeith's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default newbie question--1 table's columns to link to other tables' primary keys

On Jun 18, 11:23 am, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> On 18 Jun, 15:50, TheKeith <ksd...@gmail.com> wrote:
>
>
>
> > > Have a read through this:

>
> > >http://dev.mysql.com/doc/refman/5.0/...ey-constraints...

>
> > Hey thanks! So I've followed all the instructions, but for some reason
> > I'm getting a:

>
> > ERROR 1005 (HY000): Can't create table './keithsdb/
> > kmfnlistens.frm' (errno: 150)

>
> > when creating the referencing table, with this syntax:

>
> > create table kmfnListens (listener varchar(20) not null, kmfnSongID
> > bigint not null auto_increment, index (kmfnSongID), foreign key
> > (kmfnSongID) references kmfnSongs(songID) on delete cascade on update
> > cascade) engine=innodb;

>
> > I tried creating the table without the foreign key clause, and it
> > works. But I can't get the foreign key to work. After creating the
> > table without it, I issued a:

>
> > alter table kmfnListens foreign key kmfnSongID references
> > kmfnSongs(songID) on delete cascade on update cascade;

>
> > but got an error 1064 syntax error--what am I doing wrong? The
> > referenced table.column is kmfnSongs.songID and that column is its
> > primary key, so I'm stumped! Help! thanks.

>
> According to your first post:
> "One of the primary key columns is of DATETIME type and the other is a
> SMALLINT AUTO_INCREMENT type"
> But in the above you have "kmfnSongID bigint not null auto_increment".
>
> The joining fields must be identical.



Ah yeah, I neglected to mention that I changed teh structure of the
referenced tables so that each one only has one primary key column--
because I wasn't able to convert that table to innoDB as it was with
the two-column primary key, one of which was auto-increment.

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 07-01-2007, 11:02 PM   #6
TheKeith
 
TheKeith's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default newbie question--1 table's columns to link to other tables' primary keys

On Jun 18, 12:12 pm, TheKeith <ksd...@gmail.com> wrote:
> On Jun 18, 11:23 am, Captain Paralytic <paul_laut...@yahoo.com> wrote:
>
>
>
> > On 18 Jun, 15:50, TheKeith <ksd...@gmail.com> wrote:

>
> > > > Have a read through this:

>
> > > >http://dev.mysql.com/doc/refman/5.0/...ey-constraints...

>
> > > Hey thanks! So I've followed all the instructions, but for some reason
> > > I'm getting a:

>
> > > ERROR 1005 (HY000): Can't create table './keithsdb/
> > > kmfnlistens.frm' (errno: 150)

>
> > > when creating the referencing table, with this syntax:

>
> > > create table kmfnListens (listener varchar(20) not null, kmfnSongID
> > > bigint not null auto_increment, index (kmfnSongID), foreign key
> > > (kmfnSongID) references kmfnSongs(songID) on delete cascade on update
> > > cascade) engine=innodb;

>
> > > I tried creating the table without the foreign key clause, and it
> > > works. But I can't get the foreign key to work. After creating the
> > > table without it, I issued a:

>
> > > alter table kmfnListens foreign key kmfnSongID references
> > > kmfnSongs(songID) on delete cascade on update cascade;

>
> > > but got an error 1064 syntax error--what am I doing wrong? The
> > > referenced table.column is kmfnSongs.songID and that column is its
> > > primary key, so I'm stumped! Help! thanks.

>
> > According to your first post:
> > "One of the primary key columns is of DATETIME type and the other is a
> > SMALLINT AUTO_INCREMENT type"
> > But in the above you have "kmfnSongID bigint not null auto_increment".

>
> > The joining fields must be identical.

>
> Ah yeah, I neglected to mention that I changed teh structure of the
> referenced tables so that each one only has one primary key column--
> because I wasn't able to convert that table to innoDB as it was with
> the two-column primary key, one of which was auto-increment.




AHH! -- I got it working finally. I'm not sure what I did right this
time though.

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 07-01-2007, 11:03 PM   #7
lark
 
lark's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default newbie question--1 table's columns to link to other tables' primary keys

== Quote from TheKeith (ksdump@gmail.com)'s article
> On Jun 18, 12:12 pm, TheKeith <ksd...@gmail.com> wrote:
> > On Jun 18, 11:23 am, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> >
> >
> >
> > > On 18 Jun, 15:50, TheKeith <ksd...@gmail.com> wrote:

> >
> > > > > Have a read through this:

> >
> > > > >http://dev.mysql.com/doc/refman/5.0/...ey-constraints...

> >
> > > > Hey thanks! So I've followed all the instructions, but for some reason
> > > > I'm getting a:

> >
> > > > ERROR 1005 (HY000): Can't create table './keithsdb/
> > > > kmfnlistens.frm' (errno: 150)

> >
> > > > when creating the referencing table, with this syntax:

> >
> > > > create table kmfnListens (listener varchar(20) not null, kmfnSongID
> > > > bigint not null auto_increment, index (kmfnSongID), foreign key
> > > > (kmfnSongID) references kmfnSongs(songID) on delete cascade on update
> > > > cascade) engine=innodb;

> >
> > > > I tried creating the table without the foreign key clause, and it
> > > > works. But I can't get the foreign key to work. After creating the
> > > > table without it, I issued a:

> >
> > > > alter table kmfnListens foreign key kmfnSongID references
> > > > kmfnSongs(songID) on delete cascade on update cascade;

> >
> > > > but got an error 1064 syntax error--what am I doing wrong? The
> > > > referenced table.column is kmfnSongs.songID and that column is its
> > > > primary key, so I'm stumped! Help! thanks.

> >
> > > According to your first post:
> > > "One of the primary key columns is of DATETIME type and the other is a
> > > SMALLINT AUTO_INCREMENT type"
> > > But in the above you have "kmfnSongID bigint not null auto_increment".

> >
> > > The joining fields must be identical.

> >
> > Ah yeah, I neglected to mention that I changed teh structure of the
> > referenced tables so that each one only has one primary key column--
> > because I wasn't able to convert that table to innoDB as it was with
> > the two-column primary key, one of which was auto-increment.

> AHH! -- I got it working finally. I'm not sure what I did right this
> time though.


from the error code you gave, it appears that the key constraint (foreign key) was
incorrectly formed just like captain p mentioned if they are not of the same kind,
it won't work.
--
POST BY: lark with PHP News Reader
 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Featured Websites
Free Space
Free Space
Free Space Free Space
Reply
Tags: , , , , , , ,




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
n00b DB design question: Gigantic tables Kevin McCarthy Database 8 07-01-2007 11:01 PM
Autoincremented id with two primary keys Alex Database 2 07-01-2007 7:35 PM
Newbie - FM Modulator / Broadcaster to link MP3 player toexisting car radio Mark Faithfull Car audio 0 06-17-2007 3:13 PM
docs about access tables, operation tables, volume tables BradPitt Database 0 05-31-2007 8:42 PM
Keys into keys in template flexy Jandro brozaman Pear 1 05-20-2007 6:34 PM


Featured Websites




All times are GMT +1. The time now is 3:20 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.0.0
Cheap Computers
MK PitStop Copyright 2005 - 2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98