Modify SQL column field to be nullable, if I added constraint do I need to remove constraint as well? - sql

I altered a table with the following script
ALTER TABLE TABLENAME ADD [flagField] CHAR(1) DEFAULT 'N' NOT NULL;
ALTER TABLE TABLENAME ADD CONSTRAINT XCK6_tablename CHECK([flagField] in ('Y', 'N'));
If I want to reverse this script to change that field in the table to allow a nullable state, do I need to remove the constraint before making the field nullable?
so if I run
ALTER TABLE TABLENAME ALTER COLUMN fieldFlag CHAR(1) NULL
will that be fine as is or should I also remove the constraint?

You only have to make the field NULLable.
The logic for constraints differs from the logic for WHERE and CASE WHEN conditions. For WHERE and CASE WHEN, NULL results are treated the same as false.
CHECK is instead validating the data. It accepts as valid anything that is not explicitly false. So, there is no need to include NULL checking in the constraint.
If you did, the correct logic would be:
CHECK (flagField in ('Y', 'N') or flagField is null)
Here is a db<>fiddle illustrating that the behavior is as described above.

Related

SQL constraint to exclude certain values?

Is there an sql constraint that enables you to disallow certain values in a column?
e.g.
ALTER TABLE foo
ADD COLUMN code TEXT NOT NULL UNIQUE DISALLOW '<GENERATE>'
;
s.t. <GENERATE> could be used safely in the application to indicate that the value should be generated before persisting without risking its accidentally being pushed to DB?
You need a check constraint:
ALTER TABLE foo
ADD COLUMN code TEXT NOT NULL UNIQUE;
alter table foo
add constraint disallow_generate
check (code <> '<GENERATE>');
If you want to disallowed multiple values, use a NOT IN condition:
alter table foo
add constraint disallow_generate
check (code not in ('<GENERATE>', '<GENERATED>', 'foo');
You could use Check Constraints.
eg:
ALTER TABLE foo
ADD COLUMN code TEXT NOT NULL UNIQUE CHECK (code <> '<GENERATE>');

Disabling the Not-Nullable field in ORACLE to insert NULL data

I have to add some NULL data into an otherwise not-nullable field. I checked :-
ALTER TABLE <table_name> DROP <default_constraint_name> ;
ALTER TABLE <table_name> ALTER COLUMN <column_name> <data_type> NULL;
ALTER TABLE <table_name> DROP COLUMN <column_name>;
but only the middle one seems fit for my usage because all I want o do is alter instead of . But it does not work. I am using Oracle 11g. Could you suggest anyother method or suggest what mistake I am doing in the 2nd ALTER TABLE SQL?
you can just disable constraint. And then enable it back when you want.
alter table
table_name
ENABLE constraint
constraint_name;
alter table
table_name
DISABLE constraint
constraint_name;
As stated in comment above, it may be a bad idea to allow NULLs in a column that previously did not allow them. Queries or code that already exist may rely on the assumption that the field cannot contain NULL, and could experience various problems if that assumption becomes false (errors if you're lucky, quietly producing incorrect or incomplete results if you're not).
That said, the syntax to simply remove the NOT NULL constraint permanently is:
ALTER TABLE table_name MODIFY column_name NULL;
You can also merely disable the constraint as indicated in another answer. To do this you need the constraint name which you can find by querying USER_CONSTRAINTS. This makes more sense if you expect to enable the constraint later.

SQL how to update a column with a value that depends on the value of a different column in the same table

I have a table with 2 date columns, open_date and close_date. And when I insert a new row the close_date can be null. But in the future I will want to update the close_date, and I want to ensure that the close_date is bigger than the open_date?
Thanx
I'm using oracle...
You can add a check constraint to make sure Close_date is bigger than the Open_Date,
ALTER TABLE Table_Name
ADD CONSTRAINT ck_Con_Name CHECK (Close_date > Open_Date)
To make sure a value is provided for Close_Date column and its not left null, make the column NON-NULABLE.
For this you will need to make sure first there isnt any NULL Values in that column. UPDATE the column to some defualt value. Then Alter table definition something like this...
ALTER TABLE Table_Name
ALTER COLUMN Close_date DATETIME NOT NULL;
A check constraint like this will do ask you ask:
ALTER TABLE YourTable ADD CONSTRAINT CK_open_date_before_close_date
CHECK (open_date < close_date);
This will reject any transactions that update the table to a state that violates the condition. If, for example, you updated 5 rows but only one of them violated the constraint, the entire update will fail.
Additionally, I would recommend that instead of NULL for an open-ended close date, you use a sentinel value of 99991231. There are many reasons for this, not the least of which is performance of queries (so you can do simple inequality statements without needing an OR IS NULL clause). You can additionally then make the column NOT NULL and simplify the above check condition. This also affects front-end application code positively.
UPDATE
The check constraint does not require OR close_date IS NULL in it, since if close_date is NULL, the whole expression will be evaluated to NULL, and this will not violate the CHECK constraint.
You may be needing the CHECK Constraints.
PLease refer to the link

Database column with default constraint

I am creating a database column like this:
Alter table tablename
add column columnname null
add constraint df_columnname default 0
After executing above SQL, the new column is added to table with null values.
Does the constraint df_cloumnname have no meaning here?
Please clarify on this..
If your column is nullable, then adding it with a default constraint has no impact - it can be null, and will remain null. The DEFAULT CONSTRAINT in that case only applies to new rows that are being added (and that do not explicitly specify a value for your column).
If your column were NOT NULL, then the default constraint would be applied right away.
If you're using SQL Server (you didn't specify clearly enough - SQL is the query language - but not a database product...), and you want a nullable column witha default constraint and you want the value to be applied to the existing rows, use this syntax:
ALTER TABLE dbo.tablename
ADD columnname NULL
CONSTRAINT df_columnname DEFAULT 0 WITH VALUES
Add the WITH VALUES to your command and you should get the desired result.

How to add a not null constraint on column containing null values

I have a table with a column that contains a few null values. I want to add a NOT NULL constraint on that column without updating the existing nulls to a non-null value. I want to keep the existing null values and check for future rows that they contain a not null value for this column. Is this possible? How?
You can add an unvalidated constraint - it will not look at existing rows, but it will be checked for any new or updated rows.
ALTER TABLE mytable MODIFY mycolumn NOT NULL NOVALIDATE;
Just be aware that you won't be able to update an existing row unless it satisfies the constraint.
Also, be aware of the downside that the optimizer will not be able to take advantage of this constraint in making its plans - it has to assume that some rows may still have a null.
ALTER TABLE table_name
SET column_name = '0'
WHERE column_name IS NULL;
ALTER TABLE table_name
MODIFY COLUMN(column_name NUMBER CONSTRAINT constraint_identifier NOT NULL);
This is of course assuming that your column is a number but it's the same thing really, you would just change the '0' to a default value that isn't null.
Hammad:
I face the problem and solve like following:
Alter table thr_empl_info modify THR_EM_DESIGNATION_ID not null