Oracle SQL ALTER TABLE error [closed] - sql

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have code like this:
ALTER TABLE "VMA_CSDD" ADD CONSTRAINT "VMA_CSDD_PK" PRIMARY KEY ("ID") ENABLE
ALTER TABLE "VMA_CSDD" MODIFY ("ID" NOT NULL ENABLE)
ALTER TABLE "VMA_CSDD" MODIFY ("CSDD_NAME" NOT NULL ENABLE)
It gives me an error:
ALTER TABLE "VMA_CSDD" MODIFY ("ID" NOT NULL ENABLE)
ORA-01735: invalid ALTER TABLE option
How do I fix this?

Option ENABLE is not correct with column definition you have to also add types
ALTER TABLE "VMA_CSDD" MODIFY ("ID" int NOT NULL )
ALTER TABLE "VMA_CSDD" MODIFY ("CSDD_NAME" varchar(100) NOT NULL )

Related

Temporary table both exists and doesn't exist [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to drop a temporary table in SQL Server if it exists and create a new temp table if it doesn't exist. The command IF OBJECT_ID('tempdb.. #my_temp') IS NULL seems unable to fine the temp table and then when the query tries to create the table, I receive an error that the temp table already exists.
This is my query:
IF OBJECT_ID('tempdb.. #my_temp') IS NOT NULL
BEGIN
DROP TABLE #my_temp
END
IF OBJECT_ID('tempdb.. #my_temp') IS NULL
BEGIN
print 'trying to create a new temp table'
CREATE TABLE #my_temp
( [ID] int NOT NULL PRIMARY KEY CLUSTERED ,
) ON [PRIMARY]
END
If I run this query twice, on the second time I receive this output:
trying to create a new temp table
Msg 2714, Level 16, State 6, Line 12
There is already an object named '#my_temp' in the database.
My SQL Server is version 2019 for your info.
Remove the space between .. and the table name
IF OBJECT_ID('tempdb..#my_temp') IS NOT NULL
BEGIN
DROP TABLE #my_temp
END
IF OBJECT_ID('tempdb..#my_temp') IS NULL
BEGIN
print 'trying to create a new temp table'
CREATE TABLE #my_temp
( [ID] int NOT NULL PRIMARY KEY CLUSTERED ,
) ON [PRIMARY]
END

Adding AUTO_INCREMENT using ALTER in sql [duplicate]

This question already has answers here:
How to create id with AUTO_INCREMENT on Oracle?
(18 answers)
Closed 4 years ago.
I've been trying to add AUTO_INCREMENT on a created table , but the ALTER table query is not working
My Table:
Query and Error:
And also
ALTER TABLE professor ADD sno INT IDENTITY;
is not working
Can you please try:
alter table professor add sno integer generated by default on null as identity;
EDIT: Sorry, the OP is asking for 11g, as the code above works after 12.
Then you need to use sequence. Please see below:
ALTER TABLE professor ADD sno INT;
CREATE SEQUENCE dept_seq START WITH 1;
And you need to set a trigger for your sequence like:
CREATE OR REPLACE TRIGGER dept_bir
BEFORE INSERT ON departments
FOR EACH ROW
BEGIN
SELECT dept_seq.NEXTVAL
INTO :new.id
FROM dual;
END;
Reference: How to create id with AUTO_INCREMENT on Oracle?

alter column with default value [duplicate]

This question already has answers here:
Add a column with a default value to an existing table in SQL Server
(43 answers)
Alter column, add default constraint
(7 answers)
Closed 5 years ago.
alter table enquiry alter column RejectReson int default 0
I want alter the column data type with default value zero,but it is not happening.
ALTER TABLE enquiry ADD DEFAULT 0 FOR RejectReson
If you want to affect existing rows, then use with values:
alter table enquiry
add constraint dft_enquiry_rejectreason default 0 with values;
This is explained in the documentation:
. . . WITH VALUES can be used to store the default value in the new column for each existing row in the table.
Use ADD CONSTRAINT.
For example:
ALTER TABLE enquiry ADD CONSTRAINT some_name DEFAULT 0 FOR RejectReson;

