Oracle SQL Developer - renaming attributes causes errors - sql

I'm having a weird situation going on in Oracle SQL Developer. I had to rename some attributes to match my school's variable names standards and when I am executing the program, the new attribute names doesn't seem to have been taken into account.
For example, I'm creating a table named VARIANT like this :
create table VARIANT(
ID_VAR varchar(9) not null,
NUCL_VAR char(1) not null,
NUCL_REF_VAR char(1) not null,
CHROMOSOME_VAR number(2) not null,
GENE_VAR varchar(5) not null,
POSITION_VAR number(4) not null,
EFFET_VAR varchar(100) not null,
INDEX_EFF_VAR number(2, 2) not null,
URL_DRUGBANK_VAR varchar(500) not null,
constraint PK_VARIANT primary key(ID_VAR),
constraint FK_URL_DRUGBANK_VAR foreign key(URL_DRUGBANK_VAR)
references DRUG(URL_DRUGBANK_DRO),
constraint DOM_INDEX_EFF_VAR check(INDEX_EFF_VAR > 0)
);
My problem occurs when I execute the table creation request. The output in SQLDev's console doesn't match my code :
create table VARIANT(
ID_VAR varchar(9) not null,
NUCL_VAR char(1) not null,
NUCL_REF_VAR char(1) not null,
CHROMOSOME_VAR number(2) not null,
GENE_VAR varchar(5) not null,
POSITION_VAR number(4) not null,
EFFET_VAR varchar(100) not null,
INDEX_EFF_VAR number(2, 2) not null,
URL_DRUGBANK_VAR varchar(500) not null,
constraint PK_VARIANT primary key(ID_VAR),
constraint FK_URL_DRUGBANK_VAR foreign key(URL_DRUGBANK_VAR)
references DROGUE(URL_DRUGBANK_DRO),
constraint DOM_INDEX_EFF check(INDEX_EFF > 0)
)
Error report -
00904. 00000 - "INDEX_EFF: invalid identifier"
*Cause:
*Action:
As you can see, in my code, the attribute INDEX_EFF is renamed INDEX_EFF_VAR in the constraint below but the change does not seem to have taken effect in the console's output for the constraint, even though the attribute name has been changed properly.
If someone could help me figure out what is going on here, that would be appreciated! Thanks!
EDIT:
Found my way out of it. Seems that rebooting SQL Developer fixed the issue. But still, I'd like to know if there is another way of fixing this.

As we can clearly see in the output, there is no such column name "INDEX_EFF". There might be some kind of user error here, you may try rerunning the SQL after clearing the console output to avoid any confusions.
Please note, you may try the options like drop and recreate table. If you don't want to loose the existing data, then follow these steps
Drop the constraints
Rename the column
then finally add the constraint
Hope this helps you, though i can't say what actually gone wrong.

Primary key name is also wrong, check the key names.

INDEX_EFF should be INDEX_EFF_VAR

Related

Syntax error in CREATE TABLE statement - SQL (MS Access 2016)

I took this example from my class textbook and pretty much copied word for word from the text. The following is the code that I wrote into MS Access:
CREATE TABLE PRODUCT(
P_CODE VARCHAR(10) NOT NULL UNIQUE,
P_DESCRIPT VARCHAR(35) NOT NULL,
P_INDATE DATE NOT NULL,
P_QOH SMALLINT NOT NULL,
P_MIN SMALLINT NOT NULL,
P_PRICE NUMBER(8, 2) NOT NULL,
P_DISCOUNT NUMBER(5, 2) NOT NULL,
V_CODE INTEGER,
PRIMARY KEY (P_CODE),
FOREIGN KEY (V_CODE) REFERENCES VENDOR ON UPDATE CASCADE
);
This code produces a syntax error every time it's run within MS Access.
I tried running this query to create the table PRODUCT within my db. When reviewing the code, I couldn't find anything specific that would have caused the error, but I could be wrong due to my inexperience with SQL.
Any help would be greatly appreciated!
Per the comments the problem is the Decimal type. I'm updating the answer here for Access 2016: How do I create a decimal field in Access with Alter Table?
First enable the SQL Ansi 92 standard. In 2016 this has moved to File-Options-Object Designers-Query Design. It seems you can only enable it for new databases. So do so and create a new database then open the sql-tab of a blank query and paste the following code:
'make sure you have a VENDOR table first for instance:
CREATE TABLE VENDOR
(V_CODE AutoIncrement CONSTRAINT PrimaryKEY PRIMARY KEY);
'Then with slightly less old syntax (varchar would usually be text, smallint would usually be integer with a size, and PrimaryKey includes Not null and unique)
CREATE TABLE PRODUCTS(
P_CODE VARCHAR(10) CONSTRAINT PrimaryKey PRIMARY KEY,
P_DESCRIPT VARCHAR(35) NOT NULL,
P_INDATE DATE NOT NULL,
P_QOH SMALLINT NOT NULL,
P_MIN SMALLINT NOT NULL,
P_PRICE DECIMAL(8, 2) NOT NULL,
P_DISCOUNT DECIMAL(5, 2) NOT NULL,
V_CODE LONG REFERENCES VENDOR(V_CODE)
);
'Alter Table also works now
ALTER TABLE PRODUCTS ADD COLUMN P_PRICE DECIMAL(8,2) NOT NULL;
Caveat
At least the sql-pane tells you that something in your DDL is wrong if not what. if you are not using the default Jet database as your backend then you are stuck concatenating strings using vba and ADO according to here: https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/create-table-statement-microsoft-access-sql
At that point I would manually try manually adding the decimal columns if there where not too many.

