conditional primary key based on value not being null - sql

I have a table that has a composite primary key:
CREATE TABLE tb (
it_service VARCHAR2(200),
hostname VARCHAR2(255),
sw_name VARCHAR2(200 CHAR),
product_home VARCHAR2(500),
product_home_ods VARCHAR2(500),
instance_name VARCHAR2(255),
eg_status VARCHAR2(10),
);
ALTER TABLE tb ADD (
CONSTRAINT pk_tb PRIMARY KEY ( hostname,
instance_name,
product_home,
product_home_ods,
it_service,
sw_name )
);
The problem is that when I insert into this table, the data from the product_home column is null for the rows where product_home_ods has data. The same applies product_home_ods, the rows where that table contain data are null for product_home.
Example:
product_home product_home_ods
java null
python null
null windows
null windows_server
Is it possible to create a primary key based on the condition of these columns ? I understand a primary key should not contain null values, but unsure about the best way to tackle this problem.

And changing to this model ?
product_home will never be NULL and ods_flag will indicate if it should be interpreted a product_home or product_home_ods, something you can do in a view to get back the same data as your original table.
CREATE TABLE tb (
it_service VARCHAR2(200),
hostname VARCHAR2(255),
sw_name VARCHAR2(200 CHAR),
product_home VARCHAR2(500),
ods_flag NUMBER(1,0),
instance_name VARCHAR2(255),
eg_status VARCHAR2(10),
);
ALTER TABLE tb ADD (
CONSTRAINT pk_tb PRIMARY KEY ( hostname,
instance_name,
product_home,
ods_flag,
it_service,
sw_name )
);
(As long as product_home and product_home_ods of your original table are not foreign keys)

Related

ORA-00907: "missing right parenthesis"

First of all, I apologize for asking the same question again but the information on the old topics did not work in my oraclesql code and secondly there may be some syntax errors, I learned mysql in school last semester, but now we are learning oraclesql with old mysql information in the lesson, so i am a bit confused.
So my question is i want to create 3 related table (movie-casts-actor) but i am getting missing right parenthesis. trying to connect casts table to movie table with foreign key and actor table to casts table. what am i doing wrong? how spagetti is it?
CREATE TABLE Movie (
movie_id NUMBER(7) NOT NULL,
movie_name varchar2(50) NOT NULL ,
movie_director varchar2(50) NOT NULL ,
movie_year INT(4) NOT NULL ,
movie_duration INT(3) ,
movie_language varchar2(15) ,
movie_rating number(4,2) ,
PRIMARY KEY(movie_id),
CONSTRAINT is_unique UNIQUE(movie_id),
CONSTRAINT movie_id_checker CHECK(movie_id>0 and movie_id<=9999999)
);
CREATE TABLE Casts (
movie_id_fk INT(7) FOREIGN KEY REFERENCES Movie(movie_id) ,
cast_id INT(7) NOT NULL,
actor_fullname varchar2(50) NOT NULL ,
actor_role varchar2(50) ,
PRIMARY KEY(cast_id),
CONSTRAINT is_unique UNIQUE(cast_id),
CONSTRAINT cast_id_checker CHECK(cast_id>0 and cast_id<=9999999)
);
CREATE TABLE Actor (
cast_id_fk INT(7) FOREIGN KEY REFERENCES Casts(cast_id) ,
actor_id INT(7) NOT NULL,
actor_name CHAR(30) ,
actor_surname CHAR(25) ,
actor_gender CHAR(5) ,
actor_age CHAR(3) CONSTRAINT real_age_check CHECK(actor_age>0 AND actor_age<=150),
PRIMARY KEY(actor_id),
CONSTRAINT is_unique UNIQUE(actor_id),
CONSTRAINT actor_id_checker CHECK(actor_id>0 and actor_id<=9999999)
)
The errors are:
INT or NUMBER(4,0) and not INT(4)
You cannot have a unique key and a primary key on the same column.
An inline foreign key just need the REFERENCES keyword and does not need FOREIGN KEY.
Other issues:
You probably don't want actor name/surname as fixed-length CHAR strings and want to us variable-length VARCHAR2.
You probably want gender to be a code from a fixed list (which can be as long or as short as you find appropriate to describe the actors) rather than as a string.
You probably don't want to have an AGE column as that will be out of date as soon as the first birthday of an actor occurs; instead have a DATE_OF_BIRTH column that is a DATE data type and then you can calculate the age as and when necessary.
Using the table name as a prefix for every column is a waste of key strokes; you would be better to just name the columns for what they are without the prefix. Similar with fk as a suffix.
If you are using a NUMBER(7,0) for id values then you don't need to check that it is less than or equal to 9999999 as it is impossible to be a greater value; however, you can have zero or negative values so the check constraint for the lower bound may still be valid.
If you are using Oracle 12c or later then you should probably be using an IDENTITY column for the id values unless you are taking the id values from a 3rd party.
CREATE TABLE Movie (
id NUMBER(7,0) NOT NULL,
name varchar2(50) NOT NULL ,
director varchar2(50) NOT NULL ,
year NUMBER(4,0) NOT NULL ,
duration NUMBER(3,0),
language varchar2(15) ,
rating number(4,2) ,
PRIMARY KEY(id),
CONSTRAINT movie_id_checker CHECK(id>0)
);
CREATE TABLE Casts (
movie_id INT REFERENCES Movie(id) ,
id NUMBER(7,0) NOT NULL,
fullname varchar2(50) NOT NULL ,
role varchar2(50) ,
PRIMARY KEY(id),
CONSTRAINT cast_id_checker CHECK(id>0)
);
CREATE TABLE Actor (
cast_id NUMBER(7,0) REFERENCES Casts(id) ,
id NUMBER(7,0) NOT NULL,
name VARCHAR2(30) ,
surname VARCHAR2(25) ,
gender CHAR(1)
CHECK ( gender IN ( 'M', 'F', 'X', 'Y', 'Z' ) ),
date_of_birth DATE
CONSTRAINT real_age_check CHECK(date_of_birth >= DATE '1870-01-01' ),
PRIMARY KEY(id),
CONSTRAINT actor_id_checker CHECK(id>0)
);
db<>fiddle here

