syntax error while trying to INSERT INTO table with postgres - sql

Here's the link to my table:
http://www.sqlfiddle.com/#!17/07f75
Here is my code, but I am getting syntax errors. I've played with a few things, like adding '' around my varchar data types, even changed the text, but am getting this error currently: ERROR: syntax error at or near "moon’" Position: 76
The syntax error has changed since I changed the text, as well as the position, but the type of error has stayed the same. I thought I understood how to insert data into the table but I am at a loss!
CREATE TABLE spacecrafts (
id integer,
name varchar(15),
launched integer,
country varchar(10),
mission text,
orbitingbody varchar(10),
operating varchar(1),
milesfromearth integer
);
INSERT INTO spacecrafts
VALUES
(1, ‘rocketeer1’, 2018, ‘USA’, ‘reach moon’, ‘mars’, ‘y’, 100000),
(2, ‘rocketer2’, 2015, ‘Brazil’, ‘reach moon’, ‘Jupiter’, ’n’, 303230),
(3, ‘rocketship’, 2014, ‘Germany’, ‘reach Jupiter’, ’n’, 67939380);

if you want to insert string data use ' instead of ’
CREATE TABLE spacecrafts (
id integer,
name varchar(15),
launched integer,
country varchar(10),
mission text,
orbitingbody varchar(10),
operating varchar(1),
milesfromearth integer
);
INSERT INTO spacecrafts VALUES
(1, 'rocketeer1', 2018, 'USA', 'reach moon', 'mars', 'y', 100000),
(2, 'rocketer2', 2015, 'Brazil', 'reach moon', 'Jupiter', 'n', 303230),
(3, 'rocketship', 2014, 'Germany', 'reach Jupiter','test', 'n', 67939380);
sqlfiddle

You have a couple errors. I changed your tick marks ' (key in top left) to a quote ' (key next to enter). Those change with cut & paste sometimes so you may not have done that directly.
The other change is on your third entry you don't have an orbitingbody entry.
INSERT INTO spacecrafts
VALUES (1, 'test', 2018, 'usa', 'reach moon', 'mars', 'y', 100000),
(2, 'rocketer2', 2015, 'Brazil', 'reach moon', 'Jupiter', 'n', 303230),
(3, 'rocketship', 2014, 'Germany', 'reach Jupiter', 'Venus', 'n', 67939380);
Notice I added 'Venus' to the third entry.

Related

Select all from drivers table in database

I am writing a code to insert new drivers and vehicles into my database and along the way I came up with an error. Below is the code to create the drivers and vehicles table in my database.
create table drivers (
id serial primary key,
first_name varchar,
last_name varchar
);
create table vehicles (
id serial primary key,
make varchar,
model varchar,
driver_id integer references drivers(id)
);
Below is my INSERT statement
INSERT INTO drivers (first_name, last_name)
VALUES
('Amy', 'Hua'),
('UDOETE', 'AKAN'),
('UCHE', 'CALEB'),
('TERKIMBI', 'VANGE'),
('PETER', 'O. OKEOWO'),
('OTOGO', 'IRINEN'),
('OSAKA', 'GEORGE C.');
SELECT * from drivers;
INSERT INTO vehicles (make, model, driver(id))
VALUES
('2023 Acura', 'Integra', 1),
('2022 Acura', 'MDX', 2),
('2022 Acura', 'NSX', 3);
SELECT * from vehicles;
RETURNING *;
I was asked to Select all driver records; select all vehicle records; select only 3 vehicle records. But after writing the above code into it, it gave this error ERROR: syntax error at or near "(" Position: 44 and my code is just 22 lines.
Thanks in advance for your help.
'44' refers to the character position within the statement that caused the error:
yours:
INSERT INTO vehicles (make, model, driver(id))
must be:
INSERT INTO vehicles (make, model, driver_id)
This is my final code below, I removed the RETURNING statement and only displayed it with the SELECT statement.
INSERT INTO drivers (first_name, last_name)
VALUES
('Amy', 'Hua'),
('UDOETE', 'AKAN'),
('UCHE', 'CALEB'),
('TERKIMBI', 'VANGE'),
('PETER', 'O. OKEOWO'),
('OTOGO', 'IRINEN'),
('OSAKA', 'GEORGE C.');
SELECT * from drivers;
INSERT INTO vehicles (make, model, driver_id)
VALUES
('2023 Acura', 'Integra', 17),
('2022 Acura', 'MDX', 18),
('2022 Acura', 'NSX', 19);
SELECT * from vehicles;

I'm wondering why after running the SQL query I get this error 1241 - what's wrong here?

CREATE TABLE MOROCCO
(
NUM_PER_MANAGE INT ,
MOYEN_RENT DECIMAL (5,2),
GENDER enum ('m','f'),
race varchar (205)
);
INSERT INTO morocco (NUM_PER_MANAGE, MOYEN_RENT, GENDER, race)
VALUES (3, (300, 45), 'm', 'black');
you must specify decimal value as 300.45 not (300, 45):
INSERT INTO MOROCCO (NUM_PER_MANAGE, MOYEN_RENT, GENDER, race)
VALUES (3, 300.45, 'm', 'black');
and if you are using mysql in linux then you should use the table name in correct case(table name is case sensitive in linux).

Why aren't my columns displaying the correct names and the right amount of characters?

