Question:
please read details, involves database management problem?
2007-09-12 07:27:01 UTC
have this problem for my database class,anyone know how to get foreign key and explain it to me? Thanks! (PS the bottom is how far I've got)

1. Write a CREATE TABLE statement for the Customer table. Choose data types appropriate for the DBMS used in your course. Note that the CustBal column contains numeric data. The currency symbols are not stored in the database. The CustFirstName and CustLastName columns are required (not null).
CREATE TABLE Customer
( CusNo CHAR (8),
CusFirstName VARCHAR (50) CONSTRAINT CusFirstName Required NOT NULL,
CusLastName VARCHAR (50) CONSTRAINT CusLastName Required NOT NULL,
CusCity VARCHAR (50),
CusState CHAR (2)
CusZip CHAR (10)
CusBal INTEGER
CONSTRAINT PKCustomer PRIMARY KEY (CustNo)
CONSTRAINT FK
Three answers:
Modulus
2007-09-12 08:00:45 UTC
I don't know which DBMS you're approaching this from, looks like it could be Oracle (?) but I'm not sure, but here's some basic thoughts...



A foreign key is essentially a primary key in another table. For example, if you had a parking registration database, one table could be for Vehicles where the primary key was Vehicle Type (CR for Car, BK for Bike, etc). If you had another table, for example Customers, that stored people who could park in the garage, if you used Vehicle Type to indicate what kind of vehicle the customer had, Vehicle Type would be a foreign key.



Usually you "connect" foreign keys to primary keys to enforce referential integrity- in other words, you are trying to restrict what people can enter in that field. For Vehicle Type, for instance, you may not want people typing in MO for Motorcycle if it doesn't exist in the Vehicle table, or even doing something as simple as making a typo (CT instead of CR for Car), so that is why you connect the foreign key of one table to its primary key counterpart in the other table.



For your database problem, one of the fields in your Customer table references a primary key in another table, so you need to write a constraint statement to connect it to that primary key. In Oracle, the syntax would be something like...



CONSTRAINT constraint_name FOREIGN KEY (field in Customer table) REFERENCES table_with_primary_key (field in primary key table)



using CusBal as an example if you had a second table called Accounts



CONSTRAINT cusbal_fk FOREIGN KEY (CusBal) REFERENCES Accounts (CusBal)



Hope this helps. If you are using Oracle, syntax example in my sources below.
gamcitic
2007-09-12 12:13:27 UTC
T1 is the parent table

T2 is the child table



T1.rowid is the primary key of T1

T2.T1_rowid is the foreign key of T2 for T1 (T1.rowid is its primary key)



You can insert any rows to T1

In order to insert a row to T2, the value of T2.T1_rowid(the new inserting row) must exists in T1.rowid



When deleting/update a row in T1, you have a few options about what to do in T2 that T2.T1_rowid=T1.rowid:

No Action - if the T1.rowid exists in T2.T1_rowid, failed and do nothing.

Cascade - delete all rows in T2

Set Null - set the T2.T1_rowis to null
2016-12-13 12:04:33 UTC
expensive Asker, Wow it seems such as you have a huge difficulty on your palms! tell your BFF each and all the failings X has carried out to you and make her comprehend. tell your BFF that X is purely utilising her and taking benefit of her,if she's a real BFF she'll have faith you! as quickly as you and your BFF fix issues out,think of of a thank you to eliminate X! i'm hoping I helped!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...