Syntax Error in CREATE TABLE Statement in MS Access - ms-access

I have created a table called Customers with a CustomerID, Last Name, First Name, Address, and City, but when I tried to use the INSERT INTO to add data and ran the SQL Statement, it gives me an error: "Syntax error in CREATE TABLE statement". Below is the SQL Statement I have so far:
CREATE TABLE Customers
(
CustomerID int,
LastName varchar(50),
FirstName varchar(50),
Address varchar(50),
City varchar(50)
);
INSERT INTO (CustomerID, LastName, FirstName, Address, City)
VALUES ('10001', 'Smith', 'John', '1002 Danville Road', 'Louisville');

You are missing the table name from your INSERT statement:
INSERT INTO Customers (CustomerID, LastName, FirstName, Address, City)
VALUES ('10001', 'Smith', 'John', '1002 Danville Road', 'Louisville');
As to why you got an error pointing to your CREATE TABLE statement (which looks correct), my guess is that Access tried to connect the botched INSERT statement to the create in an effort to parse everything correctly.

Related

substring_index() returns wrong result in MySQL

I've encountered a wrong return when using substring_index.
You can reproduce my data using the query below:
create schema Airport;
use Airport;
create table aircrafttype(
aircrafttypeid char(2),
aircrafttypename varchar(20),
primary key(aircrafttypeid));
create table aircraft(
aircraftid char(2),
aircraftpurdate date,
aircraftseatcap numeric(3),
aircrafttypeid char(2),
primary key(aircraftid),
foreign key(aircrafttypeid) references aircrafttype(aircrafttypeid));
create table hangar(
hangarid char(2),
hangarlocation varchar(20),
hangarstoragecap numeric(2),
primary key(hangarid));
create table serviceteam(
teamid char(2),
teamname varchar(20),
teamlevel numeric(1),
primary key(teamid));
create table service(
serviceid char(3),
servicedate date,
hangarid char(2),
aircraftid char(2),
teamid char(2),
primary key(serviceid),
foreign key(hangarid) references hangar(hangarid),
foreign key(aircraftid) references aircraft(aircraftid),
foreign key(teamid) references serviceteam(teamid));
insert into aircrafttype values ('B7','Boeing 777');
insert into aircrafttype values ('B3','Boeing 737');
insert into aircrafttype values ('B8','Boeing 787');
insert into aircrafttype values ('B6','Boeing 767');
insert into aircrafttype values ('22','Airbus 220');
insert into aircrafttype values ('31','Airbus 310');
insert into aircraft values('A1','2012-06-19',140,'B3');
insert into aircraft values('A2','2013-08-14',129,'B6');
insert into aircraft values('A3','2013-05-01',104,'B3');
insert into aircraft values('A4','2017-04-19',296,'B7');
insert into aircraft values('A5','2018-03-02',120,'B6');
insert into aircraft values('A6','2014-10-19',191,'31');
insert into aircraft values('A7','2015-10-03',198,'31');
insert into aircraft values('A8','2016-12-31',204,'22');
insert into aircraft values('A9','2017-01-01',173,'22');
insert into hangar values('H1','Sydney, NSW',7);
insert into hangar values('H2','Melbourne, VIC',22);
insert into hangar values('H3','Sydney, NSW',25);
insert into hangar values('H4','Brisbane, QLD',8);
insert into hangar values('H5','Launceston, TAS',14);
insert into serviceteam values('T1','Sydney Rabbitohs',5);
insert into serviceteam values('T2','Melbourne Rebels',3);
insert into serviceteam values('T3','Queensland Reds',5);
insert into serviceteam values('T4','Team TRU',4);
insert into service values('S1','2019-09-25','H3','A3','T1');
insert into service values('S2','2019-08-27','H2','A7','T2');
insert into service values('S3','2019-09-22','H5','A7','T4');
insert into service values('S4','2019-05-13','H5','A4','T4');
insert into service values('S5','2019-01-08','H1','A4','T1');
insert into service values('S6','2019-09-07','H4','A9','T3');
insert into service values('S7','2019-12-20','H3','A9','T1');
insert into service values('S8','2019-12-20','H4','A3','T3');
insert into service values('S9','2019-05-18','H4','A2','T3');
insert into service values('S10','2019-05-14','H3','A3','T1');
insert into service values('S11','2019-05-27','H3','A3','T1');
insert into service values('S12','2019-08-11','H3','A9','T1');
insert into service values('S13','2019-08-17','H4','A2','T3');
insert into service values('S14','2019-12-14','H4','A4','T3');
insert into service values('S15','2025-01-25','H5','A1','T4');
Now, I want to:
Print all details of the service if the aircraft has been serviced at any hangar in NSW.
So I tried using the substring_index but it gives me an empty return, which is not true. Here's the code that I used:
select s.*, hangarlocation
from service s, hangar h
where s.hangarid = h.hangarid
and SUBSTRING_INDEX(hangarlocation, ',', -1) = 'NSW';
Can someone help me point out what is wrong here?
spaceNSW (which is what is returned by the substring index) is not the same as NSW either TRIM or include a space in the test
https://www.db-fiddle.com/f/jWBxcfXZJ8HnUuGTBKYtrt/0
Assuming, location has always state name in the end, you might use "like" instead of substring
select s.*, hangarlocation
from service s,
hangar h
where s.hangarid = h.hangarid
and hangarlocation like '%, NSW';
And it is recommended to use JOIN-syntax. This helps you to avoid mistakes like unexpected cross-joins and is more readable
select s.*, hangarlocation
from service s
join hangar h
on s.hangarid = h.hangarid
where hangarlocation like '%, NSW';