I created my table:
End result:
Any help is appreciated.
I am trying to create a table showing the employee information, but when I used the "insert" command only the information for the second employee appears. Also, the column names are missing and the numbers are incorrect.
Edit
Apologies about the images. I made some updates, but I am still having an issue with the column names and my first row not appearing.
Create table biscoe6
("EmployeeID" "varchar"(11),
"SSN" "varchar"(11),
"fName" "varchar"(12),
"lName" "varchar"(12),
"Position" "varchar"(12),
"Salary" number(10, 2),
"PhoneNum" "char"(12));
insert into biscoe6 ("EmployeeID", "SSN", "fName", "lName", "Position", "Salary", "PhoneNum")
values (null, null, null, null, null, null, null);
values (100, '111-11-0607', 'John', 'Smith', 'Manager', 35000.75, '800-350-0000');
values (200, '333-22-0607', 'John', 'Jones', 'Associate', 25000.00, '202-666-0000');
values (300, '444-444-0607', 'Sally', 'Smith', 'Manager', 46500.00, '303-999-0000');
values (400, '111-11-2102', 'Randy', 'Johnson', 'Intern', 36900.00, '205-654-0000');
I think the first insert-values is working, but the second values does not have a corresponding insert and it is being treated as a simple select-values. The results you see is from the sole values.
You are making a basic mistake,values must be one time and comma separated values. try this
insert into biscoe6 ("EmployeeID", "SSN", "fName", "lName", "Position", "Salary", "PhoneNum")
values
(100, '111-11-0607', 'John', 'Smith', 'Manager', 35000.75, '800-350-0000'),
(200, '333-22-0607', 'John', 'Jones', 'Associate', 25000.00, '202-666-0000'),
(300, '444-444-0607', 'Sally', 'Smith', 'Manager', 46500.00, '303-999-0000'),
(400, '111-11-2102', 'Randy', 'Johnson', 'Intern', 36900.00, '205-654-0000');

Error converting date or time from character string

I want to inserd data to my DB and I have 2 errors:
1)
insert EmployeesInfo
(ID, MartialStatus, BirthDate, [Address], Phone)
values
(1, 'Не женат', '08/15/1970', 'Викторкая 16/7', '(067)4564489'),
(2, 'Женат', '09/09/1985', 'Малинская 15', '(050)0564585'),
(3, 'Не женат', '12/11/1990', 'Победы 16, 145', '(068)4560409'),
(4, 'Не женат', '01/11/1988', 'Антонова 11', '(066)4664466'),
(5, 'Замужем', '08/08/1990', 'Руденко 10, 7', '(093)4334493'),
(6, 'Замужем', '01/10/1994', 'Просвещения 7', '(063)4114141')
go
Error converting date or time from character string.
2)
insert Stocks
(ProductID, Qty)
values
(1, 20),
(2, 10),
(3, 7),
(4, 8),
(5, 9),
(6, 5),
(7, 12),
(8, 54),
(9, 8),
(10, 7)
go
INSERT statement conflict with FOREIGN KEY constraint "FK__Stocks__ProductI__6FE99F9F". The conflict occurred in the database 'InternatShopDB', table 'dbo.Products', column 'ID'.
Error converting date or time from character string.
Use standard datetime formats. Either '19700815' or '1970-08-15'.
INSERT statement conflict with FOREIGN KEY constraint "FK__Stocks__ProductI__6FE99F9F". The conflict occurred in the database 'InternatShopDB', table 'dbo.Products', column 'ID'.
Make sure that every product that is inserted into the stocks table exists in the products table. Unrecognized products will cause an error.

SQL Oracle error

insert into tour_concerts values
('1', to_date('02/08/1974', 'DD/MM/YYYY'), 'Misc Concerts', 'UK'),
insert into tour_concerts values
('2', to_date('01/01/1977', 'DD/MM/YYYY'), 'The Hoople North America Your', 'USA'),
insert into tour_concerts values
('3', to_date('05/09/1971', 'DD/MM/YYYY'), 'Sheer Heart Attack UK tour', 'UK'),
insert into tour_concerts values
('4', to_date('09/02/1972', 'DD/MM/YYYY'), 'Works Japan tour', 'Japan'),
insert into tour_concerts values
('5', to_date('03/10/1975', 'DD/MM/YYYY'), 'Magic Tour', 'UK'),
insert into tour_concerts values
('6', to_date('02/01/1974', 'DD/MM/YYYY'), 'Freddie Mercury Tribute Concert for AIDS Awareness', 'UK');
SQL> #tour_concerts1;
('6', to_date('02/01/1974', 'DD/MM/YYYY'), 'Freddie Mercury Tribu
te Concert for AIDS Awareness', 'UK')
*
ERROR at line 2:
ORA-12899: value too large for column "S3327043"."TOUR_CONCERTS".
"TYPE" (actual: 50, maximum: 30)
Can someone help me fix this error?
OKAY I'VE FIXED IT
This error is clearly stating that you're trying to insert a too long varchar value in the 3rd column of the tour_concerts table.
You can fix this by:
Altering the table's structure to make the column accept more than 30 characters. For instance, 50 is the number of characters of the statement that is failing.
alter table tour_concerts modify column_name varchar2(50)
Use the Oracle substr function:
`insert into tour_concerts values ('6', to_date('02/01/1974', 'DD/MM/YYYY'), substr('Freddie Mercury Tribute Concert for AIDS Awareness', 0, 30), 'UK');
If these records are inserted thorugh an application via jdbc for instance, trim user input to not exceed your table's maximum sizes.
Error is clear: TOUR_CONCERTS.TYPE field allows 30 chars, no more, and you're trying to insert a longer string.
You have two ways to solve this:
Shorten string you're inserting (Freddie Mercury Tribute Concert for AIDS Awareness is longer than 30 chars)
Change your table definition and set TOUR_CONCERTS.TYPE field length to an higher value (say 200?)
Huh.I think you should alter your table.30 is too short.
If your DB is oracle:
alter table YOURTABLE alter column 'S3327043' varchar(100)
Check this article it seems your trying to insert longer value than the field, make the fields bigger than the maximal value you could need.