I am trying to create schemas in SQL but I am running into issues with my variable types. When I have two varchar types I run into errors but if I only have one type as a varchar my code can execute. What am I doing wrong?
My code
CREATE TABLE IF NOT EXISTS Customer(
user_id integer
ccnum integer
expdate timestamptz
name varchar(30)
email varchar(100)
);
Error
Error: near line 8: in prepare, near "email": syntax error (1)
[Execution complete with exit code 1]
You are missing commas.
CREATE TABLE IF NOT EXISTS Customer(
user_id integer,
ccnum integer,
expdate timestamptz,
name varchar(30),
email varchar(100)
);
I have a PostgreSQL database hosted on Heroku which is throwing me this error that I can't wrap my head around.
CREATE TABLE people (id INTEGER PRIMARY KEY AUTOINCREMENT, age SMALLINT, right_handed BOOLEAN)
SELECT 1 id FROM people WHERE age IN (1) AND right_handed IN (1)
It gives me the error:
Error in query: ERROR: syntax error at or near "IN"
Don't know how to proceed, any help would be greatly appreciated.
AUTOINCREMENT is not a valid option for CREATE TABLE in Postgres
You can use SERIAL or BIGSERIAL:
ALTER TABLE myTable ADD COLUMN myColumn BIGSERIAL PRIMARY KEY;
I am currently working on a SQL assignment, but first I need to create tables which it is not letting me do. I have 3 tables which are Customer, PurchasedDeal, and Usage. I manage to create the first 2 tables successfully, but I am having a little difficulty with creating the Usage table. For some reason it is giving me this error.
Error at line 2:
ORA-00904: : Invalid Identifier
If anyone could help me understand why it is giving me this error I would really appreciate it. Thanks. It is saying it is having problems with uID INT and I am using putty to create these tables.
CREATE TABLE Customer(
CustomerID INT NOT NULL PRIMARY KEY,
CustomerName VARCHAR(100),
Phone VARCHAR(15)
);
CREATE TABLE PurchasedDeal(
DID INT NOT NULL PRIMARY KEY,
dealName VARCHAR(100),
cost FLOAT,
totalValue FLOAT,
balance FLOAT,
CustomerID INT,
FOREIGN KEY(CustomerID) REFERENCES Customer(CustomerID)
);
CREATE TABLE Usage(
uID INT,
uDate DATE,
cost FLOAT,
DealID INT,
PRIMARY KEY(DealID, uID),
FOREIGN KEY(DealID) REFERENCES PurchasedDeal(DID)
);
The term ‘uid’ is reserved in Oracle. This term cannot be used as a column name in the Oracle environment.
Query: SELECT uid FROM t1
Result: will execute correctly
Query: SELECT U.”uid” FROM x.t1 U
Result: will through error:
ORA-00904: “U”.”uid”: invalid identifier 00904. 00000 – “%s: invalid identifier”
Query: SELECT U.”UID” FROM x.t1 U
Result: will execute correctly as uid is replaced with UID (in capital)
Rectification in below table and we are good to go.
CREATE TABLE Usage(
uID1 INT,
uDate DATE,
cost FLOAT,
DealID INT,
PRIMARY KEY(DealID, uID1),
FOREIGN KEY(DealID) REFERENCES PurchasedDeal_test(DID)
);
I am a beginner with SQL. I have created 4 tables and added data to my SHIP table. I am having some issues with inserting data into the CRUISE table. I get the error message at the bottom.
I have researched and can not figure out what i am doing wrong. Is there an issue with my sequence and/or trigger that is not allowing me to do this or is my syntax in the CREATE TABLE CRUISE causing the error? Everything i have done has been successful up until trying to insert the first column into the CRUISE table.
The tables:
CREATE TABLE SHIP
( Ship_Name VARCHAR2(100) PRIMARY KEY,
Ship_Size INTEGER,
Ship_Registry VARCHAR2(50),
Ship_ServEntryDate INTEGER,
Ship_PassCapacity INTEGER,
Ship_CrewCapacity INTEGER,
Ship_Lifestyle VARCHAR2(40),
CONSTRAINT Size_ck CHECK (Ship_Size > 0),
CONSTRAINT Registry_ck CHECK (Ship_Registry IN ('Norway','Liberia','The Netherlands','Bahamas'))
)
CREATE TABLE CRUISE (
Cruise_ID INTEGER Primary Key,
Ship_Name VARCHAR(100),
Cruise_DeptDate DATE NOT NULL,
Cruise_DeptCity VARCHAR(80) NOT NULL,
Cruise_Duration INTEGER,
FOREIGN KEY (Ship_Name) REFERENCES SHIP(Ship_Name)
)
CREATE TABLE PASSENGERS (
Pass_ID INTEGER PRIMARY KEY,
Pass_Name VARCHAR(100) NOT NULL,
Pass_City VARCHAR(80),
Pass_Telephone VARCHAR(15),
Pass_NextOfKin VARCHAR(100)
)
CREATE TABLE RESERVATIONS (
Pass_ID INTEGER NOT NULL,
Cruise_ID INTEGER NOT NULL,
Res_TotalCost NUMERIC(9,2),
Res_BalanceDue NUMERIC(9,2),
Res_SpecialRequest VARCHAR(30),
Res_Room VARCHAR(10),
FOREIGN KEY (Pass_ID) REFERENCES PASSENGERS(Pass_ID),
FOREIGN KEY (Cruise_ID) REFERENCES CRUISE(Cruise_ID),
CONSTRAINT Cost_ck CHECK (Res_TotalCost >= 0),
CONSTRAINT BalanceDue_ck CHECK (Res_BalanceDue >= 0),
CONSTRAINT SpecialRequest_ck CHECK (Res_SpecialRequest IN ('Vegetarian','Vegan','Low salt','Gluten free','Kosher','Other'))
)
The sequence/trigger is an attempt to auto number Cruise_ID.
Create SEQUENCE cruise_id_sq
START WITH 1
INCREMENT BY 1;
CREATE OR REPLACE TRIGGER cruise_id_t
BEFORE INSERT
ON CRUISE
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
if(:new.Cruise_ID is null) then
SELECT cruise_id_sq.nextval
INTO :new.Cruise_ID
FROM dual;
end if;
END;
ALTER TRIGGER cruise_id_t ENABLE;
Inserting into SHIP is okay....
INSERT INTO SHIP
(Ship_Name, Ship_Size, Ship_Registry,Ship_ServEntryDate, Ship_PassCapacity,Ship_CrewCapacity,Ship_Lifestyle)
Values ('Carribean Princess',142000,'Liberia',1000,3100,1181,'Contemporary');
INSERT INTO SHIP
(Ship_Name, Ship_Size, Ship_Registry,Ship_ServEntryDate, Ship_PassCapacity,Ship_CrewCapacity,Ship_Lifestyle)
Values ('Carribean Sunshine',74000,'Norway',1992,1950,760,'Premium');
INSERT INTO SHIP
(Ship_Name, Ship_Size, Ship_Registry,Ship_ServEntryDate, Ship_PassCapacity,Ship_CrewCapacity,Ship_Lifestyle)
Values ('Ship of Dreams',70000,'Liberia',2004,1804,735,'Contemporary');
INSERT INTO SHIP
(Ship_Name, Ship_Size, Ship_Registry,Ship_ServEntryDate, Ship_PassCapacity,Ship_CrewCapacity,Ship_Lifestyle)
Values ('Sunshine of the Seas',74000,'The Netherlands',1990,2354,822,'Luxury');
Inserting into CRUISE fails...
INSERT INTO Cruise
(Ship_Name, Cruise_DeptDate,Cruise_DeptCity,CruiseDuration)
Values ('Sunshine of the Seas',25-may-15,'Miami',10);
Error starting at line : 1 in command - INSERT INTO Cruise (Ship_Name,
Cruise_DeptDate,Cruise_DeptCity,CruiseDuration) Values ('Sunshine of
the Seas',25-may-15,'Miami',10) Error at Command Line : 3 Column : 35
Error report - SQL Error: ORA-00984: column not allowed here
00984. 00000 - "column not allowed here"
*Cause:
*Action:
Oracle thinks that 25-may-15 is the expression 25 minus may minus 15. In looking up the value for may Oracle finds that there is nothing there. Thus the error.
You can, but probably don't want to, quote it like so, '25-may-15'. This will make a string that may or may not be implicitly converted to a date, depending on the settings of NLS_DATE_FORMAT and or NLS_TERRITORY.
To form a date independent of session setting one can use the TO_DATE function with explicit date format, to_date('25-may-15', 'DD-Mon-YY'). Another option is a date literal, date '2015-05-25', which is always YYYY-MM-DD no matter the session settings..
I have one table called test, which has 4 columns:
id INT
v_out INT
v_in INT
label CHARACTER
I'm trying to update the table with the following query:
String sql = "
update
test
set
v_out = temp.outV
, v_in = temp.inV
, label = temp.label
from (
values
(1,234,235,'[abc]') // these value are read from other places
,(2,234,5585,'[def]') //[abc] = object.toString();
) as temp (e_id, outV, inV, label)
where
id = temp.e_id;
When I execute it, I got the error:
org.postgresql.util.PSQLException: ERROR: syntax error at or near "["
before get this error, i already update over 3000 rows.
so whats the reason caused this? is it because "[" this character?
this is the original table:
create table edges(
id serial not null primary key,
vertex_out int,
vertex_in int,
label character varying(255),
constraint fk_vertex_out foreign key (vertex_out) references vertices(id) on delete cascade,
constraint fk_vertex_in foreign key (vertex_in) references vertices(id) on delete cascade
);
The most likely problem here is string interpolation in SQL query (it is a very bad practice).
Look at this example:
values
(1,234,235,'[abc]''),
(2,234,5585,'[def]')
The ' symbol in the name of the first object breaches the string boundaries causing ERROR: syntax error at or near "[": on the second line.
You can search for SQL Injection in the internet to get details about this problem.