Check primary keys values - sql

These are tables in SQLite: especie_similar
CREATE TABLE especie_similar (
id_especie INTEGER NOT NULL,
id_similar INTEGER NOT NULL,
PRIMARY KEY (
id_especie,
id_similar
),
FOREIGN KEY (
id_especie
)
REFERENCES especie (id_especie) ON DELETE CASCADE,
FOREIGN KEY (
id_similar
)
REFERENCES especie (id_especie) ON DELETE CASCADE
);
And especie table:
CREATE TABLE especie (
id_especie INTEGER PRIMARY KEY AUTOINCREMENT
NOT NULL,
nombre_especie VARCHAR NOT NULL,
cient_especie VARCHAR NOT NULL,
desc_especie TEXT,
nidificacion TEXT NOT NULL,
dimorfismo INTEGER NOT NULL,
genero_id INTEGER NOT NULL,
endemismo_id INTEGER NOT NULL,
abundancia_id INTEGER NOT NULL,
FOREIGN KEY (
genero_id
)
REFERENCES genero (id_genero),
FOREIGN KEY (
endemismo_id
)
REFERENCES endemismo (id_endemismo),
FOREIGN KEY (
abundancia_id
)
REFERENCES abundancia (id_abundancia)
);
My situation: Table especie has multiple datas and I want to insert in especie_similar ids from table especie.
How can i code a trigger or sql statement checking if id_especie and id_similar are not equals?
How can i code to retrieve all especies distinct of target id
(ex: table especie_similar has id_especie=1 and id_similar=2
indicating that species id 1 is similar to another species id 2 but
cannot insert id_especie=2 and id_similar=1 due to previous
duplicate data)?
Thanks and sorry my english is not good.

For question #1 you can use a standard SQL constraint:
create table especie_similar (
...
check (id_especie <> id_similar) -- your condition here
);
For question #2 you can use a standard "Recursive CTE". See SQLite WITH Clause. Example:
with recursive node as (
select id from especie_similar
where id_especie = 123 -- first node criteria here
union all
select s.id_especie
from node n
join especie_similar s on s.id_similar = n.id
)
select * from node;

Related

How do i select the highest number in one column and then search the the id in other table

How do I select the highest number in one column and then search the the ID in other table?
I have these two tables:
create table armazens_has_artigos
(
stockMinimo int not null,
stockQuantidade int not null,
idArtigos int not null,
idArmazens int not null,
idZonasFisicas int not null
constraint fk_armazens_has_artigos_idArtigos
foreign key (idArtigos)
references artigos (idArtigos) on delete cascade,
constraint fk_armazens_has_artigos_idArmazens
foreign key (idArmazens)
references armazens (idArmazens) on delete cascade,
constraint fk_armazens_has_artigos_zonasFisicas_idZonasFisicas
foreign key (idZonasFisicas)
references zonasFisicas (idZonasFisicas) on delete cascade
);
In this one I need to select the highest StockQuantidade and then with the same id select in this table the nome
create table artigos
(
idArtigos int primary key,
nome varchar (45) not null,
descr varchar (45) not null,
precoCompra float not null,
precoVenda float not null,
unidadeRepresentacao varchar (45) not null
);
There might be more then one Artigos matching the criteria:
SELECT *
FROM artigos
WHERE idArtigos IN
(
SELECT idArtigos
FROM dbo.armazens_has_artigos
WHERE stockQuantidade =
(
SELECT MAX(stockQuantidade)FROM armazens_has_artigos
)
);
SELECT
nome
FROM artigos
JOIN armazens_has_artigos
ON armazens_has_artigos.idArtigos = artigos.idArtigos
WHERE armazens_has_artigos.stockQuantidade = (SELECT
MAX(stockQuantidade)
FROM armazens_has_artigos)

how can i create constraint on table based on many column and value in another table Oracle

