Create table script syntax error "table does not exist" - sql

This is my first time producing a script which I can run through the psql terminal using the -f option.
My script is as follows:
DROP TABLE if EXISTS piste;
DROP TABLE if EXISTS lift;
DROP TABLE if EXISTS lift_location;
CREATE TABLE piste (
piste_name varchar(30) PRIMARY KEY NOT NULL,
grade varchar(10) NOT NULL,
length decimal NOT NULL,
fall smallint NOT NULL,
open boolean NOT NULL,
);
INSERT INTO piste (name, grade, length, fall, open) VALUES
('test name', 'easy', 3.2, 400, true);
This produces the following error:
psql:create_tables.sql:22: NOTICE: table "piste" does not exist, skipping
DROP TABLE
psql:create_tables.sql:23: NOTICE: table "lift" does not exist, skipping
DROP TABLE
psql:create_tables.sql:24: NOTICE: table "lift_location" does not exist, skipping
DROP TABLE
psql:create_tables.sql:33: ERROR: syntax error at or near ")"
LINE 8: );
^
psql:create_tables.sql:36: ERROR: relation "piste" does not exist
LINE 1: INSERT INTO piste (name, grade, length, fall, open) VALUES
Does anybody know what is causing this? From what I can see, the table "piste" is created before I try to insert so how can it say it does not exist?
Thanks,
Chris.

This error:
psql:create_tables.sql:33: ERROR: syntax error at or near ")"
LINE 8: );
Tells you that the table is not created.
You have a dangling comma , inside your create table (after the last column). Remove that and you should be fine.

The comma at the end generates the error message:
open boolean NOT NULL,
The other messages are NOTICEs not errors. They just tell you that they didn't drop the table as they did not exist

Related

Can't drop a column with a constraint in command line

I have an exercises table with this schema:
CREATE TABLE exercises
(
id INTEGER NOT NULL,
name VARCHAR(255) NOT NULL,
workoutID INTEGER NOT NULL,
FOREIGN KEY (workoutID) REFERENCES workouts(id)
);
I want to delete the column workoutID but it won't let me delete it:
sqlite> ALTER TABLE exercises DROP workoutID;
Error: near "DROP": syntax error
Upon realizing I need to remove the constraints first I try:
sqlite> ALTER TABLE exercises DROP CONSTRAINT workoutID;
Error: near "DROP": syntax error
I followed this question to the letter: How to drop column with constraint?
But it doesn't work for me.
I can start over by dropping the whole table if I have to but I want to learn why this isn't working for me.
Any help and explanation is greatly appreciated.

insert values of table in another empty table

Can someone help me to understand where is the probleme ?
I have two tables the first one is :
CREATE TABLE "election-csv" (
"Code du département" DECIMAL NOT NULL,
.
.
...
);
the second one is empty:
CREATE TABLE departement (
code_dpt integer,
DPT varchar(30) NOT NULL,
PRIMARY KEY( code_dpt )
);
-- error when I want to insert the values from the first table --
insert into departement(code_dpt) values (select"Code du département" FROM "election-csv");
I get this error :
SQL Error [42601]: ERROR: syntax error at or near "select"¶ Position : 9
Assuming you're using MSSQL the INSERT statement should look like this:
INSERT INTO departement(code_dpt)
SELECT [Code du département]
FROM election-csv

Check length constraint is not working in Oracle11g sqlplus

I've been trying to put length constraint , so that it would not take string whose length is more or less than 5
Create Table Statement:
create table exp(id char(10),name varchar(50));
Add Constraint Statement:
alter table exp add constraint exp1 check(length(id)=5);
Insert Statement:
insert into exp(id,name) values('10001','Abhi');
But whenever i try to insert data like the above written it shows
insert into exp(id,name) values('10001','Abhi')
*
ERROR at line 1:
ORA-02290: check constraint (VIT.EXP1) violated
Change char(10) to varchar2(10):
create table exp(id varchar2(10),name varchar(50));
A char(10) column has always a length of 10. Regardsless your insert statement. That's why you get the error.

ORA-04098: trigger 'SYSTEM.TEXT_TRIG' is invalid and failed re-validation

I am using the oracle database 10g express edition to execute triggers. My table structure is as shown below:
Table text:
create table text
(
tid number Not Null,
tname varchar2(5)
);
Table book:
create table book
(
bid number Not Null,
bname varchar2(5),
foreign key(bid) references text(tid)
);
Later inserted one row in text tabe: 1,abc
My question is when i am about to insert a value to text table, it should automatically insert a value in book table.
I had created a trigger named text_trig: structure is a s follows:
create trigger text_trig after insert on text
for each row
begin
insert into book set bid=NEW.tid;
end;
/
It gave me an warning message saying: Trigger created with compilation errors.
when I inserted a new value in text table, it is showing an error message as
trigger 'SYSTEM.TEXT_TRIG' is invalid and failed re-validation.
You missed a colon.
set bid = :NEW.tid

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.