473,386 Members | 1,706 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,386 software developers and data experts.

Relationship Problem

Hello all,

OK - my problem is that my relationship(s) just doesn't seem to work.
I am currently using Microsoft SQL Server 2005, and working through the management studio. For my site coding, I'm using Coldfusion.
So you can expect that I don't know an awful lot of actual SQL code, and instead am relying on the interface.

I have already achieved making a fully functional registeration and login page for my site, so there's no hiccups there :)
But when I insert a new user to my "users" table, the userid doesn't seem to appear my other table; "characters". I should probably note that by characters, I mean personalities/people - not actual text characters or whatnot.

Here's the low-down on my current DB layout:
My first table is "users" and has the columns "userid", "username", "password", and "emailaddress". The userid column is a PK with identity specification. It is also an int datatype, while the other columns are varchar(20)s. Also, the userid doesn't allow nulls, obviously.

My second table is "characters" and has several columns, mostly of the same datatypes as the "users" table - so it's not relevant. The two important columns are "characterid" and "userid" - both are int datatypes and do not allow nulls. The characterid column serves as the PK and has identity specification.


Now comes the problem...; using a database diagram, I have created a relationship between "userid" from "users", and "userid" from "characters". The "userid" in "characters, in theory, is the FK.
Right. So I register myself on the site using a simple insert sql query (actually, I use <cfinsert>) - the insert goes directly into "users" with a username, password, and email address. A unique number is created in userid, as you'd expect... but nothing happens in "characters" at all!
How do I get the userid to pass on from "users" into "characters"?

Well crap, this post is extremely long now. I only hope it's understandable, and that someone can help me :(

Thanks a lot in advance!
Aiden
Oct 8 '07 #1
5 1259
iburyak
1,017 Expert 512MB
FK is a logical object and not physical that can insert record for you.
You have to create a second insert statement into a ‘characters’ table but getting newly created userid first.


Good Luck.
Oct 8 '07 #2
Hmm, thanks for the reply..

I think I tried doing that already.
I first used the insert sql statement and inserted a username, password, and email address into the "users" table - I can confirm that this was successful. That then created the userid automatically.
Then, another insert sql statement was ran straight after, which also inserted information into fullname, gender, and class in the "characters" table - this also was successful. The characterid was successfully added automatically, but nothing happened in the userid column (in "characters").

Am I going about this right?

More on the subject... I was under the impression that relationships were used to automatically make specific entries from a column in one table, copy over to a similar column in another table - therefore making queries from different tables always follow the same userid (for example). This would then make sure the site user always views only his or her row information, no matter the table queried.
Oct 8 '07 #3
iburyak
1,017 Expert 512MB
You have a wrong impression.
In second insert statement instead of userid use following statement
Insert into characters( "userid", …… ) values( (select userid from users where username = "username" and password = "password" and emailaddress = "emailaddress"), other columns here)
So my point is you can use select statement instead of userid.
FK works like this:
If you would try entering some userid into characters table that does not exist in users table it will give you an error.
I would recommend creating unique index on users table over (username, password, emailaddress) columns so you wouldn’t have duplicates which might cause problem at insert time. If you want to have multiple records for the same user then you can change your insert statement to this:
Insert into characters( "userid", …… ) values( (select max(userid) from users where username = "username" and password = "password" and emailaddress = "emailaddress"), other columns here)

Good Luck.
Oct 8 '07 #4
I understand now :) I guess I was expecting something more automated with the use of relationships.

But I still don't entirely understand the point in having a relationship.
Even without the relationship, I can still add the userid from "users" into "characters" in the way that you described. So long as I have an identity specification on my userid in "users", there will never be an error related to a duplicate userid.
EDIT: I could also use code to check that the userid is currently available and exists from "users", before trying to create it within "characters", thus eliminating the need for a relationship? I think?...

That aside, your solution has been very helpful to me :) I can finally continue with the rest of my site!
Thanks
Aiden
Oct 8 '07 #5
iburyak
1,017 Expert 512MB
select userid from users where username = "username" and password = "password" and emailaddress = "emailaddress"

Statement above can return 2 different userids it will be generated for you but it will be escentially the same user. If you have identity column you can use @@IDENTITY global variable in the next statement instead of select statement above.

Foreign key just to make sure primary key exists in a parent table.

Godd Luck.
Oct 8 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Larry R Harrison Jr | last post by:
I have an Access 97 database with 2 tables that have a one-many relationship. I have a SQL statement in the "one" table which I want to execute and insert 7 records into the "many" table, and I...
2
by: Marco Simone | last post by:
Hi, I have 4 tables, tblCompany, tblA, tblB and tblC. tblA, tblB and tblC contain same type of data, they should be in one table, but since there are many fields, I split it into 3 tables. Each...
3
by: lorirobn | last post by:
Hello, I have a report which uses a subreport. When I run the report, I get "Enter Parameter Value" error message for "tblGuestRoom". I click ok and the report seems to work fine. I...
3
by: musosdev | last post by:
Hi guys I've got the following error on a project which is running locally on a vs2005 machine (built in webserver), trying to connect to my win2k3 server active directory. the error is... ...
5
by: BrianDP | last post by:
This is a strange one-- I have an Access 2K database with a split front end/back end. There are two tables in the back end - RFile and RLine. There is a one to many relationship from...
7
by: Ron | last post by:
Hi All, Using Access2000, winXP. Table 1 = tblClients displayed on frmClients via qryClients. 2nd table = tblInvoices shown on frmInvoices via qryInvoices. 2nd table = tblDetails shown on...
2
by: uarana | last post by:
Hi All, I've been plugging away at this problem for a while now and was wondering if anyone had an idea they could share. I'm working on a document management database that tracks revisions of...
10
by: Richard | last post by:
Hi folks, thanks for taking the time to read this (and hopefully point our where I'm going wrong). The scenario: I have a local Access2007 database which links in several read only mySql...
11
by: RobertJohn | last post by:
Hi all I am using Access 2007 to start a small home library application, and so far it has two tables. 1. Books, with fields Book_ID (Primary Key) and Title, and 2. Authors, with fields...
2
by: paulcjcross | last post by:
How to set up a many to many relationship. I know you need a third table to join the other two. I need one table (table1) with stock_numbers(unique), one table (table2) with job-numbers(unique) and...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
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...
0
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
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.