MYSQL check input values or pass in null. In a PROCEDURE

I want to do a stored procedure to add a new customer to a table named customers. I want to define the values but even if I choose to leave a field out that those field will be set to NULL and not to a blank string as it is now in the code. I have been thinking about how I can possibly write an if statement or a loop to check the input values.
I work in MySQL Workbench 8.0 if its to any help.
I did add a screenshot of the staff table under the code.
DROP PROCEDURE IF EXISTS add_customer;
DELIMITER //
CREATE PROCEDURE add_customer
(
in First_name TEXT,
in Last_name TEXT,
in Email VARCHAR(255),
in Adress TEXT,
in Postcode TEXT,
in Country TEXT,
in City TEXT,
in Phone TEXT,
-- in Points DECIMAL(9,2),
in Social_security_no TEXT,
in org_no TEXT,
in memberNO TEXT
)
BEGIN
INSERT INTO customers (id_customer, first_name, last_name, email, adress, postcode, country, city, phone, social_security_no, org_no, memberNO)
VALUES (DEFAULT , first_name, last_name, email, adress, postcode, country, city, phone, social_security_no, org_no, memberNO );
END //
DELIMITER ;
Screenshoot of the customers table
You can use nullif(varName,''). This will return null whn varName field is empty string. Use this on fields you want to:
INSERT INTO customers (id_customer, first_name, last_name, email, adress, postcode, country, city, phone, social_security_no, org_no, memberNO)
VALUES (DEFAULT , nullif(first_name,''), nullif(last_name,''), nullif(email,''), nullif(adress,''), nullif(postcode,''), nullif(country,''), nullif(city,''), nullif(phone,''), nullif(social_security_no,''), nullif(org_no,''), nullif(memberNO ,''));

I am unable to fix this issue MySQL

