I'm using the following code in Oracle 11g
alter table account_creation
drop constraint branch_code;
I get an error saying cannot drop constraint non existent constraint but when I check the table the NULL constraint still exists for that particular column
Am I using the correct syntax?
Use this syntax:
ALTER TABLE account_creation
MODIFY (branch_code varchar(10) NULL);
Related
I have an exercises table with this schema:
CREATE TABLE exercises
(
id INTEGER NOT NULL,
name VARCHAR(255) NOT NULL,
workoutID INTEGER NOT NULL,
FOREIGN KEY (workoutID) REFERENCES workouts(id)
);
I want to delete the column workoutID but it won't let me delete it:
sqlite> ALTER TABLE exercises DROP workoutID;
Error: near "DROP": syntax error
Upon realizing I need to remove the constraints first I try:
sqlite> ALTER TABLE exercises DROP CONSTRAINT workoutID;
Error: near "DROP": syntax error
I followed this question to the letter: How to drop column with constraint?
But it doesn't work for me.
I can start over by dropping the whole table if I have to but I want to learn why this isn't working for me.
Any help and explanation is greatly appreciated.
I am using SQL Server and have a table Employee. I want to change the constraint of Starting_Date from NULL to NOT NULL and add a DEFAULT with GETDATE() function. Please check the screenshot and suggest how to solve the error I get:
Try this TSQL command for add, not null constraint
ALTER TABLE [dbo].[Employee] ALTER COLUMN [Starting_Date] DataTime NOT NULL;
If your table had any rows with null Starting_Date run this query before adding the constraint
UPDATE [dbo].[Employee] SET Starting_Date = [dbo].[GetDate]() WHERE Starting_Date = NULL;
and then this, for default constraint from your function output result
ALTER TABLE [dbo].[Employee]
ADD CONSTRAINT df_Starting_Date
DEFAULT([dbo].[GetDate]())FOR Starting_Date;
How to apply Not null constraint in SQL command line in Oracle database
this example shows if you want to give a constraint a name
ALTER TABLE mytable
MODIFY (mynumber NUMBER(8,2) CONSTRAINT my_cons_name NOT NULL);
this example show if you do not want to give a constraint a name
ALTER TABLE mytable
MODIFY (mynumber NUMBER(8,2) NOT NULL);
source: https://community.oracle.com/thread/45653
alter table bu_consultas drop constraint if exists fk_b_cnslts_cdg_sr_d;
Error SQL: ORA-01735: invalid ALTER TABLE option
There is no IF EXISTS clause in the ALTER TABLE command for Oracle. If the constraint doesn't exist the command will fail, but no harm done.
This is probably a simple answer but I can't find it. I have a table with a column of integers and I want to ensure that when a row is inserted that the value in this column is greater than zero. I could do this on the code side but thought it would be best to enforce it on the table.
Thanks!
I was in error with my last comment all is good now.
You can use a check constraint on the column. IIRC the syntax for this looks like:
create table foo (
[...]
,Foobar int not null check (Foobar > 0)
[...]
)
As the poster below says (thanks Constantin), you should create the check constraint outside the table definition and give it a meaningful name so it is obvious which column it applies to.
alter table foo
add constraint Foobar_NonNegative
check (Foobar > 0)
You can get out the text of check constraints from the system data dictionary in sys.check_constraints:
select name
,description
from sys.check_constraints
where name = 'Foobar_NonNegative'
Create a database constraint:
ALTER TABLE Table1 ADD CONSTRAINT Constraint1 CHECK (YourCol > 0)
You can have pretty sophisticated constraints, too, involving multiple columns. For example:
ALTER TABLE Table1 ADD CONSTRAINT Constraint2 CHECK (StartDate<EndDate OR EndDate IS NULL)
I believe you want to add a CONSTRAINT to the table field:
ALTER TABLE tableName WITH NOCHECK
ADD CONSTRAINT constraintName CHECK (columnName > 0)
That optional NOCHECK is used to keep the constraint from being applied to existing rows of data (which could contain invalid data) & to allow the constraint to be added.
Add a CHECK constraint when creating your table
CREATE TABLE Test(
[ID] [int] NOT NULL,
[MyCol] [int] NOT NULL CHECK (MyCol > 1)
)
you can alter your table and add new constraint like bellow.
BEGIN TRANSACTION
GO
ALTER TABLE dbo.table1 ADD CONSTRAINT
CK_table1_field1 CHECK (field1>0)
GO
ALTER TABLE dbo.table1 SET (LOCK_ESCALATION = TABLE)
GO
COMMIT