Altering constraint in Derby - sql

I don't understand the logic in Derby syntax.
I'm new to SQL, I'm learning now.
I needed to alter a column constraint. I needed to add "not null".
Since I already did an alter on a column for the constraint unique, I thought the syntax will be the same since they are both constraints.
I finally found for not null this link Cannot alter column in existing table in java Derby database
and as I say the same syntax doesn't work for adding a unique constraint to the same column, so my question is: how is the syntax changed for each constraint?
This works:
ALTER TABLE WALLETUSER ALTER COLUMN WALLETUSERNAME NOT NULL;
So why does this not work?
ALTER TABLE WALLETUSER ALTER COLUMN WALLETUSERNAME unique;
And why does this work:
alter table customer add constraint cu1 unique (cust_name);
but this does not:
alter table customer add constraint not null (cust_name);

Related

How to add check constraint on a column in table?

ALTER TABLE Student ADD CONSTRAINT CHK_Student_ID_LENGTH CHECK (LEN([ID]) between 12 and 14);
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_NAME = 'CHK_Student_ID_LENGTH';
alter table Student drop constraint CHK_Student_ID_LENGTH;
I tried to add check constraint using the first statement. It gave me the below error.
[23000][547] The ALTER TABLE statement conflicted with the CHECK constraint "CHK_Student_ID_LENGTH". The conflict occurred in database, table "Student", column 'ID'.
I tried to check if there was any existing constraint with the same name. But I did not get any. Still I tried to drop the constraint. But then it gave the error:
CHK_Student_ID_LENGTH is not a constraint.
But still add constraint statement gives error saying it already exists. Where am I going wrong?
The error message is a little badly worded, but isn't saying what you think it's saying.
It's not saying that there's already a constraint with the same name. It's saying that the constraint is being violated. That means that there is data already in the table that doesn't meet the requirements of the new constraint you're trying to introduce.
You could use the NOCHECK option to create the constraint whilst allowing existing data to violate it. But this is frequently the wrong thing to do. It is usually more sensible to fix the existing data.
Specifying NOCHECK means that the constraint can't be used by the optimizer to eliminate redundant actions that the logic of the constraint would preclude.
You probably have some records in Your table that conflict with that new constraint.
Just find them and UPDATE/DELETE before adding a contraint.
SELECT *
FROM Student
WHERE LEN([ID]) between 12 and 14
Or try to add that constraint without checking existing values using WITH NOCHECK
ALTER TABLE Student WITH NOCHECK
ADD CONSTRAINT CHK_Student_ID_LENGTH CHECK (LEN([ID]) between 12 and 14);
Tip: In migration files I usually add this before adding a new constraint:
IF OBJECT_ID('CHK_Student_ID_LENGTH') IS NOT NULL
ALTER TABLE Student DROP CONSTRAINT CHK_Student_ID_LENGTH
IF OBJECT_ID('CHK_Student_ID_LENGTH') IS NULL
ALTER TABLE Student ADD CONSTRAINT ...
More about ALTER TABLE https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-table-transact-sql?view=sql-server-ver15

The ALTER TABLE statement conflicted with the CHECK constraint

I want to create a foreign key between two database. Happens that the tables have data, so when I'm making the foreign relational there's giving me error.
I found the error happen when your tables have data. So how to not verify existing data?
ALTER TABLE [contrato_sigob].[relacion_institucion_categoria]
ADD CONSTRAINT CHECK_CATEGORIA
CHECK([dbo].[func_CHECK_CATEGORIA](id_categoria)=1);
The error says:
The ALTER TABLE statement conflicted with the CHECK constraint "CHECK_CATEGORIA". The conflict occurred in database "SIGOB_Contraloria", table "contrato_sigob.relacion_institucion_categoria", column 'id_categoria'.
So how to not verify existing data?
You can create a constraint that will not check your existing records by adding WITH NOCHECK:
ALTER TABLE TABLE_NAME WITH NOCHECK
ADD CONSTRAINT ...

SQL Alter table failure on constraint

My SQL is very out old, looking for some help with this error.
The alter table statement conflicted with the check constraint "ZYZ". The Conflict occurred in table A column DESCRIPTION
ALTER TABLE A_A
ADD CONSTRAINT ZYZ
CHECK (((LTRIM(RTRIM([DESCRIPTION)))+'A'=([DESCRIPTION+'A') AND [DESCRIPTION]<>''));
I get this is a data issue, but not sure what to look at...

How to add constraint to table in sql?

I create a table
create table CARS{
CAR_ID NUMBER(10), CONSTRAINT X_CAR_ID NOT NULL
}
and now I want to change the name of the constraint, so I drop the constraint:
ALTER TABLE CARS DROP CONSTRAINT X_CAR_ID;
This works correclty but, when I tried to add new constraint I have a problem,
my query:
ALTER TABLE CARS ADD CONSTRAINT XX_CAR_ID (CAR_ID) NOT NULL;
I thought that query, will be working correctly, but I get only error report:
Error report -
SQL Error: ORA-00904:
How to add correctly this constraint ?
While I couldn't test it I believe the statement below is what you want:
ALTER TABLE CARS MODIFY CAR_ID CONSTRAINT XX_CAR_ID NOT NULL;
Oracle uses the modify keyword in this context.
To rename it without dropping first you would use:
alter table cars rename constraint x_car_id to xx_car_id;
See the reference for more info.

SQL - Add Unique Constraint Failure

Trying to alter a table in SQL Server. I want to add a unique constraint to a column called Names in table ReportingItemNames:
ALTER TABLE ReportingItemNames
ADD CONSTRAINT UC_ReportingItemNames$Name UNIQUE ([ReportingItemNames,Name])
But I am getting this error:
Column name 'ReportingItemNames,Name' does not exist in the target table or view
Where am I going wrong?
Use this:
ALTER TABLE ReportingItemNames
ADD CONSTRAINT UC_ReportingItemNames UNIQUE ([Name])
You can refer to ALTER TABLE (Transact-SQL) documentation for more information.
Shouldn't it be:
ALTER TABLE ReportingItemNames
ADD CONSTRAINT UC_ReportingItemNames$Name UNIQUE ([Name])