connecting tables in sql apex?

hi i created tables using sql :-
create tables
create table customer (
cust_id number not null constraint customer_id_pk primary key,
cust_first varchar2(4000),
cust_last varchar2(4000),
cust_city varchar2(255),
)
;
create table medicine (
med_id number not null constraint medicine_id_pk primary key,
med_name varchar2(255),
med_info varchar2(4000),
med_prise number
)
;
create table the_order (
order_id number not null constraint the_order_id_pk primary key,
order_date date,
order_buyer varchar2(4000),
order_med varchar2(4000)
)
;
how do i connect the tables together into the order table i am using Apex to make a project.
i know its about foreign key but in the apex platform to show the forms as a report on one big table ?
Defining foreign keys is something done on the database, for a number of good reasons.
Your queries will work without them, but won't perform as well, and you could get dirty data over time.
It's all about the SQL. APEX is just a conduit to get the data from the database into a web page in front of the user's eyes.
You have a table design issue.
Provided you follow their best practices, it should figure out foreign keys and indexes for you.
Example
Quick SQL
customers
first_name /nn
last_name /nn
city
medicines
name /nn
info
price num /nn
orders
date /nn
customer_id /nn
order_lines
medicine_id /nn
medicine_name /nn
medicine_price num /nn
quantity num /nn
total num /nn
Generated SQL
-- create tables
create table customers (
id number generated by default on null as identity
constraint customers_id_pk primary key,
first_name varchar2(255 char) not null,
last_name varchar2(255 char) not null,
city varchar2(255 char)
)
;
create table medicines (
id number generated by default on null as identity
constraint medicines_id_pk primary key,
name varchar2(255 char) not null,
info varchar2(4000 char),
price number not null
)
;
create table orders (
id number generated by default on null as identity
constraint orders_id_pk primary key,
customer_id number
constraint orders_customer_id_fk
references customers on delete cascade not null,
the_date date not null
)
;
-- table index
create index orders_i1 on orders (customer_id);
create table order_lines (
id number generated by default on null as identity
constraint order_lines_id_pk primary key,
order_id number
constraint order_lines_order_id_fk
references orders on delete cascade,
medicine_id number
constraint order_lines_medicine_id_fk
references medicines on delete cascade not null,
medicine_name varchar2(255 char) not null,
medicine_price number not null,
quantity number not null,
total number not null
)
;
-- table index
create index order_lines_i1 on order_lines (medicine_id);
create index order_lines_i2 on order_lines (order_id);

Trying to do an assignment but stuck at ORA-00906: missing left parenthesis

Im doing a course on DB, this is one of the first assignments, I'm about to be done, but there is an issue somewhere in my REL_CURSOS_ALUMNOS query which ends up with the ORA-00906 error.
REL_CURSOS_ALUMNOS is a relationship table for "one to many" 1 ' CURSOS(Id_Curso) to many ALUMNOS(NIF_Alumno).
This is my query till now:
CREATE TABLE PROFESORES(
NIF_Profesor VARCHAR2(15) CONSTRAINT Prof_NIF_PK PRIMARY KEY,
Nombre VARCHAR2(30),
Apellido1 VARCHAR2(30),
Apellido2 VARCHAR2(30),
Direccion VARCHAR2(4000),
Titulacion VARCHAR2(500),
Salario FLOAT(10) CONSTRAINT Prof_sal_NN NOT NULL
);
CREATE TABLE ALUMNOS(
NIF_Alumno VARCHAR2(15) CONSTRAINT Alum_NIF_PK PRIMARY KEY,
Nombre VARCHAR2(30),
Apellido1 VARCHAR2(30),
Apellido2 VARCHAR2(30),
Direccion VARCHAR2(4000),
Sexo CHAR(1),
Fecha_Nacimiento DATE
);
CREATE TABLE CURSOS (
Id_Curso VARCHAR2(15) CONSTRAINT Curs_Id_PK PRIMARY KEY,
Nombre VARCHAR2(400) UNIQUE,
NIF_Profesor VARCHAR2(15) REFERENCES PROFESORES (NIF_Profesor),
Max_Alumnos NUMBER(5),
Inicio_Fecha DATE,
Final_Fecha DATE,
Num_Horas NUMBER(10) NOT NULL,
CONSTRAINT Curs_FechasIncorrectas CHECK (Final_Fecha > Inicio_Fecha));
CREATE TABLE REL_CURSOS_ALUMNOS (
Id_Curso VARCHAR2(15) REFERENCES CURSOS(Id_Cursos),
NIF_Alumno VARCHAR2(15) REFERENCES ALUMNOS(NIF_Alumno) CONSTRAINT RCA_NIFAlum_UQ UNIQUE,
UNIQUE KEY (Id_Curso, NIF_Alumno));
Thank you.
You have a couple of small errors in the final table definition:
CREATE TABLE REL_CURSOS_ALUMNOS (
Id_Curso VARCHAR2(15) REFERENCES CURSOS(Id_Curso),
NIF_Alumno VARCHAR2(15) REFERENCES ALUMNOS(NIF_Alumno) CONSTRAINT RCA_NIFAlum_UQ UNIQUE,
UNIQUE (Id_Curso, NIF_Alumno)
);
Your specific problem is caused by KEY. That is not needed for a unique constraint definition. Further, the reference for CURSOS is id_curso, not id_cursos.
I left both unique definitions. But if NIF_Alumno is unique, then the pair (Id_Curso, NIF_Alumno) is also unique, so that constraint is redundant.

