I am creating entities and am trying to create composite keys as my primary keys in the data modeler.
However I am not able to do that as I dont know how to. The question is how do I create a composite key in the data modeler.
Here is the snapshot:
here is the composite keys I am trying to create.
Primary key TBD_ID + TBA_STAGE + TBA_ASSIGNEEPNO + TBA_STATUS combination column, Foreign Key
TBD_ID
TBD_ID is the foreign key from T_BPM_Details and I want to add that to T_BPM_Assignee with the following colums to make a composite key as the primary key.
How do I do that
Just toggle the PK checkbox for each of the columns.
Then if you preview the DDL generated, you can see -
CREATE TABLE composite_keys (
key1 INTEGER NOT NULL,
key2 INTEGER NOT NULL,
key3 INTEGER NOT NULL,
description VARCHAR2(256 BYTE)
)
LOGGING;
ALTER TABLE composite_keys
ADD CONSTRAINT composite_keys_pk PRIMARY KEY ( key1,
key2,
key3 );
Related
I have a DB2 (for IBMi) table created as below. I would like to drop the forign key constraint while running in an SQLRPGLE program. Is this possible?
create table grid_action_details(id integer not null
generated always as identity
(start with 1 increment by 1)
PRIMARY KEY,grid_details_id integer,foreign key(grid_details_id)
references grid_details(id),action_code_details_id integer,
foreign key(action_code_details_id)
references action_code_details(id),action_code_status varchar(2),
created_date date default
current_date,created_by varchar(30),
last_updated_date date default current_date,updated_by
varchar(30),required_parameter clob);
I tried the below syntax but it just doesn't seem to work for me:
ALTER TABLE table-name
DROP FOREIGN KEY foreign_key_name
alter table iesqafile.grid_action_details
drop foreign key action_code_details_id
ACTION_CODE_DETAILS_ID in IESQAFILE type *N not found.
You drop the FK constraint via name, not via column.
Since you didn't specify one during create, you'll need to look to see what name the system generated.
Always a best practice to name things yourself.
CONSTAINT <name> FOREIGN KEY (<columns>)
create table grid_action_details (
id integer not null generated always as identity (
start with 1 increment by 1
)
,constraint grid_action_details_pk
primary key
,grid_details_id integer
,constraint grid_details_fk
foreign key (grid_details_id)
references grid_details (id)
,action_code_details_id integer
,constraint action_code_details_fk
foreign key (action_code_details_id)
references action_code_details (id)
,action_code_status varchar(2)
,created_date date default current_date
,created_by varchar(30)
,last_updated_date date default current_date
,updated_by varchar(30)
,required_parameter clob
);
Now you can drop by the known name
alter table iesqafile.grid_action_details
drop foreign key action_code_details_fk
EDIT
To find the generated name use:
the ACS Schema component
DSPFD
SQL against one of the catalog views (QSYS2.SYSCST, SYSIBM.SQLFOREIGNKEYS, SYSIBM.REFERENTIAL_CONSTRAINTS )
I am using Oracle and I'm trying to create foreign key constraints.
Say I have a user table like this:
CREATE TABLE USER (
USER_ID INTEGER GENERATED BY DEFAULT AS IDENTITY,
USERNAME VARCHAR2(30) NOT NULL,
CONSTRAINT USER_PK PRIMARY KEY (USER_ID)
Then I am trying to create table that contains lead_user, delivery_user, and support_user.
I tried both but were not working
1.
CREATE TABLE EXAMPLE (
EXAMPLE_ID INTEGER GENERATED BY DEFAULT AS IDENTITY,
LEAD_USER INTEGER NOT NULL,
DELIVERY_USER INTEGER NOT NULL,
SUPPORT_USER INTEGER NOT NULL,
CONSTRAINT USER_PK PRIMARY KEY (EXAMPLE_ID)
CONSTRAINT FK_USER FOREIGN KEY (LEAD_USER, DELIVERY_USER, SUPPORT_USER) REFERENCES USER(USER_ID)
CREATE TABLE EXAMPLE (
EXAMPLE_ID INTEGER GENERATED BY DEFAULT AS IDENTITY,
LEAD_USER INTEGER NOT NULL,
DELIVERY_USER INTEGER NOT NULL,
SUPPORT_USER INTEGER NOT NULL,
CONSTRAINT USER_PK PRIMARY KEY (EXAMPLE_ID)
CONSTRAINT FK_USER FOREIGN KEY (LEAD_USER, DELIVERY_USER, SUPPORT_USER) REFERENCES USER(USER_ID, USER_ID, USER_ID)
I saw from here
SQLite - Multiple Foreign Keys Referencing the Same Column
that for SQLite, it cannot be done the above way and have to create separate foreign keys such as
CONSTRAINT FK_LEAD_USER FOREIGN KEY (LEAD_USER) REFERENCES USER(USER_ID)
CONSTRAINT FK_DELIVERY_USER FOREIGN KEY (DELIVERY_USER) REFERENCES USER(USER_ID)
CONSTRAINT FK_SUPPORT_USER FOREIGN KEY (SUPPORT_USER) REFERENCES USER(USER_ID)
Does the same applies to Oracle as well?
I am new to here, please help me to find an answer to the below query.
"How to add a primary key constraint on two columns to make a composite key column in a table?"
Instead of writing so many lines, kindly provide the answer as short as possible with code.
(I am creating a table in which I can have only one primary key on the values of two-columns as a composite key.)
JacknJill
You can create such constraint in scope of CREATE TABLE operator
CREATE TABLE foo (
id INTEGER,
name VARCHAR(25),
bar VARCHAR(255),
CONSTRAINT foo_pk PRIMARY KEY (id,name)
);
or ALTER TABLE operator
CREATE TABLE foo (
id INTEGER,
name VARCHAR(25),
bar VARCHAR(255)
);
ALTER TABLE foo ADD CONSTRAINT foo_pk PRIMARY KEY (id,name);
Maybe I'm burnout, but I don't understand this. I have two tables in Oracle: TBL_a and TBL_x. I'm trying to create a foreign key between thos two tables as follows and get error
ORA-02270: no matching unique or primary key.
CREATE TABLE tbl_a (
cod_op integer,
cod_dni char(8),
cod_correl integer,
varchar2(50)
);
CREATE TABLE tbl_x (
cod_op integer,
cod_dni char(8),
blabla varchar2(50)
);
CREATE UNIQUE INDEX TBL_A_PK ON TBL_A (COD_OP);
CREATE UNIQUE INDEX TBL_x_PK ON TBL_x (COD_OP);
ALTER TABLE TBL_a ADD CONSTRAINT TBL_a_R01
FOREIGN KEY (COD_OP) REFERENCES TBL_x (COD_OP);
The problem is that you've created unique INDEXES on your tables, but you didn't create a unique or primary key CONSTRAINT. Oracle requires that the constraints exist in order to establish a foreign key relationship.
If you drop your existing indexes and add the appropriate constraints you can establish your foreign key relationship:
DROP INDEX TBL_A_PK;
DROP INDEX TBL_x_PK;
ALTER TABLE TBL_A
ADD CONSTRAINT UQ_A
UNIQUE(COD_OP)
USING INDEX;
ALTER TABLE TBL_X
ADD CONSTRAINT UQ_X
UNIQUE(COD_OP)
USING INDEX;
dbfiddle here
The table that is referred by the foreign key (here, tbl_x) must have a primary key or a unique constraint.
In your use case, as you are declaring a unique index on cod_op, you could simply make cod_op the primary key of tbl_x instead: that would make the error disappear.
Demo on DB Fiddle
In general, it is a good practice to have a primary key on any table. Extending the principe of turning your unique indexes to primary keys, your DDL statements could be simplified as follows:
CREATE TABLE tbl_x (
cod_op INTEGER PRIMARY KEY,
cod_dni CHAR(8),
blabla VARCHAR2(50)
);
CREATE TABLE tbl_a (
cod_op INTEGER PRIMARY KEY,
cod_dni CHAR(8),
cod_correl INTEGER,
blabla VARCHAR2(50),
CONSTRAINT TBL_a_R01 FOREIGN KEY (COD_OP) REFERENCES TBL_x (COD_OP)
);
Demo on DB Fiddle
How can I specify that a table has multiple columns that makeup the primary key? When I run this sql statement, I get "unknown data type "("
CREATE TABLE SH_LEAGUE_CONTACT_TEAM_ROLE(ROLE_NAME VARCHAR NOT NULL,
TEAM_ID INT NOT NULL,
CONTACT_ID INT NOT NULL,
FOREIGN_KEY(TEAM_ID) REFERENCES SH_LEAGUE_TEAM(ID),
FOREIGN_KEY(CONTACT_ID) REFERENCES SH_LEAGUE_CONTACT(ID),
PRIMARY KEY(ROLE_NAME, TEAM_ID, CONTACT_ID));
You have a typo in your statement, you have used FOREIGN_KEY (one word) instead of FOREIGN KEY (two words).