I'm doing a very simple SQL insert, which was working fine, until I added a UID column and now it won't work. A SQL syntax checker said this code is bad, but I can't see it. What;s the obvious thing here I'm missing? I've checked spellings, column names and db name all match.
INSERT INTO
virtualTree (PATH, NPATH, PUID, NAME, LEVEL, INDEX, UID)
VALUES(
'/data/Mitsubishi/Sys Info',
'/downstream/dgapi/DSA_Dev/config/sysInfo',
'',
'Sys Info',
'1',
'1',
'1532750575860'
);
Thanks!
You didn't specify database you use.
Furthermore, saying that you got a message "this code is bad" is quite useless. Computers don't write poetry, so shouldn't you. There was probably a very specific error message, along with the error code. For example, Oracle might have told you ORA-00904: : invalid identifier which is way more clear than "this code is bad". I suggest you to be more specific.
The following example shows what happens when you try to create a table using "invalid" column names (because they might be reserved words). It is based on Oracle. I removed "valid" column names to keep it simpler.
SQL> create table virtualtree
2 (level varchar2(20),
3 index varchar2(20),
4 uid varchar2(20));
(level varchar2(20),
*
ERROR at line 2:
ORA-00904: : invalid identifier
SQL> create table virtualtree
2 (c_level varchar2(20),
3 index varchar2(20),
4 uid varchar2(20));
index varchar2(20),
*
ERROR at line 3:
ORA-00904: : invalid identifier
SQL> create table virtualtree
2 (c_level varchar2(20),
3 c_index varchar2(20),
4 uid varchar2(20));
uid varchar2(20))
*
ERROR at line 4:
ORA-00904: : invalid identifier
SQL> create table virtualtree
2 (c_level varchar2(20),
3 c_index varchar2(20),
4 c_uid varchar2(20));
Table created.
SQL>
See? I couldn't create a table whose names were LEVEL, INDEX, UID - I had to modify them, somehow; I chose to add the C_ prefix.
I suggest you try the same. If what you said is true, your database complains about the UID column name.
If it doesn't help, back to the beginning - post actual error code and message, as well as the database you use.
Related
I'm making two tables, STUDENT and STUDENTREPORT. I made STUDENT, but not the other. I ran the code, but it says missing or invalid option.
My STUDENTREPORT command:
Both commands are correctly written, they execute OK in e.g. SQL*Plus or SQL Developer:
SQL> CREATE TABLE studentreport (
2 sr_number VARCHAR2(5),
3 sr_rade VARCHAR2(5),
4 sr_semester VARCHAR2(5),
5 class_attended NUMBER,
6 s_id VARCHAR2(5),
7 PRIMARY KEY ( sr_number )
8 USING INDEX enable
9 );
Table created.
SQL> ALTER TABLE studentreport
2 ADD FOREIGN KEY ( s_id )
3 REFERENCES student ( s_id )
4 ENABLE;
Table altered.
SQL>
But, in Oracle Apex' SQL Workshop, you can execute only one command at a time. Therefore:
remove alter table (delete it from the editor) so that you'd first execute create table; then delete that statement and execute alter table, or
select (with a mouse, so that text turns blue) create table and hit RUN to execute it; then select alter table and execute it with RUN
That's just how SQL Workshop behaves, there's nothing you can do about it (at least, I don't know what you could/should do, apart from what I already said).
I got question about using trigger to insert data, for instance, I do have two tables, and second table has attributes and records with table, except additional two attributes, like below:
CREATE TABLE dept
(
DEPTNO NUMBER(3) PRIMARY KEY,
DNAME VARCHAR2(16),
LOC VARCHAR2(16)
);
CREATE TABLE dept_shadow
(
DEPTNO NUMBER(3) PRIMARY KEY,
DNAME VARCHAR2(16),
LOC VARCHAR2(16),
USER VARCHAR2(32),
MODTIME CHAR(17)
);
and I want create a trigger to track all inserts into a table.
surprisedly, I got error about creating table:
Error starting at line : 11 in command -
CREATE TABLE dept_shadow
(
DEPTNO NUMBER(3) PRIMARY KEY,
DNAME VARCHAR2(16),
LOC VARCHAR2(16),
USER VARCHAR2(32),
MODTIME CHAR(17)
)
Error report -
ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
I have no idea about this error and does anyone can tell me how to do this job by create trigger? Since there is no actual records to insert! Any suggestions are appreciated
Okay, so the error you are getting is because oracle (like all databases) has some reserved words. Now I'm not 100% sure because I don't tend to work with Oracle DB all that often, but I would assume that you cannot use the word USER for that reason. Try using USERNAME or USERDESCRIPTION or something like that instead.
Now for the trigger:
CREATE OR REPLACE TRIGGER trg_shadow
BEFORE INSERT OR UPDATE OR DELETE
ON dept_shadow
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
MODTIME char (17);
BEGIN
IF INSERTING
THEN
-- do something
ELSIF UPDATING
THEN
-- do something
ELSIF DELETING
THEN
-- do something
END IF;
From there, you can access "new" data through :NEW and "old" data through :OLD.
EDIT:
The difference of a BEFORE and AFTER trigger is when they are executed and both have a valid use.
BEFORE triggers may be used to validate data BEFORE inserting or updating. So for instance if you don't want to update the rows that would otherwise have the value 0 in column x.
AFTER triggers may be used to validate the new data AFTER inserting. So for instance if you want to delete all rows that now have the value 0 in column x.
It doesn't really matter in your case though.
Hope that helps!
The error is because of the column name(user) you are using in the dept_shadow table.
USER is predefined that's why it won't work as column name in table. Rename it to 'audit_user' or anything you want, which is not keyword. It will work seamlessly.
And for trigger
CREATE OR REPLACE TRIGGER dept_trigger
AFTER INSERT
ON dept
FOR EACH ROW
DECLARE
v_username varchar2(10);
BEGIN
-- Find username of person performing the INSERT into the table
SELECT user INTO v_username
FROM dual;
-- Insert record into shadow table
INSERT INTO dept_shadow
( DEPTNO,
DNAME,
LOC,
AUDIT_USER,
MODTIME )
VALUES
( :new.DEPTNO,
:new.DNAME,
:new.LOC,
v_username,
:new.MODTIME
);
END;
/
Hope this will work for you.
I have created this sequence
CREATE SEQUENCE stud_seq
START WITH 1000
INCREMENT BY 1
MAXVALUE 99999
NOCACHE
NOCYCLE;
And I have this table called student
Name Null Type
-------------------- -------- -------------
CODS NOT NULL NUMBER(5)
NUME NOT NULL VARCHAR2(50)
PREN NOT NULL VARCHAR2(50)
CODS is a PRIMARY KEY.
I am trying to do this
ALTER TABLE student ADD CONSTRAINT student_cods_df DEFAULT stud_seq.nextval FOR cods;
and I get an error
SQL Error: ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
Can you please tell me what's wrong? Thank you.
I'm using SQL Developer if anyone wants to know.
Your syntax has some issues; you probably need:
ALTER TABLE student modify cods DEFAULT stud_seq.nextval
Notice that you can use this from Oracle 12c on; if you need a solution for a different version, you'll find many answers in SO; among the others, you may want to have a look at this
I am creating a table and I typed this command:
SQL> create table accident(report_number integer primary key,
2 date varchar(20),
3 location varchar(20));
I got this error:
date varchar(20),
*
ERROR at line 2:
ORA-00904: : invalid identifier
Can anyone tell me where the error is and how to rectify it?
DATE is a reserved word and can't be used as a column name.
Date is a reserved word, to use it for column names, surround it with quotas "column-name"
ex:
create table abcd(
"date" date
);
insert into abcd values (sysdate);
select "date" from abcd;
But note that when using quoats, the column names will be case sensitive
ex:
select "Date" from abcd will result in an "Date": invalid identifier
I'm, new to SQL. I have created few tables:
CREATE TABLE MAINTINANCE
(Maint_mname char(10),
Maint_date date,
Maint_duedate date NOT NULL,
Maint_mdesc char (15));
CREATE TABLE DESIGNERR
(Dez_emp_number varchar(11),
Dez_field char(12),
Dez_qualification char(10) NOT NULL,
Dez_experience smallint);
For the first table I am adding the following constraint:
ALTER TABLE MAINTINANCE ADD CONSTRAINT CHK_maintdate CHECK(Maint_date<MAint_duedate);
but I am getting the error invalid ALTER TABLE option. Could you please let me know why this is appearing? The same is working for a friend but not for me.
For the second table I have to write a SQL command for the business rule:
If the Qualification of a Designer is BS then a Minimum of 4 years
Experience is required. But, if the Qualification of the Designer is
MS then a Minimum of 2 years Experience is sufficient.
How can we define this business rule in SQL?
The code you posted works
SQL> CREATE TABLE MAINTINANCE
2 (Maint_mname char(10),
3 Maint_date date,
4 Maint_duedate date NOT NULL,
5 Maint_mdesc char (15));
Table created.
SQL> ALTER TABLE MAINTINANCE ADD CONSTRAINT CHK_maintdate CHECK(Maint_date<MAint_duedate);
Table altered.
If you're getting an error,
Cut and paste from a SQL*Plus session that shows exactly what statements you are executing
Post the full error stack
Although it does not affect your code, the word "MAINTINANCE" is misspelled. It should be "Maintenance". Future human developers will be grateful if your table names are spelled correctly.