Attempting to create a table with composite keys and date and time data types. Only get errors

OK so here is the table and data types I am attempting to create in sql developer:
Table_Name interview
Field_Name e_number, pos_id, date, time, awarded
Data_Type varchar2(9), varchar2(15), date, number(9), varchar2(3)
Nulls_Allowed
Primary_Key y, y, n, n, n
Unique y, y, n, n, n
Foreign_Key
Comments
Here is the sql I am trying to use:
CREATE TABLE interview(
e_number VARCHAR2(9) NOT NULL UNIQUE,
pos_id VARCHAR2(15) NOT NULL UNIQUE,
date DATE NOT NULL,
time NUMBER(8) NOT NULL,
awarded VARCHAR2(3),
CONSTRAINT pk_interview PRIMARY KEY (e_number, pos_id),
CONSTRAINT fk_e_number FOREIGN KEY (e_number) REFERENCES student
(e_number),
CONSTRAINT fk_pos_id FOREIGN KEY (pos_id) REFERENCES position
(pos_id)
Can someone to tell me where I have "invalid identifier"? Cause I am completely lost.
CREATE TABLE interview(
e_number VARCHAR2(9) NOT NULL UNIQUE,
pos_id VARCHAR2(15) NOT NULL UNIQUE,
"date" DATE NOT NULL,
"time" NUMBER(8) NOT NULL,
awarded VARCHAR2(3),
CONSTRAINT pk_interview PRIMARY KEY (e_number, pos_id),
CONSTRAINT fk_e_number FOREIGN KEY (e_number) REFERENCES student (e_number),
CONSTRAINT fk_pos_id FOREIGN KEY (pos_id) REFERENCES position (pos_id)
);
As suggested by #Remy_Lebeau, #CLifford, and #amdixon
date and time are reserved words (see this post), so enclose them in quotations. Also, add right parentheses and semicolon to close your table declaration.

Reference a foreign key to a different type

Is it possible to do this in PL/SQL:
CREATE TABLE ROOM_TYPE
(
ROOM_TYPE_ID NUMBER NOT NULL PRIMARY KEY,
REF_SERVICE_PROVIDER NUMBER NOT NULL,
ROOM_TYPE_NAME1 VARCHAR2(100) NOT NULL,
ROOM_TYPE_NAME2 VARCHAR2(100),
REF_SERVICE_ID NUMBER(15,3),
ROOM_AC NUMBER(1),
ROOM_BATH_ROOM NUMBER(1),
ROOM_CHOICE_OF_MEAL NUMBER(1),
CONSTRAINT fk_ref_service_provider
FOREIGN KEY (REF_SERVICE_PROVIDER)
REFERENCES PROVIDER(PROVIDER_ID),
CONSTRAINT fk_ref_service_id
FOREIGN KEY (REF_SERVICE_ID)
REFERENCES MEDICAL_SERVICE(SERVICE_ID)
);
While the REF_SERVICE_ID of type number(15,3) is a foreign key in this table which refers to a primary key SERVICE_ID of type number.
The question is, can I refer to a primary key of type number while the foreign key is number(15,3) type !?
Yes you can, but there is a big chance getting following error:
ORA-01438: value larger than specified precision allowed for this column
For eg:
CREATE TABLE A ( ID NUMBER NOT NULL PRIMARY KEY);
CREATE TABLE b
(
id NUMBER(15,3),
val CHAR(1),
CONSTRAINT b_pk FOREIGN KEY (ID) REFERENCES a(id)
);
INSERT INTO A VALUES(456);
INSERT INTO B VALUES( 456, 'X'); --inserts successfully
INSERT INTO A VALUES(12345678901234567890);
INSERT INTO B VALUES( 12345678901234567890, 'X'); --errors