composite primary key
3 posters
Re: composite primary key
A composite key has more than one attribute (field). In this example we store details of tracks on albums - we need to use three columns to get a unique key - each album may have more than one disk - each disk will have tracks numbered 1, 2, 3...
The primary key must be different for each row of the table. The primary key may not contain a null.
The primary key must be different for each row of the table. The primary key may not contain a null.
thomasdfg- Posts : 11
Join date : 2014-06-18
Age : 39
Location : Hyderabad
Re: composite primary key
When you use the multiple-column
constraint format, you can create a composite key. A composite
key specifies multiple columns for a primary-key or foreign-key
constraint.
The below example creates two tables. The first table has
a composite key that acts as a primary key, and the second table has
a composite key that acts as a foreign key.
CREATE TABLE accounts (
acc_num INTEGER,
acc_type INTEGER,
acc_descr CHAR(20),
PRIMARY KEY (acc_num, acc_type));
CREATE TABLE sub_accounts (
sub_acc INTEGER PRIMARY KEY,
ref_num INTEGER NOT NULL,
ref_type INTEGER NOT NULL,
sub_descr CHAR(20),
FOREIGN KEY (ref_num, ref_type) REFERENCES accounts
(acc_num, acc_type));
In this example, the foreign key of the sub_accounts table, ref_num and ref_type,
references the composite key, acc_num and acc_type,
in the accounts table. If, during an insert or update, you
tried to insert a row into the sub_accounts table whose value
for ref_num and ref_type did not exactly correspond
to the values for acc_num and acc_type in an existing
row in the accounts table, the database server would return
an error.
A referential constraint must have a one-to-one relationship
between referencing and referenced columns. In other words, if the
primary key is a set of columns (a composite key), then the foreign
key also must be a set of columns that corresponds to the composite
key.
Because of the default behavior of the database server,
when you create the foreign-key reference, you do not need to reference
the composite-key columns (acc_num and acc_type) explicitly.
You can rewrite the references section of the previous example as
follows:
FOREIGN KEY (ref_num, ref_type) REFERENCES accounts
constraint format, you can create a composite key. A composite
key specifies multiple columns for a primary-key or foreign-key
constraint.
The below example creates two tables. The first table has
a composite key that acts as a primary key, and the second table has
a composite key that acts as a foreign key.
CREATE TABLE accounts (
acc_num INTEGER,
acc_type INTEGER,
acc_descr CHAR(20),
PRIMARY KEY (acc_num, acc_type));
CREATE TABLE sub_accounts (
sub_acc INTEGER PRIMARY KEY,
ref_num INTEGER NOT NULL,
ref_type INTEGER NOT NULL,
sub_descr CHAR(20),
FOREIGN KEY (ref_num, ref_type) REFERENCES accounts
(acc_num, acc_type));
In this example, the foreign key of the sub_accounts table, ref_num and ref_type,
references the composite key, acc_num and acc_type,
in the accounts table. If, during an insert or update, you
tried to insert a row into the sub_accounts table whose value
for ref_num and ref_type did not exactly correspond
to the values for acc_num and acc_type in an existing
row in the accounts table, the database server would return
an error.
A referential constraint must have a one-to-one relationship
between referencing and referenced columns. In other words, if the
primary key is a set of columns (a composite key), then the foreign
key also must be a set of columns that corresponds to the composite
key.
Because of the default behavior of the database server,
when you create the foreign-key reference, you do not need to reference
the composite-key columns (acc_num and acc_type) explicitly.
You can rewrite the references section of the previous example as
follows:
FOREIGN KEY (ref_num, ref_type) REFERENCES accounts
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Thu Jun 19, 2014 11:31 am by thomasdfg
» How to identify the object when it is dynamically changing behavior
Wed Jun 18, 2014 6:37 pm by thomasdfg
» Patches for QTP 10, 11, 11.5
Wed Jun 18, 2014 6:04 pm by thomasdfg
» Retrieving data from Txt file into datatable
Wed Jun 18, 2014 5:32 pm by thomasdfg
» VB Script to count number of links in a web page.
Wed Jun 18, 2014 4:51 pm by thomasdfg
» Customized HTML reports in QTP
Wed Jun 18, 2014 4:42 pm by thomasdfg
» how to print values into a field of a web page, from a data table in qtp?
Wed Jun 18, 2014 4:37 pm by thomasdfg
» How to delete cookies using QTP?
Wed Jun 18, 2014 4:30 pm by thomasdfg
» When UFT Does Not Identify an Object
Wed Jun 18, 2014 4:23 pm by thomasdfg