I have recently started MySQL and I got the MySQL Workbench and I am making a table.
But there is a section where it has the red cross and I do not know what is wrong with my code. I have tried changing the syntax but the error does not go away. Can someone please help me?
CREATE DATABASE form_acceptance;
CREATE TABLE form_acceptance (
PersonID int,
Player_Name varchar(255),
Countries varchar(255),
Username varchar(255),
Level_and_rank varchar(255),
Max_BR varchar(255)
);
INSERT INTO form_acceptance (SayByeBye_exe, SayByeBye_exe, US, '^GYMP^SayByeBye_exe', '12_Luitenant', '4.7');
SELECT PersonID, Player_Name, Countries, Username, Level_and_rank, Max_BR
This is my code so far. Except where it says INSERT INTO form_acceptance it says it is wrong and I have no idea why.
enter image description here
Thanks
It appears that you roughly got the insert logic backwards, providing the target column names in the select, and the literal values in the insert clause. Try reversing this order:
INSERT INTO form_acceptance (PersonID, Player_Name, Countries, Username, Level_and_rank, Max_BR)
SELECT 'SayByeBye_exe', 'SayByeBye_exe', 'US', '^GYMP^SayByeBye_exe', '12_Luitenant', '4.7';
You could also just use a VALUES clause here:
INSERT INTO form_acceptance (PersonID, Player_Name, Countries, Username, Level_and_rank, Max_BR)
VALUES
('SayByeBye_exe', 'SayByeBye_exe', 'US', '^GYMP^SayByeBye_exe', '12_Luitenant', '4.7');
You just need to reverse your syntax -
INSERT INTO form_acceptance (PersonID, Player_Name, Countries, Username, Level_and_rank, Max_BR)
SELECT Some_integer_value, 'SayByeBye_exe', 'US', '^GYMP^SayByeBye_exe', '12_Luitenant', '4.7';

SQL: How to insert data in specific column with specific id as well

Is it possible to perform kind of that query on single table?
I have read that it could be done by multiple tables (inserting in first table by selecting that specific value from second )
INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Suppliers
WHERE Country='Germany';
Is it possible to perform kind of that query on single table?
Yes, it is possible:
CREATE TABLE Customers(ID INT IDENTITY(1,1),
CustomerName VARCHAR(100), Country VARCHAR(100));
INSERT INTO Customers (CustomerName, Country)
VALUES ('John', 'USA'), ('Martin','Germany');
INSERT INTO Customers (CustomerName, Country)
SELECT CustomerName, Country
FROM Customers
WHERE Country='Germany';
SELECT *
FROM Customers;
LiveDemo
Keep in mind that Table Spool is required to avoid Halloween Effect

Insert into table if a field value do not already exist

I want to insert values to a row in my customer table if the Name value I'm providing do not already exist,
After some searching I used this sql query to do it and it does not work :(
IF NOT EXISTS (SELECT Name FROM customer WHERE Name = 'Riyafa')
INSERT INTO customer (`Name`, `Address`, `ContactNo`,`Total_amout`)
VALUES ('Riyafa', 'ABC', '555','1000');
Please instruct me why that is incorrect.
The if statement is only allowed in stored procedures, functions, and triggers. One way you can do this is:
INSERT INTO customer (`Name`, `Address`, `ContactNo`,`Total_amout`)
SELECT name, address, contactno, total_amount
FROM (SELECT 'Riyafa' as name, 'ABC' as address, '555' as contact no, '1000' as total_amount) t
WHERE NOT EXISTS (SELECT 1 FROM customer c WHERE c.name = t.name);
A better approach, however, is to have the database enforce uniqueness on the name. Start by creating a unique index or name:
CREATE UNIQUE INDEX idx_customer_name ON customer(name);
Then use a construct such as on duplicate key update:
INSERT INTO customer (`Name`, `Address`, `ContactNo`,`Total_amout`)
SELECT 'Riyafa' as name, 'ABC' as address, '555' as contact no, '1000' as total_amount
ON DUPLICATE KEY UPDATE Name = VALUES(Name);
The expression ON DUPLICATE KEY UPDATE Name = VALUES(Name) actually doesn't do anything, but it prevents the INSERT from returning an error.