• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Create primary and foreign key with SQL

 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm new to SQL. I need to create a table that
has a primary key to be used by a CMP entity
bean. I used following to create the table:
create table my_table(id not null number,
val_1 number, val_2 string)
ID is the primary key. Does above SQL statement requires id
to be unique? If not, what should I do to modify
the SQL statement? What should I do to add a
foregin key in above statement?
Thanks so much.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John, to create a Primary key you can use the ALTER command in your existing table by doing the following:
ALTER TABLE my_table ADD CONSTRAINT pk_id PRIMARY KEY (id)
If you are going to DROP the TABLE and start again use this:
CREATE TABLE my_table (
id NUMBER(10,0) NOT NULL,
val_1 NUMBER(4,0) NOT NULL,
val_2 VARCHAR(12) NOT NULL,

CONSTRAINT pk_id
PRIMARY KEY (id));
As far as the FORIEGN KEY is concerned you must reference another table.
Take a look at the following link for references and examples:
http://cisnet.baruch.cuny.edu/holowczak/classes/4300/week9.html
I hope this helps!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic