Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set two field primary key

table1


id1 ==> number

id2 ==> number


id want id1 contain uniqe number and id2 contain unique number.

how can set id1 and id2 primary key?

like image 871
ashkufaraz Avatar asked Nov 05 '11 18:11

ashkufaraz


People also ask

How do you set two columns as primary key?

In Table Designer, click the row selector for the database column you want to define as the primary key. If you want to select multiple columns, hold down the CTRL key while you click the row selectors for the other columns. Right-click the row selector for the column and select Set Primary Key.

Can we make 2 columns as primary key in SQL?

If you're asking if a table can have multiple columns as a primary key, then for MS SQL Server, the answer is yes, and it's called a composite (corrected) key.


1 Answers

In the Table Designer, Select both rows, and then click the Primary Key button in the Toolbar.

OR, open the Query Designer, change the view to SQL View, and type the following code:

ALTER TABLE your_table_name ADD CONSTRAINT your_pk_name PRIMARY KEY(id1, id2);

Then, hit the run button. This will create a Primary key which includes both of those fields.

If you are asking about having an Auto-incrementing integer field in both columns, you can't. You would have to do that programmatically. Access (and most other db's) only allow one "autonumber" field per table.

like image 95
XIVSolutions Avatar answered Sep 21 '22 15:09

XIVSolutions