Length or precision specification 0 is invalid

I have an MFC app which run some SQL statements, and I have run them on several PCs, without any problem.
But, I have met an PC, where when I have tried to run these SQLs, I have encountered an strange error (strange for me, I know SQL a little):
Length or precision specification 0 is invalid
This error fired on the following SQL:
CREATE TABLE mytable (
nmytable_ID int IDENTITY (1, 1) PRIMARY KEY NOT NULL,
nAsset_ID int FOREIGN KEY REFERENCES t_asset (nAsset_ID) DEFAULT NULL,
dtInit date NOT NULL,
dRest decimal(10, 2) NULL,
nOdo int NULL,
sObs varchar(1024) NULL,
dtDateC datetime2(0) NULL,
dtDateU datetime2(0) NULL,
nUserC int FOREIGN KEY REFERENCES ntp_appuserx (nAppUserx_ID) DEFAULT NULL,
nUserU int FOREIGN KEY REFERENCES ntp_appuserx (nAppUserx_ID) DEFAULT NULL,
CONSTRAINT nAsset_ID_mytable UNIQUE (nAsset_ID, dtInit)
)
Ok, I have changed DATETIME2(0) data type to DATETIME, but then I get the following error:
DATE data type is unknown
There is a question: why this error(s), but on an PC, and not for others ? Is there some local settings which affect my app ? I am using SQL Server 2012.
Solved
I have tried to create the table on older DB installed on this PC, without to notice that.

how do I finish this conversion from SQL Server to Oracle?

I have a partially translate SQL script and several like it that look something like this:
CREATE TABLE STATUSES
( STATUS_ID CHAR(1) NOT NULL,
DESCRIPTION VARCHAR2(50) NOT NULL,
EVENT NUMBER(3) NOT NULL,
RESOLUTION NUMBER(3) NOT NULL,
CONSTRAINT PK_STATUSES PRIMARY KEY CLUSTERED (STATUS_ID ASC));
They were tables defined in SQL Server and I am looking to translate them for use in Oracle, however I am unsure how to handle the CLUSTERED part of the constraint and the ASC attribute of the Primary Key, I have seen some articles out there but they have served to be unhelpful. Can anyone help me complete this, or point me to an article that can help?
Just ignore them. You can remove both the clustered and the asc and the table should be fine in both databases.
I would be inclined to just use the primary key keyword, instead of an explicit constraint:
CREATE TABLE STATUSES (
STATUS_ID CHAR(1) NOT NULL PRIMARY KEY,
DESCRIPTION VARCHAR2(50) NOT NULL,
EVENT NUMBER(3) NOT NULL,
RESOLUTION NUMBER(3) NOT NULL
);
Your table is too small to worry about clustered indexes. It is questionable whether any index at all is useful on such a small table.

Error creating table in oracle

