Constraint Check Oracle SQL - oracle-sqldeveloper

I would like to know how I could make sure that the value of SecondInstrument doesn't equal the value in PrimInstrument since a musician cannot be good with the same instrument twice. So far, this is what I have tried yet, and I got this error message:
02438. 00000 - "Column check constraint cannot reference other columns"
*Cause: attempted to define a column check constraint that references
another column.
*Action: define it as a table check constraint.
Could somebody help me out on this one? ;) Thanks in advance.
Create table SessionMusician (
Musician_ID number constraint pkSessionMus_Mus_ID Primary Key,
StageName varchar2(30)constraint nnSessionMus_StageName Not NULL,
RealName varchar2(30)constraint nnSessionMus_RealName Not NULL,
PrimInstrument varchar2(30) constraint nnSessionMus_PrimInstrumant Not Null,
SecondInstrument varchar2(30) Default 'N/A' Constraint
chk_SessionMus_Secinstrument check (SecondInstrument <> PrimInstrument),
CONSTRAINT SessionMusician_unique UNIQUE (StageName,RealName));

You need to make it an out-of-line constraint:
CREATE TABLE SessionMusician (
Musician_ID NUMBER CONSTRAINT pkSessionMus_Mus_ID PRIMARY KEY,
StageName VARCHAR2(30) CONSTRAINT nnSessionMus_StageName NOT NULL,
RealName VARCHAR2(30) CONSTRAINT nnSessionMus_RealName NOT NULL,
PrimInstrument VARCHAR2(30) CONSTRAINT nnSessionMus_PrimInstrumant NOT NULL,
SecondInstrument VARCHAR2(30) DEFAULT 'N/A',
CONSTRAINT SessionMusician_unique UNIQUE ( StageName, RealName ),
CONSTRAINT chk_SessionMus_Secinstrument CHECK (SecondInstrument <> PrimInstrument)
);

Related

Postgres violates not null constraint, even when there isn't one

Hey I have a Postgres database that has a Schema with
CREATE TABLE Mentor (
mentor_ID serial unique,
person_ID serial not null unique,
career_history varchar(255) not null,
preferred_communication varchar(50) not null,
mentoring_preference varchar(50) not null,
linked_in varchar(100) not null,
capacity int not null,
feedback_rating int,
feeback_comment varchar(255),
PRIMARY KEY (mentor_ID),
CONSTRAINT fk_person FOREIGN KEY (person_ID) REFERENCES Person(person_ID)
);
CREATE TABLE Mentee(
mentee_ID integer not null unique,
mentor_ID serial references Mentor(mentor_ID),
person_ID serial not null unique,
study_year int,
motivation varchar(50),
interests varchar(255),
random_match boolean default false,
PRIMARY KEY (mentee_ID),
CONSTRAINT fk_person FOREIGN KEY (person_ID) REFERENCES Person(person_ID)
);
With this, i expect to be able to enter null values for mentor_ID in my database but when I enter the query
insert into mentee(mentee_ID, mentor_ID, person_ID) VALUES (12313, null, 1)
I get the violation
ERROR: null value in column "mentor_id" of relation "mentee" violates not-null constraint
I was wondering how I could make it so I can insert null values for mentor_ID? I dont have it as not null in the table but it still says violating not null constraint.
Thank you
Because serial is not null.
serial is...
CREATE SEQUENCE tablename_colname_seq AS integer;
CREATE TABLE tablename (
colname integer NOT NULL DEFAULT nextval('tablename_colname_seq')
);
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;
Note the integer not null. This is because serial is to be used for primary keys, not foreign keys. Foreign keys are always assigned, they don't need to auto increment.
Use a plain integer.
mentor_ID integer references Mentor(mentor_ID)
Same for your other foreign keys.
Notes:
identity is the SQL standard way to do auto incremented primary keys.
You don't need to declare primary keys as unique, primary keys are already unique.
Unless there's a specific reason to constrain the size of a text field, use text. varchar and text only use the necessary amount of space for each row. "foo" will take the same amount of space in varchar(10) as in varchar(255). For example, there's no particular reason to limit the size of their linked in nor motivation.

postgreSQL syntax error at or near "foreign"

I get the following error of "syntax error at or near "foreign"" when trying to create kurinys table. Might be a silly mistake but I can't recognize it. Thank you in advance.
create table elba7430.kurinys(
id INTEGER not null check (id > 10000),
pavadinimas VARCHAR (55) not null,
metai YEAR,
meno_rusies_id INTEGER not null check (meno_rusies_id > 100),
autoriaus_id INTEGER not null check (autoriaus_id > 1000000),
kliento_id INTEGER not null check (kliento_id > 1000000),
ilgis_cm DECIMAL (100,2),
plotis_cm DECIMAL (100,2),
kaina DECIMAL (100,2),
primary key (id),
foreign key (meno_rusies_id)
REFERENCES elba7430.meno_rusis on delete cascade on update restrict,
foreign key (autoriaus_id)
REFERENCES elba7430.autorius on delete cascade on update restrict,
foreign key (kliento_id)
REFERENCES elba7430.klientas on delete cascade on update restrict
);
create table elba7430.meno_rusis(
id INTEGER not null check (id > 100),
pavadinimas VARCHAR (100) not null,
primary key(id)
);
create table elba7430.autorius(
id INTEGER not null check (id > 1000000),
vardas VARCHAR (40) not null,
pavarde VARCHAR (55) not null,
gimimo_metai DATE,
primary key(id)
);
create table elba7430.klientas(
id INTEGER not null check (id > 1000000),
vardas VARCHAR (40),
pavarde VARCHAR (55),
primary key(id)
);
Changing the order of your declarations and replacing the year data type by a valid one will solve this issue, here you can replicate this: db<>fiddle

Postgres Unique constraint based column value

CREATE TABLE employee (
id varchar(40) NOT NULL,
legacy_id varchar(40) NOT NULL,
short_name varchar(255) NULL,
is_deleted bool NOT NULL DEFAULT false,
CONSTRAINT employee_pkey PRIMARY KEY (id)
)
I want to plan for unique constraint like this
The table must have these constraint
1. Unique constraint is required on column 'legacy_id' and 'is_deleted = false'. Its okay to have multiple values with legacy_id and is_deleted = true.
Is there any way to achieve this
You cannot have a constraint for that, but a partial unique index will do just as well:
CREATE UNIQUE INDEX ON employee (legacy_id)
WHERE is_deleted;

ERROR: there is no unique constraint matching given keys for referenced table "stream_types"

Below example table Stream_types gives an ERROR: there is no unique constraint matching given keys for referenced table, and having stared at it for while now I can't figure out why this error arises in this situation.
create table Stream_types (
id integer NOT NULL,
career careertype NOT NULL,
code character(1) NOT NULL,
description shortstring NOT NULL
);
create table Streams (
id integer, -- PG: serial
code char(6) not null, -- e.g. COMPA1, SENGA1
name LongName not null,
offeredBy integer references OrgUnits(id),
stype ShortString references Stream_types(id),
description TextString,
firstOffer integer references Semesters(id), -- should be not null
lastOffer integer references Semesters(id), -- null means current
primary key (id)
);
The error is:
ERROR: there is no unique constraint matching given keys for referenced table "stream_types"
********** Error **********
ERROR: there is no unique constraint matching given keys for referenced table "stream_types"
SQL state: 42830
I'm using postgresql 9.3.16, can anyone help me fix this?
change create table Stream_types statement like here:
create table Stream_types (
id integer NOT NULL PRIMARY KEY,
career careertype NOT NULL,
code character(1) NOT NULL,
description shortstring NOT NULL
);

Insert into PERSOANE table return violates check constraint but shouldn't

This is my creation script:
CREATE TABLE PERSOANE (
idPers numeric (5)
CONSTRAINT pk_persoane PRIMARY KEY,
NumePren varchar (30)
CONSTRAINT ck_nume CHECK (NumePren=LTRIM(INITCAP(NumePren))),
Loc varchar (30)
CONSTRAINT nn_loc NOT NULL
CONSTRAINT ck_loc CHECK (Loc=LTRIM(INITCAP(Loc))),
Jud varchar (25)
CONSTRAINT nn_jud NOT NULL
CONSTRAINT ck_jud CHECK (Jud=LTRIM(INITCAP(Jud))),
Tel numeric (10)
CONSTRAINT nn_tel NOT NULL,
E_mail varchar(254)
CONSTRAINT nn_e_mail NOT NULL
CONSTRAINT ck_e_mail CHECK (E_mail = LTRIM(E_mail))
);
When I try to insert values in the table I got error: new row for relation "persoane" violates check constraint "ck_jud" Here is my insert script:
INSERT INTO PERSOANE VALUES (11111, 'slimi marius', 'oras', 'judet', 0752361507, 'simic#yahoo.com');
Anyone has any suggestion how to fix this problem?
initcap() will change the first character to uppercase. So your check constraints on the columns numepren, loc and jud require you to enter values where the first character of every word is in uppercase. In the value 'jude' the first character is lowercase, so the check constraint is violated. You need to use 'Jude' instead. This is also true for the other columns you have defined:
INSERT INTO PERSOANE
(idpers, numepren, loc, jud, tel, e_mail)
VALUES
(11111, 'Slimi Marius', 'Oras', 'Judet', 0752361507, 'simic#yahoo.com');