i have atable such as codes table have these values:
Major_code
minor_code
name
1
0
Country
1
1
USA
1
2
JOR
2
0
Food
2
1
Burger
2
2
Mansaf
I need to create table have these columns (ID, Country_ID , Food_ID) what is the best constraint that resolve this issue?
Having a single lookup table for all reference codes is a very poor design, to the point of almost being a SQL anti-pattern. It is likely to have performance issues. It is also difficult to ensure that the value of COUNTRY_MINOR_CODE references an actual country minor code in the reference table. Which brings us to this statement:
I need to create table have these columns (ID, Country_ID , Food_ID)
Can't be done. Or at least it can be done but you can't build foreign keys on that, because minor keys are not unique in the referenced table. But if you are set on a single table, say lookup_table with primary key defined on (major_code, minor code), you need this:
create table country_food (
id number primary_key
,country_major_code number not null
,country_minor_code number not null
,food_major_code number not null
,food_minor_code number not null
,constraint country_major_code_ck check (country_major_code = 1)
,constraint country_minor_code_ck check (country_minor_code != 0)
,constraint food_major_code_ck check (country_major_code = 2)
,constraint food_minor_code_ck check (country_minor_code != 0)
,constraint country_code_fk foreign key (country_major_code, country_minor_code) references lookup_table
,constraint food_code_fk foreign key (food_major_code, food_minor_code) references lookup_table
)
/
The hard coding of values, the additional check constraints, the increased complexity of joining to look up the name ... These are all reasons why you should choose to have separate tables for FOOD, COUNTRY, etc.
create table country (
country_id number primary_key
,country_name varchar2(24) not null
)
/
create table food (
food_id number primary_key
,food_name varchar2(24) not null
)
/
create table country_food (
id number primary_key
,country_id number not null
,food_id number not null
,food_major_code number not null
,constraint country_code_fk foreign key (country_id) references country
,constraint food_code_fk foreign key (food_id) references food
)
/
If I've got it right, you can get all Country+Food pairs with the query
select t1.minor_code counrty_id, t2.minor_code food_id
from (
select minor_code
from codesTable
where Major_code = (
select c.Major_code
from codesTable c
where c.minor_code=0 and c.name='Country')
) t1
cross join (
select minor_code
from codesTable
where Major_code = (
select c.Major_code
from codesTable c
where c.minor_code=0 and c.name='Food')
) t2
You can use the query to insert data into a table with an autogenerated ID or use it any other way.
First you need to consider the design of the tables
Table of country
Table of foods
Tables of food_country -------- relationship => many-to-many
CREATE TABLE Country
(
Major_code numeric(15) not null,
minor_code numeric(15),
name varchar(50),
CONSTRAINT country_pk PRIMARY KEY (Major_code)
);
CREATE TABLE Food
(
Food_ID numeric(15) not null,
//...........................
name varchar(50),
CONSTRAINT food_pk PRIMARY KEY (Food_ID)
);
CREATE TABLE Counry_Food
(
ID numeric(10) not null,
Country_ID numeric(15) not null,
Food_ID numeric(15) not null,
CONSTRAINT fk_country
FOREIGN KEY (Country_ID)
REFERENCES Country(Major_code),
CONSTRAINT fk_food
FOREIGN KEY (Food_ID)
REFERENCES supplier(Food_ID),
);

sqlite insert into table with NULL foreign key

I have the following tables: (notice that SUPP may be NULL)
CREATE TABLE IF NOT EXISTS A
(
ID INTEGER PRIMARY KEY NOT NULL
);
CREATE TABLE IF NOT EXISTS B
(
ID INTEGER PRIMARY KEY NOT NULL
);
CREATE TABLE IF NOT EXISTS C
(
ID INTEGER PRIMARY KEY NOT NULL
);
CREATE TABLE IF NOT EXISTS D
(
SID INTEGER NOT NULL,
DID INTEGER NOT NULL,
SUPP INTEGER,
PRIMARY KEY (SID, DID),
FOREIGN KEY (SID) REFERENCES A (ID),
FOREIGN KEY (DID) REFERENCES B (ID),
FOREIGN KEY (SUPP) REFERENCES C (ID)
);
Assuming table A contains ID=2 and table B contains ID=5.
I want to perform the following insert:
INSERT OR REPLACE INTO D VALUES (2,5,NULL);
However I get the following error:
Foreign key mismatch - D referencing C (INSERT INTO "main"."D" ...
I'm assuming the issue stems from the NULL foreign key but I don't know what I did wrong or how to fix it.
You need to use valid referenced value if you are inserting into child table. The ID 2 in table A and ID 5 in table B has to be present.
CREATE TABLE IF NOT EXISTS A
(
ID INTEGER PRIMARY KEY NOT NULL
);
CREATE TABLE IF NOT EXISTS B
(
ID INTEGER PRIMARY KEY NOT NULL
);
CREATE TABLE IF NOT EXISTS C
(
ID INTEGER PRIMARY KEY NOT NULL
);
CREATE TABLE IF NOT EXISTS D
(
SID INTEGER NOT NULL,
DID INTEGER NOT NULL,
SUPP INTEGER,
PRIMARY KEY (SID, DID),
FOREIGN KEY (SID) REFERENCES A (ID),
FOREIGN KEY (DID) REFERENCES B (ID),
FOREIGN KEY (SUPP) REFERENCES C (ID)
);
INSERT INTO A VALUES (2);
INSERT INTO B VALUES (5);
INSERT INTO D VALUES (2, 5, NULL);

SQL not exists in another table, but count is higher than 3 in another table

I have the following tables that have profiles and list of photos (in the photo table) for each profile. I also have a service table and a used table, I want a query that would return the profile id which does not belong in the used service and also has more than 3 photos in the photo table
The profile table
CREATE TABLE public.profile
(
id integer NOT NULL DEFAULT nextval('profile_id_seq'::regclass),
name text COLLATE pg_catalog."default" NOT NULL,
birthday timestamp with time zone NOT NULL,
CONSTRAINT profile_id PRIMARY KEY (id)
)
The photo table
CREATE TABLE public.photo
(
id integer NOT NULL DEFAULT nextval('photo_id_seq'::regclass),
image bytea NOT NULL,
image_id text COLLATE pg_catalog."default" NOT NULL,
order_count smallint NOT NULL,
profile_id bigint NOT NULL,
CONSTRAINT photo_id PRIMARY KEY (id),
CONSTRAINT photo_profile_id_fkey FOREIGN KEY (profile_id)
REFERENCES public.profile (id) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE CASCADE
)
the service table
CREATE TABLE public.service
(
id integer NOT NULL DEFAULT nextval('service_id_seq'::regclass),
name text COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT service_id PRIMARY KEY (id)
)
the used table
CREATE TABLE public.used
(
id integer NOT NULL DEFAULT nextval('used_id_seq'::regclass),
service_id bigint NOT NULL,
profile_id bigint NOT NULL,
insert_timestamp timestamp with time zone NOT NULL DEFAULT now(),
CONSTRAINT used_id PRIMARY KEY (id),
CONSTRAINT used_profile_id_fkey FOREIGN KEY (profile_id)
REFERENCES public.profile (id) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT used_service_id_fkey FOREIGN KEY (service_id)
REFERENCES public.service (id) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE CASCADE
)
use exists and not exists
select p.* from profile p
where exists ( select 1 from photo ph where ph.profile_id =p.id
having count (distinct image_id )=3
)
and not exists ( select 1 from used u where u.profile_id =p.id)
I would go for:
select p.profile_id
from photo p
where not exists (select 1
from used u
where u.profile_id = p.profile_id
)
group by p.profile_id
having count(*) >= 3;
If you just need the profile_id, then the profiles table is not needed.

Reference from one table to another entire table and specified row

I've got three tables: A, B and C. I want to create table A, and it should have 3 columns, first one should be a PRIMARY KEY.
1) How to create second column, with is a refference to table B,
2) and third with is a refference to 'C.id' row from table C. A.id = C.id
CREATE TABLE A
(
id SERIAL PRIMARY KEY,
// ? - reference to table B
// ? - reference to C.id row. A.id = C.id
)
Database: postgresql
I assume you use mysql database.
CREATE TABLE A
(
id INT NOT NULL PRIMARY KEY,
b_id INT NOT NULL,
c_id INT NOT NULL,
FOREIGN KEY (b_id) REFERENCES B (id),
FOREIGN KEY (c_id) REFERENCES C (id)
) TYPE = INNODB;
Update for using postgresql:
CREATE TABLE "A"
(
id integer NOT NULL,
b_id integer NOT NULL,
c_id integer NOT NULL,
CONSTRAINT id PRIMARY KEY (id),
CONSTRAINT b_id FOREIGN KEY (b_id) REFERENCES "B" (id)
ON UPDATE NO ACTION ON DELETE NO ACTION, --with no action restriction
CONSTRAINT c_id FOREIGN KEY (c_id) REFERENCES "C" (id)
ON UPDATE CASCADE ON DELETE CASCADE --with cascade restriction
)
WITH (
OIDS = FALSE
)
;
ALTER TABLE "C" OWNER TO postgres;