Alter table dept drop constraint dno_notnull - oracle11g

here I am droping a not null constraint. Is there any alternate statement for this in oracle?
SQL> alter table dept drop constraint dno_notnull;

Yes, if the constraint is actually a named "NOT NULL" constraint, rather than a check constraint defined as "check (dno is not null)":
alter table dept modify dno null;
A named not null constraint could for example be added at table creation time like this:
create table dept (dno integer constraint dno_notnull not null, ...);

Related

cannot drop NOT NULL constraint on a DEFAULT ON NULL column

I have a table A with schema.
Name Null? Type
-------- -------- ------------
NAME VARCHAR2(10)
TABLE_ID NOT NULL NUMBER
I wanted Table_ID to be an auto-incrementing column so I created a sequence.
create sequence table_id minvalue 1 start with 1 cache 10;
and I modified the Table_ID column as
alter table A
modify table_id default on null table_id.nextval;
now I cannot drop the table or the column or the constraint.
It is giving me this error.
An error was encountered performing the requested operation:
ORA-30667: cannot drop NOT NULL constraint on a DEFAULT ON NULL column
30667.0000 - "cannot drop NOT NULL constraint on a DEFAULT ON NULL column"
*Cause: The NOT NULL constraint on a DEFAULT ON NULL column could not be
dropped.
*Action: Do not drop the NOT NULL constraint on a DEFAULT ON NULL column.
The only way to drop the constraint is to remove the ON NULL
property of the column default.
Vendor code 30667
I have tried to purge the recyclebin but it is not working either.
I read other posts but none of this seems to make sense.
Please help.
If you just need to remove the constraint, just set a default value on that column. The constraint will be removed automatically:
alter table a modify table_id default 0;
Full example:
create table a(name varchar2(10), table_id number not null);
create sequence table_id minvalue 1 start with 1 cache 10;
alter table a modify table_id default on null table_id.nextval;
select * from user_constraints where table_name = 'A'; -- one constraint "TABLE_ID" IS NOT NULL
alter table a drop constraint SYS_C0026367; -- ORA-30667: cannot drop NOT NULL constraint on a DEFAULT ON NULL column
alter table a modify table_id default 0;
alter table a drop constraint SYS_C0026367; -- ORA-02443: Cannot drop constraint - nonexistent constraint
select * from user_constraints where table_name = 'A'; -- the constraint was dropped automatically
I ran this code on Oracle Database 19c Standard Edition 2 Release 19.0.0.0.0 - Production and it worked fine:
create table a(name varchar2(10), table_id number not null);
create sequence table_id minvalue 1 start with 1 cache 10;
alter table a modify table_id default on null table_id.nextval;
drop table a;
Table A created.
Sequence TABLE_ID created.
Table A altered.
Table A dropped.

Set auto_increment for a primary key already created

I created two tables in a database on MariaDB: CasaProduzione and Produzione.
create table CasaProduzione(nome varchar(80) primary key);
alter table CasaProduzione add column id tinyint;
alter table CasaProduzione drop primary key;
alter table CasaProduzione modify nome varchar(80) not null;
alter table CasaProduzione modify id tinyint primary key auto_increment;
create table Produzione(
id_film smallint not null,
id_casaProduzione tinyint not null,
data date not null,
constraint `fk_produzione`
foreign key (id_film) references Film(id),
foreign key (id_casaProduzione) references CasaProduzione(id)
on update cascade
on delete restrict);
alter table Produzione modify data in smallint(4);
After i moved the column id in the table of CasaProduzione at first
alter table CasaProduzione modify column id tinyint(4) first;
Then i tried to set auto_increment in prevoius column
alter table Produzione modify column id tinyint(4) auto_increment;
ERROR 1833 (HY000): Cannot change column 'id': used in a foreign key constraint 'Produzione_ibfk_1' of table 'Film.Produzione'
So i tried to cancel the foreign key from Produzione
alter table Produzione drop foreign key fk_produzione;
but the result is the same.
What am I doing wrong?
After suggestion from the comment, I post here the result of this command:
SHOW CREATE TABLE Film.Produzione \G;
***************************
1. row
***************************
Table: Produzione
Create Table:
CREATE TABLE Produzione (
id_film SMALLINT(6) NOT NULL,
id_casaProduzione TINYINT(4) NOT NULL,
DATA SMALLINT(4) DEFAULT NULL,
KEY fk_produzione (id_film),
KEY id_casaProduzione (id_casaProduzione),
CONSTRAINT Produzione_ibfk_1 FOREIGN KEY (id_casaProduzione) REFERENCES CasaProduzione (id) ON UPDATE CASCADE )
ENGINE=INNODB DEFAULT CHARSET=utf8mb4
1 row in set (0.001 sec)
As told by #FanoFN, with the command
show CREATE TABLE Film.Produzione \G;
the system showed me the constraint on the table Produzione. The system created the constraint Produzione_ibfk_1
I deleted this constraint with the command
alter table Produzione drop foreign key Produzione_ibfk_1;
Query OK, 0 rows affected (0.130 sec)
Records: 0 Duplicates: 0 Warnings: 0
After I can applied the command
alter table Produzione modify column id tinyint(4) auto_increment;
Thanks a lot

