简体   繁体   中英

How to insert data into a table using SQL?

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)

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM