The INSERT statement conflicted with the CHECK (len(column_name)) constraint - insert

i'm new to SQL Sever
I'm trying to insert values into a table but keep getting the following error : The INSERT statement conflicted with the CHECK constraint "CK__Course__CourseCo__3A81B327". The conflict occurred in database "CustomerDB", table "dbo.Course", column 'CourseCode'.
create table Course (
CourseCode int check(len(CourseCode) =4 ),
Cdesc varchar(20),
TotalFeeCash money check(len(TotalFeeCash)=8),
TotalFeeInstallment money check(len(TotalFeeInstallment) =8),
MinDownPayment money check(len(MinDownPayment)= 8),
CourseType varchar(10),
Duration int check(len(Duration) = 4)
)
insert into Course(CourseCode,CDesc,TotalFeeCash,TotalFeeInstallment,MinDownPayment,CourseType,Duration)
values (1,'EACCP',55000,60000,10000,'full',36)
can we fix this without drop the constraint ? (i want to limit the input length to <=4) i've change the constraint check(len(CourseCode) =4 ) to check(len(CourseCode) <=4 ) but it still fail.

Related

How can i drop a check constraint without deleting and recreating the table in sqlplus

I have this table and i added a check to the column duree
with this condition alter table operation modify Duree integer check(Duree>=2);
but in the homework it's specified that it needs to be like (duree>=44) isntead
how this be fixed.
create table Operation (
CodeOP varchar(20) not null ,
Duree integer ,
Chef integer ,
DateDeb Date ,
Budget float ,
CodeS varchar (20),
constraint pk_operation primary key (CodeOP),
constraint fk_operation foreign key(CodeS)REFERENCES Service(CodeS) on delete set null
i tried alter table operation modify Duree integer check(Duree>=44);
but i'm getting this message :
ERROR at line 1: ORA-02293: cannot validate (TPBDD2.SYS_C008292) - check constraint violated

Sample Oracle DB for Real Estate Agency

Im trying to build a sample database for a project about a real estate agency,on the table about the realties i have a column Realtie_id and i want it to start with 11%%% what type should it be (int or varchar ) and how do I make the constraint ?
create table Realties (
rid int not null,
address varchar(50),
m2 real not null,
r_type varchar(20),
primary key (rid),
constraint c_rid check(rid in (.....
);
It depends on what you'll be storing in there.
if it is a string, use VARCHAR2
if it is a number, use NUMBER (or INT)
Constraint in any case might be
SQL> create table realties
2 (rid int constraint ch_rid check (substr(to_char(rid), 1, 2) = '11'));
Table created.
SQL> insert into realties (rid) values ('abc');
insert into realties (rid) values ('abc')
*
ERROR at line 1:
ORA-01722: invalid number
SQL> insert into realties (rid) values ('245');
insert into realties (rid) values ('245')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CH_RID) violated
SQL> insert into realties (rid) values ('1245');
insert into realties (rid) values ('1245')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CH_RID) violated
SQL> insert into realties (rid) values ('11245');
1 row created.
SQL>
If your value is a number then make the column a numeric data type. The exception to this is when your value is a number string that can start with zeroes, like a phone number, when it should be a string data type; otherwise if you use a number data type then the, semantically important, leading zeroes will be stripped off. Since you want the number to start with 11 then this caveat does not apply.
As for the CHECK constraint, you can use LIKE '11%' and Oracle will implicitly convert it to a string to perform the check.
create table Realties (
rid int
not null
CONSTRAINT realties__rid__pk PRIMARY KEY
CONSTRAINT realties__rid__chk CHECK ( rid LIKE '11%' ),
address varchar(50),
m2 real
not null,
r_type varchar(20)
);

Oracle Insert all data from another table contain duplicates how to avoid

have two tables A and B both same structure except B has one addition extra column inserting as "null". I need to Retain all data from A in B when I insert like below query it is inserting duplicate values because of that getting "primary Key violation error" when I try to create the "CONSTRAINT PK_Details_A PRIMARY KEY" Please help on this to avoid duplicate values while inserting the records.
Thanks in advance.
Insert into tableB(
id, effectiveDate, endDate
,startDate, Type, salary
,baseSalary, Amount, Amount1
,currency, Percentage, Salary
,Notional
)
select id, effectiveDate, endDate
,startDate, Type, salary
,baseSalary, Amount, Amount1
,currency, Percentage, Salary,null
from tableA;
EDIT
Primary key definition for B copied from comment below:
ALTER TABLE B
ADD CONSTRAINT PK_B
PRIMARY KEY ( oid)
USING INDEX ( CREATE UNIQUE INDEX PK_B ON B ( oid )

How can I modify a unique constraint to allow for a one off exception

CREATE TABLE SPONSORS (owner_number INT NOT NULL,
requires_anonimity CHAR(3) NOT NULL,
CONSTRAINT chk_requires_anonimity CHECK (requires_anonimity IN ('Yes', 'No')),
FOREIGN KEY (owner_number) REFERENCES OWNER (owner_number),
PRIMARY KEY (owner_number));
CREATE TABLE DONATIONS (receipt_number NCHAR(6) CHECK(receipt_number BETWEEN 111111 AND 999999),
amount_donated NUMBER NOT NULL,
document_reference VARCHAR(50) UNIQUE ,
donations_event_number INT NOT NULL CHECK,
donations_sponsor_number INT NOT NULL,
FOREIGN KEY (donations_sponsor_number) REFERENCES SPONSORS(benefactor_number_sponspor)
);
What I am trying to get out of this command is the following instance,
If Donator BOB decided to give £100 on conditions x and y which will be referenced in the document_reference, he should be able to put in place the same conditions for a future donation, but another individual cannot use his conditions
CREATE UNIQUE INDEX doc_unique_indx
ON DONATIONS ( CASE WHEN donations_sponsor_number = donations_sponsor_number THEN AllOW to use same document_reference ELSE END);
It looks like what you have here is a modelling problem. If document reference must be unique to a particular sponsor then it should be in its own table with a foreign key to sponsor, and the donation table should have a foreign key to the document reference table.
Dave Aldridg is 100% on the mark. This is why normalization is important - your problem is trivial if you have:
SPONSORS (OWNER_NUMBER PK, ...)
DOC_REFS (DOCUMENT_REFERENCE PK,
DONOR_NUMBER FK REFERENCES SPONSORS(OWNER_NUMBER),
...)
DONATIONS (RECEIPT PK,
DOCUMENT_REFERENCE FK REFERENCES DOC_REFS(DOCUMENT_REFERENCE),
...)
If at all possible, fix your model, split the table.
That said... if you had to do this the hard way, do NOT attempt to enforce constraints with triggers (unless only one person is allowed to use the database at a time). The only thing worse than no constraint at all is a false sense of security from a perceived constraint that isn't enforced. If you absolutely must solve this with the table at hand, you can pull off a deferred constraint using a "sanity-checking" FAST REFRESH MV:
CREATE TABLE SPONSORS (owner_number INT NOT NULL,
requires_anonimity CHAR(3) NOT NULL,
CONSTRAINT chk_requires_anonimity CHECK (requires_anonimity IN ('Yes', 'No')),
PRIMARY KEY (owner_number));
CREATE TABLE DONATIONS (receipt_number NCHAR(6) CHECK(receipt_number BETWEEN 111111 AND 999999),
amount_donated NUMBER NOT NULL,
document_reference VARCHAR(50) ,
donations_event_number INT NOT NULL ,
donations_sponsor_number INT NOT NULL,
FOREIGN KEY (donations_sponsor_number) REFERENCES SPONSORS(owner_number));
CREATE MATERIALIZED VIEW LOG ON DONATIONS WITH ROWID (DOCUMENT_REFERENCE, DONATIONS_SPONSOR_NUMBER) INCLUDING NEW VALUES
CREATE MATERIALIZED VIEW DOC_REF_CONSTRAINT
REFRESH FAST ON COMMIT
AS
SELECT DOCUMENT_REFERENCE, MIN(DONATIONS_SPONSOR_NUMBER) MX, MAX(DONATIONS_SPONSOR_NUMBER) MN
FROM DONATIONS
GROUP BY DOCUMENT_REFERENCE
CREATE INDEX DOC_REF_CONSTRAINT_IDX ON DOC_REF_CONSTRAINT(DECODE(MX,MN,0,1/0))
BEGIN
INSERT INTO SPONSORS VALUES (1, 'Yes');
INSERT INTO SPONSORS VALUES (2, 'Yes');
INSERT INTO DONATIONS VALUES ('111111',100,'A',0,1);
INSERT INTO DONATIONS VALUES ('222222',100,'A',0,1);
INSERT INTO DONATIONS VALUES ('333333',100,'C',0,2);
COMMIT;
END;
-- Success!
BEGIN
INSERT INTO SPONSORS VALUES (3, 'Yes');
INSERT INTO SPONSORS VALUES (4, 'Yes');
INSERT INTO DONATIONS VALUES ('444444',100,'A',0,3);
INSERT INTO DONATIONS VALUES ('555555',100,'C',0,3);
INSERT INTO DONATIONS VALUES ('666666',100,'C',0,4);
COMMIT;
END;
--ORA-12008: error in materialized view refresh path
--ORA-01476: divisor is equal to zero
--ORA-06512: at line 7
Note - it's entiirely possible to get more creative than 1/0 - you can put a User Defined Function that throws a comprehensible EXCEPTION instead.

Trigger to enter deleted entries to a new table - SQL plus

I am trying to create a trigger which will enter values into a table terminated_employees when we delete values from the nm_employees table. I have written the trigger but it does not work. Is my trigger format right? Any ideas?
CREATE TABLE nm_departments(
dept2 varchar(20),
CONSTRAINT empPK PRIMARY KEY (dept2)
);
CREATE TABLE nm_employees(
name varchar(20),
dept varchar(20),
CONSTRAINT departments FOREIGN KEY (dept) REFERENCES nm_departments (dept2)ON DELETE CASCADE
);
CREATE TABLE terminated_employees(
te_name varchar(20),
te_dept varchar(20)
);
CREATE TRIGGER term_employee AFTER DELETE ON nm_employee
FOR EACH ROW
BEGIN
INSERT INTO terminated_employees (NEW.te_name, NEW.te_dept) VALUES (OLD.name,OLD.dept)
END;
You should not be specifying the NEW. on the column names of your INSERT statement. These are the columns in the terminated_employees table, NOT the new values. i.e.
INSERT INTO terminated_employees (te_name, te_dept)
VALUES (OLD.name,OLD.dept)
You can use show errors (or show err) in SQL*Plus to see the exact error.
You have a number of problems:
Wrong table name on create trigger (missing the s)
Missing ; after instert statement
The OLD. need to have : prefix. i.e. :OLD.name

Resources