Select all from drivers table in database - sql

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;

Related

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).

SQLDelight FTS5 insert trouble

I created a table in DBBrowser:
CREATE VIRTUAL TABLE IF NOT EXISTS Students USING FTS5
(
GroupId UNINDEXED,
StudentName
);
and insert values to it. After that I add DB with this table to my project.
It is declaration of this table in sqldelight .sq file:
CREATE VIRTUAL TABLE IF NOT EXISTS Students USING FTS5
(
GroupId INTEGER AS Int,
StudentName TEXT,
rank REAL
);
I need to explicit declare rank because I want to apply HAVING MIN(rank) for it when SELECT from table (otherwise it is not compile), but when I trying to insert values in table like that:
insert:
INSERT INTO Students VALUES (?,?);
I receive an error:
Unexpected number of values being inserted. found: 2 expected: 3
If I do like that:
insert:
INSERT INTO Students VALUES (?,?,?);
I receive an exception:
SQLiteException - table Students has 2 columns but 3 values were supplied (code 1): , while compiling: INSERT INTO Students VALUES (?,?,?)
How I can perform insert? Or maybe I can apply HAVING MIN(rank) without explicit declare?
does
insert:
INSERT INTO Students(GroupId, StudentName) VALUES (?,?);
work?

Storing an object into a SQL database in Oracle

Simply trying to find the correct syntax/method to enter create SQL objects and store them inside an oracle database. (school project so it's got to be possible)
CREATE OR REPLACE TYPE Person AS OBJECT
(id NUMBER,
fname VARCHAR(255),
lname VARCHAR(255)) NOT FINAL
CREATE OR REPLACE TYPE customer UNDER Person (
num_purchases NUMBER,
email VARCHAR(255)
);
CREATE TABLE Customers (
id NUMBER,
cust customer
);
INSERT INTO Customers(id, cust)
VALUES (1, customer(1, "John", "Doe", 44, "doezer#gmail.com"));
Returns the error
ORA-00984: column not allowed here
I can't seem to find any way to declare a specific order for putting the values inside of the customer object other than the order that they were declared in. Thank you!
Seems you are inserting columns not literal values. replace your double quotes to single quotes.
INSERT INTO Customers(id, cust)
VALUES (1, customer(1, 'John', 'Doe', 44, 'doezer#gmail.com'));
SELECT ID, TREAT(cust AS Person).id, TREAT(cust AS Person).fname FROM Customers;
See dbfiddle

Can't run SQL query due to type conversion failure, key violation, and more

I get an error when trying to run this SQL query in MS Access:
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate)
VALUES (1, 21/09/2015);
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate, recommendedByLearnerPersonIDFK)
VALUES (2, 05/03/2016, 1);
This is the error:
enter image description here
The Learner table looks like this:
CREATE TABLE Learner
(
learnerPersonIDPKFK INT NOT NULL PRIMARY KEY,
registrationDate DATETIME,
recommendedByLearnerPersonIDFK INT NOT NULL,
CONSTRAINT fk_recommendedByLearnerPersonIDFK
FOREIGN KEY(recommendedByLearnerPersonIDFK)
REFERENCES Learner (learnerPersonIDPKFK)
);
Try wrapping your dates in single quotes:
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate)
VALUES (1, '21/09/2015');
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate, recommendedByLearnerPersonIDFK)
VALUES (2, '05/03/2016', 1);
Depending on your localization settings for the DATETIME format, you may need to use the MM/DD/YYYY format. Remember, your passing in a varchar and letting SQL try to "guess" how to implicitly convert it:
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate)
VALUES (1, '09/21/2015');
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate, recommendedByLearnerPersonIDFK)
VALUES (2, '03/05/2016', 1);
Use a simple tester to verify if SQL can "guess" your date formating:
SELECT CAST('09/21/2015' AS DATETIME)

cannot insert the NULL value into the column

I have been trying to insert data into the database, but it's continuously showing me this error, even though I not writing the name of the column(MemID) in the first fields, rather I am trying to enter it separately like this
create table Member (
MemID VARCHAR(20) PRIMARY KEY,
FullName VARCHAR(50),
email VARCHAR(50),
gender VARCHAR(50),
Contact_Number VARCHAR(50),
Registration_Number VARCHAR(50),
User_S# INT
);
insert into Member (FullName, email, gender, Contact_Number,
Registration_Number, User_S#) values ('Jemmy Joutapaitis',
'jjoutapaitiscf#dailymail.co.uk', 'Male', '86-(804)800-8008',
'3574884734839928', 449);
insert into Member (FullName, email, gender, Contact_Number,
Registration_Number, User_S#) values ('Cleo Glynn', 'cglynncg#i2i.jp',
'Male', '81-(694)548-5205', '5443114970343516', 450);
insert into Member (FullName, email, gender, Contact_Number,
Registration_Number, User_S#) values ('Ivonne Deetlefs',
'ideetlefsch#virginia.edu', 'Female', '86-(257)683-5628',
'3571675846170605', 451);
insert into Member(MemID)
values('Mem0');
insert into Member(MemID)
values('Mem1');
insert into Member(MemID)
values('Mem2');
This code is giving me the error
Cannot insert the value NULL into column 'MemID', table
'master.dbo.Member'; column does not allow nulls. INSERT fails.
But I am inserting MemID column separately.
And also the table which has created above is not showing in the database, is it because of the error or something else?
That's not how inserts work.
When an insert is triggered sql tries to add the row immediately, without waiting for the consequent statements (it's stands true for any dml statements not only insert).
So when your first insert is triggered then sql is trying to put null in MemID as you haven't supplied any MemID in your insert.
Try inserting it in the very first place itself in the insert. Instead of separate statement something like:
insert into Member (MemID, FullName, email, gender, Contact_Number,
Registration_Number, User_S#) values ('Mem0','Jemmy Joutapaitis',
'jjoutapaitiscf#dailymail.co.uk', 'Male', '86-(804)800-8008',
'3574884734839928', 449);
EDIT
With response to your comment , I think Prepared statements is what you're looking for.
Since you're using python you could do prepare your insert statement like:
sql = "insert into Member (MemID, FullName, email, gender, Contact_Number,
Registration_Number, User_S#) values (%s,'Jemmy Joutapaitis',
'jjoutapaitiscf#dailymail.co.uk', 'Male', '86-(804)800-8008',
'3574884734839928', 449);"
sql = sql.format(self.db_scan_table)
//whenever you've memid ready use.
self.cursor.execute(sql, ('Mem01'))
Define an AUTO_INCREMENT column to generate ID values for you:
CREATE TABLE Member (
MemID INT AUTO_INCREMENT PRIMARY KEY,
-- ...
);
If this isn't practical because you need some kind of particular string definition for these then your only option is to supply it as part of the initial INSERT call. You have a constraint here, the PRIMARY KEY value must not be NULL.
should be able to using the query below. identify the next number
insert into member(memid,......)
values ( 'MEM' + cast((select count(1) from member)+1 as varchar(100)),... )
if you want to start with 1 then + 1
otherwise remove + 1
insert into member(memid,......)
values ( 'MEM' + cast((select count(1) from member) as varchar(100)),... )