CHECK Constraint based on a column value IN OTHER Table

SqlServer
Suppose I have 2 tables:
Table 1 - having column A
Table 2 - having column B [Bit] Not Null
Is it possible to have a Check Constraint, such that value of Column B can be "0", only when Column A is NOT NULL.
OR put it other way, value of Column B can be "1", only when Column A is NULL.
Thanks in advance.
Assuming that these tables are already related by a suitable foreign key, we can implement this check using a computed column and a new foreign key.
Make sure you read to the end
So if we have:
CREATE TABLE Table1 (
Table1ID char(5) not null,
ColumnA int null,
constraint PK_Table1 PRIMARY KEY (Table1ID)
)
CREATE TABLE Table2 (
Table2ID char(7) not null,
Table1ID char(5) not null,
ColumnB bit not null,
constraint PK_Table2 PRIMARY KEY (Table2ID),
constraint FK_Table2_Table1 FOREIGN KEY (Table1ID) references Table1 (Table1ID)
)
We can run this script:
alter table Table1 add
ColumnBPrime as CAST(CASE WHEN ColumnA is NULL THEN 1 ELSE 0 END as bit) PERSISTED
go
alter table Table1 add constraint UQ_Table1_WithColumnBPrime UNIQUE (Table1ID, ColumnBPrime)
go
alter table Table2 add constraint FK_Table2_Table1_CheckColumnB FOREIGN KEY (Table1ID, ColumnB) references Table1 (Table1ID,ColumnBPrime)
Hopefully you can see how this enforces the relationship between the two tables1.
However, there's an issue. In T-SQL, any DML statement may only make changes to one table. So there's no way to issue an update that both changes whether ColumnA is null or not and changes Column B to suit it.
This is another good reason not to have Column B in the database at all - it's derived information, and in our quest to ensure it always matches its definition, we'd have to always delete from Table 2, update Table 1 and re-insert in Table 2.
1It's now a matter of personal taste whether you remove the previous foreign key or leave it in place as the "real" one.

Unable to insert data into table with foreign key in oracle11g

