0

Insert into the App.settings table the following values:

(99, DEFAULT, "horizontal", "2015-09-15 04:01:04")

I have a DATABASE called App with a settings table. I am trying to insert into the table but I can not seem to get it right.

My statement:

INSERT INTO App.settings
    VALUES(99, DEFAULT, "horizontal", "2015-09-15 04:01:04");

Am I doing it right? It says my answer is wrong.

mysql> DESC App.settings;
+-----------------+---------------------+------+-----+---------+-------+
| Field           | Type                | Null | Key | Default | Extra |
+-----------------+---------------------+------+-----+---------+-------+
| user_id         | int(7)              | NO   |     | NULL    |       |
| email_frequency | tinyint(2) unsigned | YES  |     | NULL    |       |
| layout          | varchar(70)         | YES  |     | NULL    |       |
| updated_at      | datetime            | YES  |     | NULL    |       |
+-----------------+---------------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
2
  • In Standard SQL strings are single-quoted: (99, DEFAULT, 'horizontal', '2015-09-15 04:01:04'). Object names are double-quoted: "App"."settings" Commented May 27, 2018 at 20:19
  • What columns are in App.settings? Can you use DESC App.settings and show us the result? Also, what DBMS are you using? (MySQL, Oracle, MSSQL, MongoDB, ...) Commented May 27, 2018 at 20:19

3 Answers 3

2

When you use insert, always list the columns in the table. Second, the default string delimiter is the single quote in SQL rather than the double quote.

So I would expect to see:

INSERT INTO App.settings (col1, col3, col4)  -- your real column names here
    VALUES (99, 'horizontal', '2015-09-15 04:01:04');

Note that col2 was removed from the INSERT and the VALUES because you seem to want a DEFAULT value. Not all databases support that syntax.

Sign up to request clarification or add additional context in comments.

6 Comments

Why always? If the table is not subject to change, I wouldn't say that it's absolutely necessary
@TwiN . . . Because people who are learning SQL should learn how to do it right. If you are experienced and know when not to use column names, then you'll ignore the advice. There is only one case where I do not list column names, and it is something of an outlier. I always list column names for non-temporary tables. I don't want changes in table structures to cause unexpected behavior in code.
This is the statement I use: INSERT INTO App.settings VALUES (99, DEFAULT, 'horizontal', '2015-09-15 04:01:04'); The error I get is [Error]: Task 1. Expected: Insert into the App.settings table the specified values. Try again.
@GordonLinoff Ah, when in code, I definitely agree with you. We should always include the name of the columns. From his example, though, I thought it didn't look like he was trying to use it in code.
Maybe I have to use the ALTER keyword?
|
0

you are sure that all the fields of the tuple are there and if so, they are in the correct order if you are not entering all the fields of the tuple you should use the following form: INSERT INTO App.settings(value, config, position, date ) VALUES(99, "DEFAULT", "horizontal", "2015-09-15 04:01:04");

In the same order

this is only an example i dont know the fields names you must be change for the you are using

Comments

0

Like @GordonLinoff said, you should include the columns in your query. That way, you can also skip entering the DEFAULT value for email_frequency.

As for the mistake, it's most likely the double quotes that you're currently using instead of single quotes.

Try the following:

INSERT INTO App.settings (user_id, layout, updated_at) 
    VALUES (99, 'horizontal', '2015-09-15 04:01:04');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.