Oracle check constraint1 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a column Hire_date in table x with data
01-jun-98
16-aug-99
02-feb-09
01-mar-06
-
01-dec-08
17-mar-99
16-feb-07
I was asked to create a check constraint on that Hire_date column. Check that the hire_date comes after 2000. I see an error
I tried again to check if the hire_date comes after 1996. The constraint was successfully created.
why did i see an error when i created a check constraint for hire_date>2000 ?
Code:
alter table x
add constraint check_hire_date check(hire_Date>'01-jan-2000')
got error
alter table x
add constraint check_hire_date check(hire_Date>'01-jan-1996')
successful
The reason you got error is obvious. Some of your data violate the constraint. When you add a constraint to a table, it will validate the all data to the constraint.
You can use novalidate option to skip the validation. If you apply novalidate option when you create the constraint, you can add the constraint while you don't validate the old(=current) date with the constraint. The constraint will affect to the data which is inserted after the creation of the constraint.
sql> create table x (hire_date date);
sql> insert into x values ('1998-01-01');
sql> insert into x values ('2000-01-05');
sql> select * from x;
HIRE_DAT
--------
98/01/01
00/01/05
sql> alter table x add constraint d_check check
2 (hire_date >= to_date('2000-01-01', 'yyyy-mm-dd'))
3 novalidate;
sql> insert into x values ('1999-01-01');
insert into x values ('1999-01-01')
*
error at line 1:
check constraint (d_check check) violated

UPDATE Statement not working. what's wrong [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm SORRY everyone, It wasn't allowing me to comment. you are right. It's the ALTER I'm having issues with. How do I add the Cumulative GPA column to the table (using the alter statement) where the gpa is displayed between 0.00 and 4.00
CREATE database "IS4440_DuBoseJasmine"
CREATE TABLE StudentInformation (
StudentID CHAR(7) not null,
StudentSSN CHAR(9) null,
StudentFirstName VARCHAR(50) null,
StudentLastName VARCHAR(50) null,
StudentMiddleName VARCHAR(50) null,
StudentHomeCountry CHAR(2) not null
)
/*2*/
ALTER TABLE StudentInformation ADD Cumulative GPA ;
/*3*/
INSERT INTO StudentInformation (StudentID, StudentLastName, StudentFirstName, StudentMiddleName, StudentHomeCountry)
VALUES ('1352154', 'DuBose', 'Jasmine', 'Leigh', 'US')
INSERT INTO StudentInformation (StudentID, StudentLastName, StudentFirstName, StudentMiddleName, StudentHomeCountry)
VALUES ('1234565', 'Smith', 'Johnny', 'Apple', 'GB');
/*4*/
UPDATE StudentInformation SET StudentSSN = 123456789
WHERE StudentID = 1352154;
The update works fine (demonstrated on SQL Fiddle), it is the ALTER TABLE statement that fails. This line:
ALTER TABLE StudentInformation ADD Cumulative GPA;
should be:
ALTER TABLE StudentInformation ADD [Cumulative GPA] INT -- OR WHATEVER TYPE IT SHOULD BE;
ALTER TABLE Documentation
As an aside, although this may just be an example, if it isn't you will want to USE your database before you create the table:
CREATE database "IS4440_DuBoseJasmine";
GO
USE IS4440_DuBoseJasmine;
CREATE TABLE ...
Otherwise you will just be creating your table in whatever database you are connected to.
Your update query is fine, Your ALTER statement what fails, You are adding new column to your table without any datatype or constrains, try this to alter your table
You can use Numeric(3,2) or Decimal(3,2), that means total three digits two after decimal point so a max of 9.99 .
ALTER TABLE StudentInformation ADD CumulativeGPA Numeric(3,2) NULL
To alter your table to add new column make that column NULLABLE and while naming your column do not give spaces.
To make column type Not Nullable
ALTER TABLE StudentInformation ALTER COLUMN CumulativeGPA DECIMAL(3,2) NULL ;