How can I mapping Tables embeded in spring boot jpa? - spring-boot

i have create two table in sql like this:
create table dbo.person(
id int not null identity(1,1),
code nvarchar(50),
name nvarchar(250),
PRIMARY KEY (Id)
)
create table dbo.employ(
personId int not null,
age smallint,
sex bit,
PRIMARY KEY (personId),
FOREIGN KEY (PersonId) REFERENCES Persons(Id)
)
how can i pmapping this tables in spring boot jpa?

Related

drop foreign key constraint on DB2 table through alter table

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 )

Create table field with foreign key constraint

I want to create a table department:
COLUMN NAME DATATYPE SIZE CONSTRAINT
dept_id number 4 Primary key
prod_id number 4 Foreign key
I tried this:
CREATE TABLE Department(
dept_id number(4) primary key,
prod_id number(4) foreign key);
It shows error. How can I add a foreign key constraint to this table?
A foreign key defines a relationship between your table DEPARTMENT and another table with a primary key. It means, you cannot create a row in DEPARTMENT with a PROD_ID of 1234 unless there is a pre-existing row in the designated parent table with a value of 1234 as its primary key.
So do you have such an existing parent table? if so you need to include its name in the foreign key definition. Otherwise you must create it.
Let's say the parent table is PRODUCT:
create table product (
prod_id number(4) primary key
, name varchar2(32) not null
);
Then you can create DEPARTMENT with a foreign key like this:
CREATE TABLE Department(
dept_id number(4) primary key,
prod_id references PRODUCT );
Yep, that's all the syntax you need: it automatically creates a column PROD_ID with the same datatype and precision as the primary key column of the referenced table. More verbose syntax is available. Read the Oracle SQL documentation to find out more.
I assume that the other table is named other_table_name and that it contains a primary key named prod_id.
CREATE Department (
dept_id number(4) primary key,
prod_id number(4) REFERENCES other_table_name (prod_id)
);
or a different syntax
CREATE Department (
dept_id number(4) primary key,
prod_id number(4)
...
CONSTRAINT fk_prod_id
FOREIGN KEY (prod_id)
REFERENCES other_table_name (prod_id)
);

How to assign foreign key in Oracle 10g?

I am try to assign foreign key in Oracle 10g. But the error shows like
ORA-907: missing right parentheses
I have three tables
TblCustomer
TblProducts
TblSales
create table tblSales
(SalesID int primary key,
ProductId int foreign key references tblProducts(ProductId),
CustomerID int foreign key references tblCustomer(CustomerID),
SalesPrice numeric,
SalesDate date);
Whats going wrong can you suggest me.
You syntax must look like:
create table tblSales (SalesID int,
ProductId int ,
CustomerID int ,
SalesPrice numeric,
SalesDate date,
CONSTRAINT sales_pk PRIMARY KEY (SalesID ),
CONSTRAINT fk_produkt
foreign key (ProductId)references tblProducts(ProductId),
CONSTRAINT fk_customer
foreign key (CustomerID)references tblCustomer(CustomerID)
);
For in inline foreign key you don't specify the foreign key keyword:
create table tblSales
(
SalesID int primary key,
ProductId int references tblProducts(ProductId),
CustomerID int references tblCustomer(CustomerID),
SalesPrice numeric,
SalesDate date
);
With inline foreign keys, you don't even need to specify the target column:
create table tblSales
(
SalesID int primary key,
ProductId int references tblProducts,
CustomerID int references tblCustomer,
SalesPrice numeric,
SalesDate date
);
SQLFiddle example: http://sqlfiddle.com/#!4/420b9c
As a side note: prefixing each and every table with tbl doesn't really make sense. If you are programming, do you prefix every class with Cls or if you are naming a person, do you prefix every name with Pers?

How can I create a sequential SQL creation script for Oracle?

Something like:
create table Employee(
ID int primary key,
Name nvarchar(200)
IDArea int foreign key references Area(ID)
);
go
create table Area(
ID int primary key,
Name nvarchar(200)
);
Does something like this exist in Oracle?
Yes, just leave out the "GO" keywords and put the statements in a file:
create table Area(
ID int primary key,
Name nvarchar2(200)
);
create table Employee(
ID int primary key,
Name nvarchar2(200),
IDArea int references Area(ID)
);
You must create the Area primary key before the foreign key that references it, so I have swapped these around. Also the foreign key syntax is slightly different in Oracle.
You should first create the master table, forget nvarchar datatype, and eventually the script would be :
create table Area(
ID number primary key,
Name varchar2(200)
);
create table Employee(
ID number primary key,
Name varchar2(200) ,
IDArea number, constraint fk_idarea foreign key (idarea) references Area(ID)
);

ROO: how to create composite primary key in Entity

What can I do if I need to create entity for
a table in production DB (Oracle 10g) with composite primary key.
For example:
[CODE]
CREATE TABLE TACCOUNT
(
BRANCHID NUMBER(3) NOT NULL,
ACC VARCHAR2(18 BYTE) NOT NULL,
DATE_OPEN DATE NOT NULL,
DATE_CLOSE DATE,
NOTE VARCHAR2(38 BYTE)
);
CREATE UNIQUE INDEX PK_TACCOUNT ON TACCOUNT
(BRANCHID, ACC);
I don't want to change the structure of this table.
Is it possible to create an "id" field using roo commands?
I use Spring Roo 1.0.2.RELEASE [rev 638]

Resources