Can the same column have primary key & foreign key constraint to another column?
Table1: ID - Primary column, foreign key constraint for Table2 ID Table2: ID - Primary column, Name
Will this be an issue if i try to delete table1 data?
Delete from table1 where ID=1000;
Thanks.
Yes, it can.
You can only have one primary key per table, but multiple unique keys. Similarly, a primary key column doesn't accept null values, while unique key columns can contain one null value each.
You can define keys which allow duplicate values. However, do not allow duplicates on primary keys as the value of a record's primary key must be unique. When you use duplicate keys, be aware that there is a limit on the number of times you can specify the same value for an individual key.
A primary key is a column or a group of columns that uniquely identifies each row in a table. You create a primary key for a table by using the PRIMARY KEY constraint. Each table can contain only one primary key. All columns that participate in the primary key must be defined as NOT NULL .
Assigning Primary Key And Foreign key to the same column in a Table:
create table a1 ( id1 int not null primary key ); insert into a1 values(1),(2),(3),(4); create table a2 ( id1 int not null primary key foreign key references a1(id1) ); insert into a2 values(1),(2),(3);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With