views:

1815

answers:

3
create table d(id numeric(1), code varchar(2))

After I create the above table how can I add a composite primary key on both fields and also a foreign key?

+2  A: 

alter table d add constraint pkc_Name primary key (id, code)

should do it. There's lots of options to a basic primary key/index depending on what DB your working with.

Chris W
create table tr(id numeric(1),bin varchar(10))alter table tr add constraint pk_id primary key (id, bin)its not worked
Domnic
in sql server 2005 that command is noy worked
Domnic
its worked thank u
Domnic
+1  A: 

In Oracle, you could do this:

create table D (
  ID numeric(1),
  CODE varchar(2),
  constraint PK_D primary key (ID, CODE)
);
Simon Nickerson
create table tr(id numeric(1),bin varchar(10))alter table tr add constraint pk_id primary key (id, bin)its not worked why
Domnic
Sir its worked thank u
Domnic
@simonn: just curious, cannot check right now, but is the NOT NULL for the columns implicit, or do you need to add them to the column definitions?
Thilo
A: 

The ALTER TABLE statement presented by Chris should work, but first you need to declare the columns NOT NULL. All parts of a primary key need to be NOT NULL.

Thilo