I am trying to create a table in oracle but I am getting this error: unknown command ")" - rest of line ignored. I can't figure out what is causing this error. Below is my SQL for the table:
CREATE TABLE PAYMENT
(PayNum INT NOT NULL PRIMARY KEY,
CType VARCHAR(1) NOT NULL,
CCNum VARCHAR(16) NOT NULL,
BankName VARCHAR(75) NOT NULL,
AccNum INT NOT NULL,
PDate DATE NOT NULL,
Amt DECIMAL(11,2) NOT NULL,
CONSTRAINT fk_BANKACC_PAYMENT FOREIGN KEY (BankName, AccNum)
REFERENCES BANKACC(BankName, AccNum),
CONSTRAINT fk_CRCARD_PAYMENT FOREIGN KEY (CType, CCNum)
REFERENCES CRCARD(CType, CCNum)
);
Your code is correct. Make sure you are referencing primary keys (all 4).
Check this: http://sqlfiddle.com/#!2/7be70/1/0
If you do not follow that, you may get this error: There are no primary or candidate keys in the referenced table 'BANKACC' that match the referencing column list in the foreign key 'fk_BANKACC_PAYMENT'.
Code in the fiddle above:
CREATE TABLE BANKACC
(BankName VARCHAR(75) NOT NULL,
AccNum INT NOT NULL,
PRIMARY KEY(BankName, AccNum));
CREATE TABLE CRCARD
(CType VARCHAR(1) NOT NULL,
CCNum VARCHAR(16) NOT NULL,
PRIMARY KEY(CType, CCNum));
CREATE TABLE PAYMENT
(PayNum INT NOT NULL PRIMARY KEY,
CType VARCHAR(1) NOT NULL,
CCNum VARCHAR(16) NOT NULL,
BankName VARCHAR(75) NOT NULL,
AccNum INT NOT NULL,
PDate DATE NOT NULL,
Amt DECIMAL(11,2) NOT NULL,
CONSTRAINT fk_BANKACC_PAYMENT FOREIGN KEY (BankName, AccNum)
REFERENCES BANKACC(BankName, AccNum),
CONSTRAINT fk_CRCARD_PAYMENT FOREIGN KEY (CType, CCNum)
REFERENCES CRCARD(CType, CCNum)
);
Also you should read this for better understanding on how to implement foreign key constraint: http://docs.oracle.com/cd/E17952_01/refman-5.5-en/create-table-foreign-keys.html
If you're running this in SQL*Plus, get rid of the blank line between REFERENCES and ):
REFERENCES CRCARD(CType, CCNum)
);
Or set sqlblanklines on to change the behaviour.
By default it interprets a blank line as the end of the statement, but doesn't run it. So your entire CREATE TABLE command is essentially ignored, and the ); is treated as a stanalone command. Hence the error message you get, since it doesn't mean anything on its own.
Please use NUMBER for numeric columns.
http://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm#CNCPT313
Also, in Oracle, use VARCHAR2 for strings.
but of it your syntax should be correct.

ORA-00905: missing keyword (constraint foreign key)

hi everyone I'm completely new to SQL and I'm trying to answer this question in my book:
7.5 Write a CREATE TABLE statement for the EMPLOYEE table. Email is required and is an alternate key, and the default value of Department is Human Resources. Cascade updates but not deletions from DEPARTMENT to EMPLOYEE.
I'm running the query in Oracle iSQL*Plus, I successfully created the department table, but when i tried creating the employee table while meeting these requirements I get the missing keyword error at line 12
constraint DepartmentFK FOREIGN KEY(DepartmentName),
this is the whole query (I dropped the department table before attempting the whole thing, and it still gives the same error)
CREATE TABLE DEPARTMENT (
DepartmentName char(35) NOT NULL,
BudgetCode char(30) NOT NULL,
OfficeNumber char(15) NOT NULL,
Phone char(12) NOT NULL,
Constraint DepartmentPK PRIMARY KEY(DepartmentName)
);
CREATE TABLE EMPLOYEE (
ProjectID int NOT NULL,
Name char(30) NOT NULL,
Department char(15) NOT NULL,
MaxHours int NOT NULL,
StartDate char(8) NULL,
EndDate char(8) NULL,
Email char(30) DEFAULT 'Human Resources' NOT NULL,
Constraint EmployeePK PRIMARY KEY(ProjectID),
Constraint EmployeeAK1 UNIQUE(Email),
constraint DepartmentFK FOREIGN KEY(DepartmentName),
references DEPARTMENT(DepartmentName)
ON UPDATE CASCADE
ON DELETE no ACTION
);
I tried following the most similar example in the book and looking up foreign key constraint and references but i can't understand why I'm getting this error...
EDIT:
I took out the comma but i still got these two errors:
CREATE TABLE DEPARTMENT (
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
ON UPDATE CASCADE
*
ERROR at line 14:
ORA-00905: missing keyword
You have to remove the comma you have used after
constraint DepartmentFK FOREIGN KEY(DepartmentName),
This is one unit
constraint DepartmentFK FOREIGN KEY(DepartmentName) references DEPARTMENT(DepartmentName)
EDIT:
Since, you have edited your question -
ON UDDATE CASCADE option is not available in Oracle Database, and that's why you're getting an error.
You're getting an error for Department table, since the table already exists, probably from the last run!