Sunday, March 11, 2012
Assigning FOREIGN KEY, which method is correct?
I am real new to SQL and just like to clear up some questions regarding to assigning FOREIGN KEY.
Are there any differences in assigning foreign keys this way:
CREATE TABLE NURSE
(EMP_ID VARCHAR2(8) CONSTRAINT fk_nurse_emp REFERENCES EMPLOYEE(EMP_ID),
WARD_ID VARCHAR2(8) CONSTRAINT fk_nurse_ward REFERENCES WARD(WARD_ID),
CONSTRAINT pk_nurse PRIMARY KEY(EMP_ID, WARD_ID));
and this way:
CREATE TABLE NURSE
(EMP_ID VARCHAR2(8),
WARD_ID VARCHAR2(8),
CONSTRAINT fk_nurse_emp FOREIGN KEY(EMP_ID) REFERENCES EMPLOYEE(EMP_ID),
CONSTRAINT fk_nurse_ward FOREIGN KEY(WARD_ID) REFERENCES WARD(WARD_ID),
CONSTRAINT pk_nurse PRIMARY KEY(EMP_ID, WARD_ID));
What are the differences between them?
Many thanks.No difference - except that you couldn't create a composite foreign key with the first method. They are just alternatives.|||Thank you for clearing this up for me andrewst.
Another question, might not be all that important, but which is the preferred method?
I guess the second method is more preferred, as it clearly identifies the FOREIGN KEYS, but it takes more lines. I just like to learn the basic conventions first and stick to it.|||Well it's really a matter of personal taste and/or company policy. But since the second syntax works for all foreign keys and the first works only for single-column foreign keys, the second syntax could be preferred.
Assigning Foreign Key To New SQL Server Table
I am creating new SQL Server Tables using SQL Server 2005. I have set
primary key to the tables .But I do not know how to assign Foreign key
to the tables .I need to do some joins later and that is why I have to
put Foreign key to the table . The Primary key is visible and can be
assigned easily .But How do I assign foreign key .
Thanks
*** Sent via Developersdex http://www.developersdex.com ***.. . (kmandal@.sark.com) writes:
> I am creating new SQL Server Tables using SQL Server 2005. I have set
> primary key to the tables .But I do not know how to assign Foreign key
> to the tables .I need to do some joins later and that is why I have to
> put Foreign key to the table . The Primary key is visible and can be
> assigned easily .But How do I assign foreign key .
First of all, questions about SQL 2005 are best asked in the SQL 2005
newsgroups, as these are monitored by the SQL Server deverlopers. Access
info here: http://go.microsoft.com/fwlink/?linkid=31765
As for your question, the syntax is as in this example:
ALTER TABLE tbl ADD CONSTRAINT fk_myforeignley (col1, col2)
REFERENCES othertbl (col1, col2)
Or were you using the table designer? I recommend that you learn the
syntax to create table from SQL statements. In the long run that will
make you more effective and productive, than clicking around in the
table designer. Also, there are several *serious* bugs in the table
designer when it comes to modify existing tables, so the less you use
it, the better.
(If you are dead set on it, I believe that if you right-click there
are foreign keys in the context menu.)
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||
Hi
Thanks for your reply . It worked and I was able to assign Foreign key
to the tables . Thanks again .
*** Sent via Developersdex http://www.developersdex.com ***|||Foreign keys will definitely help with relational integrity. However,
for the purposes of joining the foreign key need not be pre-defined.
That is what the join syntax in the select statement is for.
Sunday, February 19, 2012
ASP/SQL Database question
Is there anyway of putting a prefix on an primary key field? I'll try explain with an example.
tblCodes
CodeID [PK] - Integer
CodeName
CodeDesc
CodeType
When a new code is created the ID is simply the next value as you would expect. To help with identifying the codes in my actual application, I would like the ID to be based on the CodeType.
For example: There are four types of code (red, green, blue, orange), if when creating a new code the user selects the type red, the CodeID will be "RED\1". If another is made using the type red, it will become "RED\2". The same applied the the others, a green code will have a prefix of "GREEN\" which increments.
Really not sure how to go about doing this, maybe a seperate table for CodeTypes is needed? I'm a novice programmer and i'm also new to SQL to please to be gentle!
There is and only needs to be 4 code types, if that's any help.
To give a bit more information on the reasoning for wanting the prefix on the CodeID.
Scenario
A user is inputting the amount of time he has spent on a code. There is a drop down value which he must select the CodeID from. At the moment there is no distinguishing between the CodeTypes, so he will just see 1, 2, 3 ,4 ,5.
If I can do what i'm wanting, the user will see GREEN\1, GREEN\2, RED\1, RED\2 and so on. Making it a fair bit more user friendly.
Any ideas?
I would suggest using a separate field to store the CodeType (Red, Green etc) in addition to the ID.
This will give you the flexibility of using it in different ways e.g. concatenate in sql statement to generate strings like "Green/1" or use it separately to say group by CodeType.
|||Ideally, you need to create another table to store user/CodeType pair to deal with Many-To-Many relationship. The table at least includes two columns: UserID and CodeId.
|||I don't really see why you are saying I need to have UserID in there at all.
I'm still not sure how to do this :(
It's only really needed for presentation reasons. So the actual value doesn't need to be stored as GREEN\1.
For example, I want the drop down menu to show GREEN\1 or RED\12 simply to make it easier for the user to tell what time of code his is picking, rather then all the codes looking the same but just with different numbers (1, 2, 3, 4, 5, 6).
Any ideas?