CREATE TABLE Client_master (
Client_no varchar(6) PRIMARY KEY,
Name varchar(15) NOT NULL,
City varchar(15),
Pincode number(8),
State varchar(15),
Bal_due Number(10,2),
CHECK(Client_no LIKE 'C%'));
INSERT INTO
Client_master(Client_no,Name,City,Pincode,State,Bal_due)
VALUES('C00001','Ivan Bayross','Bombay','400054','Maharashtra',15000);
1 row created.
CREATE TABLE Sales_order(
Order_no varchar(6) PRIMARY KEY REFERENCES Client_master (Client_no),
Order_date date,
Client_no varchar(6),
Dely_type char(1) DEFAULT 'f',
Billed_yn char(1),
Salesman_no varchar(6),
Dely_date date,
Order_status varchar(10),
CHECK(Order_no LIKE 'O%'),
CHECK(Order_status IN ('inprocess','backorder','cancelled')),
CHECK(Dely_date>Order_date));
INSERT INTO Sales_order(Order_no, Order_date, Client_no, Dely_type, Billed_yn, Salesman_no, Dely_date, Order_status)
VALUES('C19001','12-jan-96','C00001','f','n','S0001','20-jan-96','inprocess');
INSERT INTO
*
ERROR at line 1:
ORA-02291: integrity constraint (SYSTEM.SYS_C007155) violated - parent key not
found
Please help me insert data into Child table.
What's this SYSTEM.SYS_C007155 error? Why this error message "parent key is not found"?
The parent key not found is because you have a foreign key in your sales_order table which is pointing to the client_master, and there is no matching key value - exactly what it says. But it's because you've done something odd:
Sales_order(Order_no varchar(6) PRIMARY KEY REFERENCES Client_master (Client_no)
You've made the order_no the primary key for this table, but also made it a foreign key to the client_no on the other table. Your insert uses order_no of 'C19001', which doesn't match the client_no you previously inserted into the parent table.
You almost certainly wanted sales_order.client_no to be a foreign key to client_master.client_no, so you would have the references... against that column:
CREATE TABLE Sales_order(Order_no varchar(6) PRIMARY KEY,
Order_date date,
Client_no varchar(6) REFERENCES Client_master (Client_no),
...
You have a further problem as the check constraint you have against the order_no is CHECK(Order_no LIKE 'O%') but as mentioned the value you're putting in is 'C19001', which doesn't match the pattern. Presumably you meant the insert to be for 'O19001'; if not then the constraint is defined incorrectly. I actually hit that check constraint before the primary key constrain in 11gR2, so you may just have changed that while posting the question.
You can look in the user_constraints and user_cons_columns views to see what a constraint is doing, but you'll find it easier if you name your constraints instead of letting Oracle give them default names like SYS_C007155:
Specify a name for the constraint. If you omit this identifier, then Oracle Database generates a name with the form SYS_Cn. Oracle stores the name and the definition of the integrity constraint in the USER_, ALL_, and DBA_CONSTRAINTS data dictionary views (in the CONSTRAINT_NAME and SEARCH_CONDITION columns, respectively).
For example:
CREATE TABLE Client_master (
Client_no varchar(6),
Name varchar(15) NOT NULL,
City varchar(15),
Pincode number(8),
State varchar(15),
Bal_due Number(10,2),
CONSTRAINT Client_master_pk PRIMARY KEY (Client_no),
CONSTRAINT Client_master_chk_no CHECK(Client_no LIKE 'C%'));
INSERT INTO Client_master(Client_no,Name,City,Pincode,State,Bal_due)
VALUES('C00001','Ivan Bayross','Bombay','400054','Maharashtra',15000);
1 row inserted.
CREATE TABLE Sales_order (
Order_no varchar(6),
Order_date date,
Client_no varchar(6),
Dely_type char(1) DEFAULT 'f',
Billed_yn char(1),
Salesman_no varchar(6),
Dely_date date,
Order_status varchar(10),
CONSTRAINT Sales_order_pk PRIMARY KEY (Order_no),
CONSTRAINT Sales_order_fk_client FOREIGN KEY (Client_no)
REFERENCES Client_master (Client_no),
CONSTRAINT Sales_order_chk_no CHECK (Order_no LIKE 'O%'),
CONSTRAINT Sales_order_shk_status CHECK
(Order_status IN ('inprocess','backorder','cancelled')),
CONSTRAINT Sales_order_chk_dates CHECK(Dely_date>Order_date));
INSERT INTO Sales_order(Order_no, Order_date, Client_no, Dely_type, Billed_yn, Salesman_no, Dely_date, Order_status)
VALUES('O19001','12-jan-96','C00001','f','n','S0001','20-jan-96','inprocess');
1 row inserted.
If you do have a violation, e.g. with your original order_no value, you'd see a more useful message:
INSERT INTO Sales_order(Order_no, Order_date, Client_no, Dely_type, Billed_yn, Salesman_no, Dely_date, Order_status)
VALUES('C19001','12-jan-96','C00001','f','n','S0001','20-jan-96','inprocess');
ORA-02290: check constraint (YOUR_SCHEMA.SALES_ORDER_CHK_NO) violated
so you can have a better idea which constraint is violated, and what that means, without having to loo in the data dictionary. SALES_ORDER_CHK_NO is easier to interpret than SYS_C007155 Use names that make sense for you of course.
You can name inline constraints too:
CREATE TABLE Client_master (
Client_no varchar(6) CONSTRAINT Client_master_pk PRIMARY KEY,
...
CREATE TABLE Sales_order (
Order_no varchar(6) CONSTRAINT Sales_order_pk PRIMARY KEY,
Order_date date,
Client_no varchar(6) CONSTRAINT Sales_order_fk_client REFERENCES Client_master (Client_no),
...
but it might be clearer and easier to maintain with them all grouped together at the end.

problem with sql table updation

I have 2 tables
CREATE TABLE PODRAS_MS.tbl_FieldWorkers
(
[FWInsOnServerID] INT NOT NULL IDENTITY(1,1),
[BaseStationID] INT NOT NULL,
[RefID] INT NOT NULL,
[DisSubInsID] INT NOT NULL,
[FieldWorkerID] CHAR(7) NOT NULL,
[LastRecDate] DATETIME NOT NULL,
)
ALTER TABLE PODRAS_MS.tbl_FieldWorkers ADD CONSTRAINT PK_FieldWorkers_FWInsOnServerID PRIMARY KEY([FWInsOnServerID])
ALTER TABLE PODRAS_MS.tbl_FieldWorkers ADD CONSTRAINT FK_FieldWorkers_DisSubInsID FOREIGN KEY([DisSubInsID]) REFERENCES PODRAS_MS.tbl_DisasterSubInstances([SubInsID]) ON UPDATE CASCADE ON DELETE NO ACTION
ALTER TABLE PODRAS_MS.tbl_FieldWorkers ADD CONSTRAINT FK_FieldWorkers_BaseStationID FOREIGN KEY([BaseStationID]) REFERENCES PODRAS_MS.tbl_BaseStations([BaseStationID]) ON UPDATE CASCADE ON DELETE NO ACTION
ALTER TABLE PODRAS_MS.tbl_FieldWorkers ADD CONSTRAINT DF_FieldWorkers_LastRecDate DEFAULT(GETDATE()) FOR [LastRecDate]
GO
CREATE TABLE PODRAS_MS.tbl_FieldWorkerNodeGPSLocations
(
[FWNStatID] INT NOT NULL IDENTITY(1,1),
[FWInsOnServerID] INT NOT NULL,
[Latitude] DECIMAL(20,17) NOT NULL,
[Longitude] DECIMAL(20,17) NOT NULL,
[UpdateOn] DATETIME NOT NULL,
)
ALTER TABLE PODRAS_MS.tbl_FieldWorkerNodeGPSLocations ADD CONSTRAINT PK_FieldWorkerNodeGPSLocations_FWNStatID PRIMARY KEY([FWNStatID])
ALTER TABLE PODRAS_MS.tbl_FieldWorkerNodeGPSLocations ADD CONSTRAINT FK_FieldWorkerNodeGPSLocations_FWInsOnServerID FOREIGN KEY([FWInsOnServerID]) REFERENCES PODRAS_MS.tbl_FieldWorkers([FWInsOnServerID]) ON UPDATE CASCADE ON DELETE NO ACTION
ALTER TABLE PODRAS_MS.tbl_FieldWorkerNodeGPSLocations ADD CONSTRAINT DK_FieldWorkerNodeGPSLocations_UpdateOn DEFAULT(GETDATE()) FOR [UpdateOn]
GO
Both tables are updated through a webservice in the 1st table all the fields can be inserted through the web service but in the second table only data for [Latitude],[Longitude],[UpdateOn] fields comes through the webservice.so my problem is how can i insert the values to [FWInsOnServerID] field since its not comes through the webservice and its a reference for the 1st table???
If I understand you correctly, the insert in the second table is dependant on the result of the insert in your first table?
In this case, you could simply return the generted ID of the first table and use that result to insert into the second table.
Something like (if you're using Stored Procedures).
SELECT SCOPE_IDENTITY()
at the end of your stored procedure. And then in your .NET code which handles the database code, use that number to do the second insert.
You could do it all in a single stored procedure like this:
CREATE PROCEDURE PODRAS_MS.insert_FieldWorker()
#BaseStationID INT,
#RefID INT,
#DisSubInsID INT,
#FieldWorkerID CHAR(7),
#Latitude DECIMAL(20,17),
#Longitude DECIMAL(20,17)
AS
BEGIN
INSERT INTO
PODRAS_MS.tbl_FieldWorkers
([BaseStationID], [RefID], [DisSubInsID], [FieldWorkerID])
VALUES (#BaseStationID, #RefID, #DisSubInsID, #FieldWorkerID)
DECLARE #FWInsOnServerID INT
SELECT #FWInsOnServerID = SCOPE_IDENTITY()
INSERT INTO PODRAS_MS.tbl_FieldWorkerNodeGPSLocations
([FWInsOnServerID], [Latitude], [Longitude])
VALUES (#FWInsOnServerID, #Latitude, #Longitude)
END
You could then select the records from the same stored procedure, but it is more common to separate this out into another stored proc.
EDIT: use an output parameter
CREATE PROCEDURE PODRAS_MS.insert_FieldWorker()
#BaseStationID INT,
#RefID INT,
#DisSubInsID INT,
#FieldWorkerID CHAR(7),
#FWInsOnServerID INT output
AS
BEGIN
INSERT INTO
PODRAS_MS.tbl_FieldWorkers
([BaseStationID], [RefID], [DisSubInsID], [FieldWorkerID])
VALUES (#BaseStationID, #RefID, #DisSubInsID, #FieldWorkerID)
SELECT #FWInsOnServerID = SCOPE_IDENTITY()
END

Resources