Inserting into an SQL database? - mysql

I know this sounds like a very basic question but i've been doing SQl for a little while now and only ever come across this once and its just blowing my mind.
I have a DB called stud with 6 columns.
sid , firstname,lastname ,title,dob , gender
"124084","Bobby" ,"Carpenter","Mr" ,"040774","m"
"129002","Robin" ,"Hart" ,"Ms" ,"160275","f"
"129275","Julia" ,"Foster" ,"Ms" ,"080575","f"
I'm trying to insert it in stud but am getting the
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
INSERT INTO stud ( "123969","Heather","Howard","Ms","111274","f"
"124084","Bobby","Carpenter","Mr","040774","m"
"129002","Robin","Hart","Ms","160275","f"
is this because the data doesnt have an extra comma after the sex part or?
Sorry for such a noob question but its just been one of them days.

you need to identify the rows
INSERT INTO stud (sid, firstname, lastname, title, dob, gender)
values ("123969","Heather","Howard","Ms","111274","f")
, ("124084","Bobby","Carpenter","Mr","040774","m")
, ("129002","Robin","Hart","Ms","160275","f")

You need to identify rows and use single quotes, not double. Double quotes interpreted not as strings, but as identifiers, such as table/column name.
INSERT INTO stud (sid, firstname, lastname, title, dob, gender)
values ('123969','Heather','Howard','Ms','111274','f')
, ('124084','Bobby','Carpenter','Mr','040774','m')
, ('129002','Robin','Hart','Ms','160275','f')

Related

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';

Error while inserting in to a Mysql table which contain fields "from" and "to"

When I tried to insert into a table in Mysql with the following fields direction, from, to, message, I got the following error
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from, to, message) VALUES ( 'outgoing','FROM_NUMBER','TO_NUMBER','asas')'
Query:
INSERT INTO corporate.sms (direction, from, to, message) VALUES ( 'outgoing','FROM_NUMBER','TO_NUMBER','test message');
Is there any other way to do the insertion without changing the column names?
Since field name such as 'from' and 'to' is reserved by MySQL's SQL syntax,
you need to wrap those field names by ` ( backquote ).
So your SQL will be
INSERT INTO corporate.sms (direction, `from`, `to`, message) VALUES ( 'outgoing','FROM_NUMBER','TO_NUMBER','test message');
from and to are reserved keywords in SQL.
The solution is quite simple. Just encapsulate the from and to columns in your query between two Grave Accents(``), which is the key above Tab in your keyboard. That way, SQL Parser distinguishes the column names with reserved keywords and performs the insertion.
The query becomes:
INSERT INTO corporate.sms (direction, `from`, `to`, message) VALUES ( 'outgoing','FROM_NUMBER','TO_NUMBER','test message');

#1136 - Column count doesn't match value count at row 1 in MySQL

INSERT into Customer
(CustomerID, Forename, Surname, DOB, Address, Email)
VALUES ('1', 'Steven’, ‘Halls’, ‘08/02/1992’, ‘%d-%m-%y’, ‘12 Lesnes Abbey SE7 8TX’, ‘stevenH#gmail.com');
Could someone explain why this is not working?
Remove ‘%d-%m-%y’ from insert statement, define DOB data type as Date.
You can't change the default format for a date during the table definition stage. (It must always obey the DATETIME, DATE or TIMESTAMP formats.) As the manual puts it:
See the date and time reference docs for more info.
As such, you'll have to use the DATE_FORMAT() function at the point of output to achieve this goal.
INSERT into Customer
(CustomerID, Forename, Surname, DOB, Address, Email)
VALUES ('1', 'Steven’, ‘Halls’, ‘1992-02-08’, ‘12 Lesnes Abbey SE7 8TX’, ‘stevenH#gmail.com');

How Group By works with Duplicates

I am trying to get some insight as to how some SQL statements work. Right know I am looking into GROUP BY and want to know, how does it choose what to show/return with duplicate data.
Consider the following example:
CREATE TABLE customers
(
FirstName VARCHAR(50),
LastName VARCHAR(50),
MobileNo VARCHAR(15)
);
INSERT INTO customers VALUES ('Niraj','Yadav',989898);
INSERT INTO customers VALUES ('Chetan','Gadodia',959595);
INSERT INTO customers VALUES ('Chetan','Gadodia',959590);
INSERT INTO customers VALUES ('Atul','Kokam',42424242);
INSERT INTO customers VALUES ('Atul','Kokam',42424246);
INSERT INTO customers VALUES ('Vishal','Parte',9394452);
INSERT INTO customers VALUES ('Vishal','Parte',939445);
INSERT INTO customers VALUES ('Vishal','Parte',9394451);
INSERT INTO customers VALUES ('Jinendra','Jain',12121);
INSERT INTO customers VALUES ('Jinendra','Jain',121212);
If I run this query...
SELECT *
FROM customers
GROUP BY FirstName;
I get the following results:
FirstName LastName MobileNo
--------- -------- ----------
Atul Kokam 42424242
Chetan Gadodia 959595
Jinendra Jain 12121
Niraj Yadav 989898
Vishal Parte 9394452
So, my question is: is there any reason why it returns these particular records? How does it determine what to get? I'm using MySQL.
In other databases, your query would not be allowed exactly because the results are unpredictable in this case.
Notice what the MySQL documentation has to say for this case:
MySQL Handling of GROUP BY
The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate.
I should also mention, that Gordon Linoff recently pointed out to me that, starting in version 5.7 of MySQL, a query like yours, where unpredictable results are possible, will no longer be allowed by default.
Info on that: MySQL 5.7: only_full_group_by Improved, Recognizing Functional Dependencies, Enabled by Default!

UPDATE 2 columns in all rows using VALUES() array

I cant quite figure out the correct way to do the following:
1.) I have a fullname column
2.) I need to split that fullname column into first & last name chunks
3.) I need to insert the 'split' data into their respective columns (which are currently empty and in the same table)
I couldnt figure out how to do it in one query, so I broke it down into steps:
1.) got my split data (first/last strings)
SELECT fullname, SUBSTRING_INDEX(fullname,' ',1) AS fname, SUBSTRING_INDEX(SUBSTRING_INDEX(fullname,'.',2),' ',-1) AS lname FROM tablename;
which gave my fname & lname sub-strings..
I erroneously tried this:
INSERT INTO paypalRegistration (firstname, lastname) VALUES
("xxxx", "xxx"),
("yyy", "yyy"),
("zzz", "zzz"),...etc..
which just insert things into NEW rows.. (and didnt update the current rows [with blank columns] already in the table)
So I then tried this:
UPDATE paypalRegistration SET(firstname, lastname) VALUES ("xxxx", "xxx"),
("yyy", "yyy"), ("zzz", "zzz"),...etc..
but got an error..
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(firstname, lastname) VALUES ("xxxx", "xxx").....
So at this point, my question is two-fold.
1.) How can I do this in 1 query?
2.) At the point I'm at, what is the correct way to update 2+ columns with unique values like I have above?
Update:
This is what worked for me in the end:
UPDATE myTable SET firstname=SUBSTRING_INDEX(fullname,' ',1), lastname=SUBSTRING_INDEX(SUBSTRING_INDEX(fullname,'.',2),' ',-1);
Say your table name is paypalRegistration
This table paypalRegistration has the following columns:
id INT PRIMARY KEY
fullName VARCHAR
firstName VARCHAR
lastName VARCHAR
I expect to have id column already in your table paypalRegistration as explained above.
Now, you can do this SQL:
UPDATE paypalRegistration x
INNER JOIN (
SELECT
id,
SUBSTRING_INDEX(fullName,' ',1) AS fname,
SUBSTRING_INDEX(SUBSTRING_INDEX(fullName,'.',2),' ',-1) AS lname
FROM paypalRegistration
) y ON y.id=x.id
SET x.firstName = y.fname, x.lastName = y.lname;