missing or invalid option , in creating table due to if not exists - sql

i have tried to create a table using if not exists using mysql in sql plus, but i got error
SQL> CREATE TABLE IF NOT EXISTS tasks (
2 todo_id int auto_increment,
3 task_id int);
CREATE TABLE IF NOT EXISTS tasks (
*
ERROR at line 1:
ORA-00922: missing or invalid option

You message indicates that you are using Oracles SQL PLUSand not mysql.,
so use
CREATE TABLE tasks (
todo_id NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY,
task_id NUMBER(5)
);
Please check the manual, for more information

Related

Sql error while creating tables - Firebird

I have simple sql code for create table and then add constraint to it. It looks like this:
CREATE TABLE bills (
id INTEGER NOT NULL,
code VARCHAR2(25) NOT NULL,
dateOfGeneration DATE NOT NULL,
job_id INTEGER NOT NULL
);
ALTER TABLE bills ADD CONSTRAINT bills_pk PRIMARY KEY ( id,job_id );
I am using IBExpert - client for Firebird. When I execute this code I get 2 errors:
First error: - in code VARCHAR2(25) NOT NULL
Invalid token.
Dynamic SQL Error.
SQL error code = -104.
Token unknown - line 3, column 29.
(.
Second error: - in code ALTER TABLE ...
Invalid token.
Dynamic SQL Error.
SQL error code = -104.
Token unknown - line 8, column 1.
ALTER.
The first one I think is because i am using varchar2 instead of varchar. What about second error? How to fix this?
There is no VARCHAR2 type in Firebird - https://firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25-datatypes-chartypes.html
If you want to run two commands - you have to run TWO commands. You try to run two commands in one, but that is not a way to do it. You have to split them and run one after another. Or you have to wrap them into one EXECUTE BLOCK command.
Also IBExpert has a separate window of Script Executive for multiple commands running. It is not SQL Editor which is designed to execute ONE command, it is a separate window in another menu - https://www.ibexpert.net/ibe/pmwiki.php?n=Doc.ScriptExecutive
Table creation command is described here: https://firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25-ddl-tbl.html
Basically what you trying to do looks like this, if to do it in one command:
CREATE TABLE bills (
id INTEGER NOT NULL,
code VARCHAR(25) NOT NULL,
dateOfGeneration DATE NOT NULL,
job_id INTEGER NOT NULL,
PRIMARY KEY ( id,job_id )
)
or if you insist on naming then perhaps
CREATE TABLE bills (
id INTEGER NOT NULL,
code VARCHAR(25) NOT NULL,
dateOfGeneration DATE NOT NULL,
job_id INTEGER NOT NULL,
CONSTRAINT bills_pk PRIMARY KEY ( id,job_id )
)

How do I select insert into select a table which already has values in the primary key column without adding new rows?

I'm working on a database for my school project in which I have to produce a functional database by normalizing sample tables given to us.
One table I'm having trouble with is itineraries. I produce 3 tables from the normalization which are "Destinations", "Itineraries" and "Itinerary_Destinations".
The code for Destinations is:
create table Destinations
(
DestinationID varchar(5) primary key,
Name varchar(45)
);
The code for Itineraries is:
create table Itineraries
(
ItineraryID varchar(5),
Name varchar(45)
);
The code for the last table is:
create table Itinerary_Destinations
(
DI varchar(5) primary key,
ItineraryID varchar(5) foreign key references Itineraries(ItineraryID),
Itinerary_Name varchar(45),
DestinationID varchar(5) foreign key references Destinations(DestinationID),
Destination_Name varchar(45)
);
Data has already been inserted into all 3 tables with the exception of 'Destination_Name' and 'Itinerary_Name' columns. The code I'm attempting to use is returning as error. The code is shown below.
insert into Itinerary_Destinations (Itinerary_name)
select Name from Itineraries where
Itineraries.ItineraryID = ItineraryID;
The error it returns is
Msg 515, Level 16, State 2, Line 1 Cannot insert the value NULL into
column 'DI', table 'DDDAssignment.dbo.Itinerary_Destinations'; column
does not allow nulls. INSERT fails. The statement has been terminated.
Is there a method to accomplish the task of inserting the Destination_Name and Itinerary_Name without creating new records that require primary keys?
Or should I do it manually?
If you want to modify records which already exist, then you should be using an UPDATE rather than an INSERT:
UPDATE a
SET Itinerary_name = b.Name
FROM Itinerary_Destinations a
INNER JOIN Itinerary_name b
ON a.ItineraryID = b.ItineraryID;
But, if you do have some data which is not already logically associated with the Itinerary_Destinations table, then using an insert is appropriate.
use coalesce funtion in case null it will insert blank string, as your column does not allow null value thats why you got that error in your query
insert into Itinerary_Destinations (Itinerary_name)
select coalesce(Name,' ') from Itineraries where
Itineraries.ItineraryID = ItineraryID;

ERROR: relation "students" already EXISTS

When I am executing this query:
CREATE TABLE public.students (
id INTEGER PRIMARY KEY NOT NULL DEFAULT NEXTVAL('students_id_seq'::regclass),
first_name CHARACTER VARYING(20) NOT NULL,
last_name CHARACTER VARYING(20) NOT NULL,
major CHARACTER VARYING(20) NOT NULL
);
CREATE UNIQUE INDEX "Students_ID_uindex" ON students USING BTREE (id);
SELECT * FROM public.students;
I get the following error:
[2016-03-12 22:16:54] Run postgres.public.students [PostgreSQL - postgres#localhost]
[2016-03-12 22:16:54] Connecting TO PostgreSQL - postgres#localhost...
CREATE TABLE public.students (
id INTEGER PRIMARY KEY NOT NULL DEFAULT NEXTVAL('students_id_seq'::regclass),
first_name CHARACTER VARYING(20) NOT NULL,
last_name CHARACTER VARYING(20) NOT NULL,
major CHARACTER VARYING(20) NOT NULL
)
[2016-03-12 22:16:54] [42P07] ERROR: relation "students" already EXISTS
CREATE UNIQUE INDEX "Students_ID_uindex" ON students USING BTREE (id)
[2016-03-12 22:16:54] [42P07] ERROR: relation "Students_ID_uindex" already EXISTS
SELECT * FROM public.students
[2016-03-12 22:16:54] Executed IN 14ms ms
[2016-03-12 22:16:54] Summary: 3 OF 3 statements executed, 2 failed IN 68ms (338 symbols IN file)
I have made the table using DataGrip generate:
Any idea what am I doing wrong?
UPDATE: Just to clarify my question, when I first run the code with a new table name, I get now error but when I run it again I get the above error. How can this be fixed?
You cannot create more tables with the same name - so statement CREATE should fail if there is a table with the same name already.
You can run the statement DROP TABLE before - but be aware! - it drops the table with all it's data, and undo is not possible. Second alternative is using the clause IF NOT EXISTS in CREATE statement:
DROP TABLE IF EXISTS foo;
CREATE TABLE foo(a int);
or
CREATE TABLE IF NOT EXISTS foo(a int);
You don't need to use an index name, just allow to PG to made it by itselves:
CREATE INDEX name ON public.students ("id");

SQL Error about syntax error(POSTGRES)

I always get the error on postgres sql can anyone help me
ERROR: Syntaxerror in „(“ LINE 4: Liga_Nr int(1),
^
********** ERROR **********
ERROR: Syntaxerror in „(“ SQL Status:42601 Symbol:79
Here is my code
DROP TABLE IF EXISTS Liga;
Create Table Liga(
Verband varchar(90),
Liga_Nr int(1),
PRIMARY KEY(Liga_Nr)
);
DROP TABLE IF EXISTS Spiel;
CREATE Table Spiel(
);
DROP TABLE IF EXISTS Verein;
CREATE Table Verein(
);
DROP TABLE IF EXISTS Spieler;
CREATE Table Spieler(
PRIMARY KEY(Spieler_ID)
);
Integer types don't accept a parameter. The correct code is:
Create Table Liga(
Verband varchar(90),
Liga_Nr int,
PRIMARY KEY(Liga_Nr)
);
If you want to store a small number, use smallint. You can read about numeric types here.

SQLPlus AUTO_INCREMENT Error

When I try and run the following command in SQLPlus:
CREATE TABLE Hotel
(hotelNo NUMBER(4) NOT NULL AUTO_INCREMENT,
hotelName VARCHAR(20) NOT NULL,
city VARCHAR(50) NOT NULL,
CONSTRAINT hotelNo_pk PRIMARY KEY (hotelNo));
I get the following error:
(hotelNo NUMBER(4) NOT NULL AUTO_INCREMENT,
*
ERROR at line 2:
ORA-00907: missing right parenthesis
What am I doing wrong?
Many will gripe about this not being a standard feature in Oracle, but when it’s as easy as two more commands after your CREATE TABLE command I can’t see any good reason to use fancy SQL on every insert.
First let’s create a simple table to play with.
SQL> CREATE TABLE test
(id NUMBER PRIMARY KEY,
name VARCHAR2(30));
Table created.
Now we’ll assume we want ID to be an auto increment field. First we need a sequence to grab values from.
SQL> CREATE SEQUENCE test_sequence
START WITH 1
INCREMENT BY 1;
Sequence created.
Now we can use that sequence in a BEFORE INSERT trigger on the table.
CREATE OR REPLACE TRIGGER test_trigger
BEFORE INSERT
ON test
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT test_sequence.nextval INTO :NEW.ID FROM dual;
END;
/
SQL> INSERT INTO test (name) VALUES ('Jon');
1 row created.
SQL> INSERT INTO test (name) VALUES (’Bork’);
1 row created.
SQL> INSERT INTO test (name) VALUES (’Matt’);
1 row created.
SQL> SELECT * FROM test;
ID NAME
———- ——————————
1 Jon
2 Bork
3 Matt
Oracle has no auto_increment, you need to use sequences.
Or - starting with Oracle 12.1 - you can simply have:
CREATE TABLE employee
(
id NUMBER GENERATED by default on null as IDENTITY
....
)