Reserved word in column name - insert into MySQL [duplicate] - mysql

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
I have a MySQL database with the word "group" in one of the column names.
I can't change this database and column's name; it's not mine.
Table users, columns: id, name, password, group, and other.
I need to insert a record into this table. I tried INSERT INTO users (name, group) VALUES ('John', '9'), but it's not working because of "group".
Can you help me, how to insert a record into this table, please?

Try:
INSERT INTO users (`name`, `group`) VALUES ('John', '9')

use backticks(`) around column names when you use reserved keywords in query:
INSERT INTO users (`name`,`group`) VALUES ('John', '9')
Read here: Reserved Words

Related

What will be the database query? [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 2 years ago.
I have a table as shown and I am trying to insert the data to it but I am constantly getting error . Can anyone suggest what should be the query ??
Here's my table structure:
My insert query:
INSERT INTO country (id, country_name, iso_code, country_pic_url, rank, created_time, updated_time, deleted_time) VALUES(1, 'Afghanistan', 'AF', null, 0, '2013-09-04 13:30:00', '2013-09-04 13:30:00', '2013-09-04 13:30:00')
Don't use null instead Put NULL in your syntax or either leave it blank.
Check if the rank you are trying to put is a string, so instead of just using 0, put '0' in your query.

MySQL REPLACE without all fields provided

I have mysql query to replace some records:
REPLACE INTO product_core
(id, title, description, category_id)
VALUES (2, 'new_title', 'new_description', 33)
Can I do the same, but not providing all needed values? Example:
REPLACE INTO product_core
(id, title, description, category_id)
VALUES (2, 'new_title', 'new_description') #no category_id
Got error wrong number of values here near
I want to bulk replace many records, but I do not want to query all fields before. In this example, I want to update category_id for some records, but not for all.
REPLACE INTO product_core
(id, title, description, category_id)
VALUES (2, 'new_title_2', 'new_description_2'), #no category_id
(3, 'new_title_3', 'new_description_3', 34) #with category_id
Is it real to do this? Replace some fields for one record and other fields for second record in one query.
Or if is it real to provide special variable meaning that some fields will be the same as before replace (category_id)?
VALUES (2, 'new_title_2', 'new_description_2', #category_id_same_as_before)
Can I do the same, but not providing all needed values? Example:
REPLACE INTO product_core (id, title, description, category_id) VALUES
(2, 'new_title', 'new_description') #no category_id
Yes, the correct query is:
REPLACE INTO product_core
(id, title, description)
VALUES (2, 'new_title', 'new_description') #no category_id
EDIT: As Tom commented below the above might be misleading as for the omitted columns default values will be used, not the ones set for the record which is being replaced.
Is it real to do this? Replace some fields for one record and other fields for second record in one query.
It's not possible in MySQL. The column list is common for all the values sets.
Or if is it real to provide special variable meaning that some fields
will be the same as before replace (category_id)?
It's perhaps possible, but not straightforward and not in all MySQL versions. In the docs they say: "You cannot refer to values from the current row and use them in the new row".
In MySQL > 8.0.19 perhaps VALUES(ROW) can help. Or you can perhaps write you own UDF which does it.
You can't omit these columns from a REPLACE command, unless it is the default value for the column.
According to the documentation:
Any missing columns are set to their default values. [...] You cannot refer to values from the current row and use them in the new row.
It may be better to use a standard UPDATE command instead, which can reference the current column value.

Mysql use last inserted id in a different table query multiple times [duplicate]

This question already has answers here:
LAST_INSERT_ID() MySQL
(14 answers)
Closed 5 years ago.
I am working on a query that I'd like to run to import some phrases based on actions.
These are the two tables I want to insert data into.
actions:
- id
- name
- data
phrases:
- id
- action_id
- phrase
Where phrases.action_id = actions.id
I want to create a new action. Return that ID and then add multiple phrases using the returned ID.
Is there anyway of making the action.id that was inserted persistent or a variable for re-use.
My train of thinking has led me to things like:
SELECT LAST_INSERT_ID();
And
OUTPUT insterted.id
Not expecting an answer but some helpful information that will point me in the right direction would be great
You can assign it to a user variable:
INSERT INTO actions ...;
SET #action_id = LAST_INSERT_ID();
Then you can use #action_id in all the INSERT queries that insert into phrases.
INSERT INTO phrases (action_id, phrase) VALUES (#action_id, "Whatever");
INSERT INTO phrases (action_id, phrase) VALUES (#action_id, "Some other phrase");
You could also solve it by doing all the inserts in a single query:
INSERT INTO phrases (action_id, phrase) VALUES
(LAST_INSERT_ID(), "Whatever"),
(LAST_INSERT_ID(), "Some other phrase");

mysql insert into query not working

The query is pasted below. Basically I have a users table, and I want to create 1 record for every entry in the users table in the controls table. I need the user_id from the users table when I'm inserting into the controls table. I get a syntax error when I try and run the query below. I've looked at a bunch of other MySQL examples that do something similar but get the same error. I'm running MySQL 5.6.21.
This will end up being a Rails migration, so I am also amenable to using ActiveRecord, but haven't really figure out how to do something like this with it.
INSERT into controls (default, whitelist, user_id, admin_status, system_status, created_at, updated_at) values (0, 0, (SELECT id FROM users), 'inactive', 'sleeping', DATE(NOW), DATE(NOW));
I believe your problem is that you're trying to mix a SELECT with VALUES (which is used for inputting literal values).
Try:
INSERT into controls
(`default`, whitelist, user_id, admin_status, system_status, created_at, updated_at)
SELECT 0, 0, id, 'inactive', 'sleeping', NOW(), NOW() FROM users;
DEFAULT is a MySQL reserved word. You may need to enclose that column name in backticks.
INSERT into controls (`default`, whitelist,
^ ^
Those are backtick characters (key to the left of the 1/! key), not single quotes.
The error message from MySQL should indicate where in the SQL text MySQL thinks the problem is.
If this is a new table, strongly consider using a column name that is not a MySQL reserved word.
Reference: https://dev.mysql.com/doc/refman/5.5/en/keywords.html
The SELECT in the context of the VALUES clause needs to return at most one row. You'd need to ensure that SELECT doesn't return more than one row, e.g. add a LIMIT 1 clause. But
Reading your question again... if you want to insert a row in the new controls table for every row that's in users table, don't use the VALUES keyword. Use the INSERT ... SELECT form of the INSERT statement.
Reference: http://dev.mysql.com/doc/refman/5.5/en/insert-select.html

Adding only one value to the table in sql

I have a table named student which consists of (rollno, name, sem, branch)
If I want to INSERT only one value (i.e only name has to be entered) what is the query?
To insert values into specific columns, you first have to specify which columns you want to populate. The query would look like this:
INSERT INTO your_table_name (your_column_name)
VALUES (the_value);
To insert values into more than one column, separate the column names with a comma and insert the values in the same order you added the column names:
INSERT INTO your_table_name (your_column_name_01, your_column_name_02)
VALUES (the_value_01, the_value_02);
If you are unsure, have a look at W3Schools.com. They usually have explanations with examples.
insert into student(name) values("The name you wan to insert");
Be careful not to forget to insert the primary key.
First, if the table is all empty, just wanna make the column 'name' with values, it is easy to use INSERT INTO. https://www.w3schools.com/sql/sql_insert.asp.
`INSERT INTO TABLE_NAME (COLUMN_NAME) VALUES ("the values")`
Second, if the table is already with some values inside, and only wanna insert some values for one column, use UPDATE. https://www.w3schools.com/sql/sql_update.asp
UPDATE table_name SET column1 = value1 WHERE condition;
insert into student (name)
select 'some name'
or
insert into student (name)
values ('some name')
Following works if other columns accept null or do have default value:
INSERT INTO Student (name) VALUES('Jack');
Further details can be found from the Reference Manual:: 13.2.5 INSERT Syntax.
Execute this query, if you want the rest of columns as "#", do insert # inside the single quote which is left blank in query.
Also, there is no need of defining column name in the query.
insert into student values('','your name','','');
Thanks...