Home > Blockchain >  Select all from drivers table in database
Select all from drivers table in database

Time:08-03

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.

CodePudding user response:

'44' refers to the character position within the statement that cuased the error:

yours:
INSERT INTO vehicles (make, model, driver(id))

must be:
INSERT INTO vehicles (make, model, driver_id)

CodePudding user response:

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;
  •  Tags:  
  • sql
  • Related