I create a table and set one Integer column as a primary key.Now I want to set Auto Increment by 1 to that column.I am using this query but getting error.
ALTER TABLE APP.DocumentGroupCategory
ALTER ID INTEGER NOT NULL
GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1);
How can I alter column without drop table and create once again?
Related
EDIT: I have replaced the generic SQL with the actual SQL.
I know how to fix it. I am just wondering why it worked on some developer machines and not others.
CREATE TABLE OUTPUTDEFNACTIVATION ("OUTPUTDEFNACTIVATIONID" NUMBER, "OUTPUTDEFNTYPE" NUMBER(1,0) NOT NULL, "OUTPUTDEFNID" NUMBER NOT NULL, "PROJECTVIEWID" NUMBER, "SYSTEMUSERID" NUMBER, "VISIBLE" NUMBER(3,0));
ALTER TABLE OUTPUTDEFNACTIVATION ADD CONSTRAINT OUTPUTDEFNACTIVATION_P PRIMARY KEY (OUTPUTDEFNACTIVATIONID) ENABLE;
ALTER TABLE OUTPUTDEFNACTIVATION ADD CONSTRAINT OUTPUTDEFNACTIVATION_PROJECT FOREIGN KEY (PROJECTVIEWID) REFERENCES PROJECTVIEW (PROJECTVIEWID) ON DELETE CASCADE ENABLE;
ALTER TABLE OUTPUTDEFNACTIVATION ADD CONSTRAINT OUTPUTDEFNACTIVATION_USER FOREIGN KEY (SYSTEMUSERID) REFERENCES SYSTEMUSER (SYSTEMUSERID) ON DELETE CASCADE ENABLE;
ALTER TABLE OUTPUTDEFNACTIVATION ADD CONSTRAINT UNIQUE_OUTPUTDEFNACTIVATION UNIQUE (OUTPUTDEFNTYPE, OUTPUTDEFNID, SYSTEMUSERID, PROJECTVIEWID);
CREATE SEQUENCE "OUTPUTDEFNACTIVATION_SEQ" MINVALUE 1 MAXVALUE 1.00000000000000E+27 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE;
INSERT INTO OUTPUTDEFNACTIVATION (OUTPUTDEFNTYPE, OUTPUTDEFNID, SYSTEMUSERID, PROJECTVIEWID, VISIBLE)
SELECT 0, KPIDEFNID, SYSTEMUSERID, PROJECTVIEWID, VISIBLE FROM KPIDEFN, SYSTEMUSER, PROJECTVIEW, DUAL WHERE ACTIVE = 1 AND SYSTEMUSERID <> 1 AND STATUSID IN (1,4);
My fix is
INSERT INTO OUTPUTDEFNACTIVATION (OUTPUTDEFNACTIVATIONID, OUTPUTDEFNTYPE, OUTPUTDEFNID, SYSTEMUSERID, PROJECTVIEWID, VISIBLE)
SELECT OUTPUTDEFNACTIVATION_SEQ.NEXTVAL, 0, KPIDEFNID, SYSTEMUSERID, PROJECTVIEWID, VISIBLE FROM KPIDEFN, SYSTEMUSER, PROJECTVIEW, DUAL WHERE ACTIVE = 1 AND SYSTEMUSERID <> 1 AND STATUSID IN (1,4);
But why wasn't it necessary on all developer machines? They all have their own schema, but my script created this table for them all.
ORA-01400: cannot insert NULL into ("DEVF22FMT"."OUTPUTDEFNACTIVATION"."OUTPUTDEFNACTIVATIONID")
The same table behaves the same way, so - if all of them (developers) used the same INSERT statement, connected to the same database (user), they'd get the same result - either success or a failure.
However, if they have their own schemas, then it depends on the way they created target tables.
Behaviour you described could be result of
identity column (12c onwards)
database trigger
which then "automatically", in the background, populate that NOT NULL column.
Could anyone help me with this please. I have a table that's not unique (as I'm rummaging through old databases of my predecessor.)
I would like to assign it to the "ID" field within the Fruits table I have.
I'd like to go ahead and get the NEWSEQUENTIALID() to be setup so I can see all what I'm working with.
Assuming ID is of type uniqueidentifier, you can create another column with sequential guids as default.This will populate the values in that column. After that you may copy these values to your id column and then drop the tmp column. Once all data is in, then specify defaults for your id column. See SQL Script below :
--create a new column with default as sequential ids
USE [BASKET]
ALTER TABLE [FRUITS]
ADD [TMPID] UNIQUEIDENTIFIER NOT NULL CONSTRAINT DF_TMPID DEFAULT NEWSEQUENTIALID()
GO
--update existing id column values with the newly created values
UPDATE [FRUITS] SET ID = TMPID GO
--remove constraint
ALTER TABLE [FRUITS] DROP CONSTRAINT DF_TMPID GO
--remove the temp column
ALTER TABLE [FRUITS] DROP COLUMN TMPID GO
--specify defaults for newly inserted defaults
ALTER TABLE [FRUITS] ADD DEFAULT NEWSEQUENTIALID() FOR ID
--or--
ALTER TABLE [FRUITS] ADD CONSTRAINT DF_ROWGUID DEFAULT NEWSEQUENTIALID() FOR ID;
CREATE PROCEDURE [dbo].[uspGetSequentialGuid]
AS
DECLARE #SequentialGuids as Table ( SequentialGuid uniqueidentifier DEFAULT NEWSEQUENTIALID() PRIMARY KEY,InitDate datetime )
BEGIN
INSERT INTO #SequentialGuids(InitDate) Values(GETDATE());
END
SELECT SequentialGuid from #SequentialGuids
GO
CREATE PROCEDURE [dbo].[uspGetSequentialGuids](#RequiredGuids as int)
AS
DECLARE #SequentialGuids as Table ( SequentialGuid uniqueidentifier DEFAULT NEWSEQUENTIALID() PRIMARY KEY,InitDate datetime )
Declare #Counter int
SET #Counter = 0
While #Counter < #RequiredGuids
BEGIN
INSERT INTO #SequentialGuids(InitDate) Values(GETDATE());
SET #Counter = #Counter + 1
END
SELECT SequentialGuid from #SequentialGuids
GO
I have a table with three columns, [Id,QTY,Date]. out of these three, two columns [id and date], should be set as primary keys, because I need to fetch the record one by one, from this table, into a reference.
the data to be inserted into this table is
101,10,NULL
101,20,201220
101,7,201440
102,5,null
102,8,201352
date is in yyyyww format
How do I define two columns as composite primary keys when they have null values, duplicates?
alter table abc add constraint pk primary key (ID, DATE);
if I try to alter the table the error appears
Error report:
SQL Error: ORA-01449: column contains NULL values; cannot alter to NOT NULL
01449. 00000 - "column contains NULL values; cannot alter to NOT NULL"
*Cause:
*Action:
Using table level constraint, you can use this query
alter table your_table add constraint pkc_Name primary key (column1, column2)
but first you need to declare the columns NOT NULL. All parts of a primary key need to be NOT NULL.
The column name of your table is ID and it is still null and non-unique, how is it possible. If it is primary key of other table try adding a surrogate key column for this table and make it primary key.
In case of composite primary key, it should have atleast one not null value(For each row) in the combination of columns. And the combination of column must be unique at all case.
For further details check, http://docs.oracle.com/cd/B10500_01/server.920/a96524/c22integ.htm
Correction - If composite primary key is made up of 3 columns, then no column (among 3) can hold NULL value. And the combination of those 3 columns must be unique.
E.g. (1,2,2)
(1,2,1)
(2,2,1)
(1,2,2) - not valid
When I try to add a sorted ASC index on the field MyId the oracle sql developer tool says:
Index MyIdSortedIndex is defined identically to constraint PK_MyTable
CREATE TABLE MyTable
( "MyId" NVARCHAR2(50),
"DESCRIPTION" NVARCHAR2(200),
CONSTRAINT "PK_MyTable" PRIMARY KEY ("MyId")
)
How can I set a sorted ASC index on my tables Primary Key MyId in Oracle ?
It is telling you that you don't need a new index, because you already have an index called "PK_MyTable" on "MyId" in ascending order. When you add a primary key constraint to a table Oracle automatically adds an index of the same name to help enforce it, indexing the columns of the constraint in ascending order (since ascending is the default direction).
If you don't want to use the index Oracle created automatically you can do this:
CREATE TABLE MyTable
( "MyId" NVARCHAR2(50),
"DESCRIPTION" NVARCHAR2(200),
);
CREATE INDEX MyIdSortedIndex ON MyTable ("MyId" ASC);
ALTER TABLE MyTable Add CONSTRAINT "PK_MyTable" PRIMARY KEY ("MyId");
Since your index already exists when the primary key is added Oracle will use that instead of creating its own.
I create a table in Oracle 11g with the default value for one of the columns. Syntax is:
create table xyz(emp number,ename varchar2(100),salary number default 0);
This created successfully. For some reasons I need to create another table with same old table structure and data. So I created a new table with name abc as
create table abc as select * from xyz.
Here "abc" created successfully with same structure and data as old table xyz. But for the column "salary" in old table "xyz" default value was set to "0". But in the newly created table "abc" the default value is not set.
This is all in Oracle 11g. Please tell me the reason why the default value was not set and how we can set this using select statement.
You can specify the constraints and defaults in a CREATE TABLE AS SELECT, but the syntax is as follows
create table t1 (id number default 1 not null);
insert into t1 (id) values (2);
create table t2 (id default 1 not null)
as select * from t1;
That is, it won't inherit the constraints from the source table/select. Only the data type (length/precision/scale) is determined by the select.
The reason is that CTAS (Create table as select) does not copy any metadata from the source to the target table, namely
no primary key
no foreign keys
no grants
no indexes
...
To achieve what you want, I'd either
use dbms_metadata.get_ddl to get the complete table structure, replace the table name with the new name, execute this statement, and do an INSERT afterward to copy the data
or keep using CTAS, extract the not null constraints for the source table from user_constraints and add them to the target table afterwards
You will need to alter table abc modify (salary default 0);
new table inherits only "not null" constraint and no other constraint.
Thus you can alter the table after creating it with "create table as" command
or you can define all constraint that you need by following the
create table t1 (id number default 1 not null);
insert into t1 (id) values (2);
create table t2 as select * from t1;
This will create table t2 with not null constraint.
But for some other constraint except "not null" you should use the following syntax
create table t1 (id number default 1 unique);
insert into t1 (id) values (2);
create table t2 (id default 1 unique)
as select * from t1;