Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the same column have primary key & foreign key constraint to another column

Tags:

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.

like image 709
Sharpeye500 Avatar asked Sep 23 '10 02:09

Sharpeye500


People also ask

Can a same column be primary and foreign key?

Yes, it can.

Can a primary key be repeated in the same table?

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.

Can primary keys be duplicates?

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.

Can a column be a primary 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 .


1 Answers

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); 
like image 155
Jason Clark Avatar answered Oct 27 '22 06:10

Jason Clark