Inserting rows
Databases & SQL Lesson 14 1:51 English narration · English + 中文 subtitles burned in
Chapters
Transcript
INSERT adds a row.
INSERT 添加一行。
Name the table, list the columns you are supplying, and then give the matching values.
写出表名,列出你要提供的那些列,然后给出对应的值。
Run it and the product table has a fourth row — the ruler.
运行它,product 表就多了第四行——那把尺子。
And nothing else moved: the other three rows are exactly as they were.
而其余什么都没动:另外三行还是原来的样子。
The columns and the values are two lists that must correspond.
列名和值是两个必须一一对应的列表。
Whatever order you name the columns in, the values go in the same order.
不管你按什么顺序写列名,值就按同样的顺序给。
Same count, same order, same types.
个数一样,顺序一样,类型一样。
If they do not correspond, that is an error and the row is not inserted — the database does not guess, and that is a mercy, because a guess would put the price in the stock column.
如果对不上,那就是一个错误,这一行不会被插入—— 数据库不会去猜,而这是好事, 因为一次猜测会把价格放进库存那一列里。
The same rule as WHERE: text goes in single quotes, numbers do not.
和 WHERE 里同样的规则:文字加单引号,数字不加。
What decides it is the type in the CREATE TABLE, not what the value looks like.
决定它的是 CREATE TABLE 里的那个类型,而不是这个值看起来像什么。
A product code is text even though it contains a number, and a quoted number may end up stored as text and sort wrongly later.
商品编号是文字,尽管它里面有数字; 而一个被加了引号的数字,可能最后被当作文字存起来,日后排序就会出错。
One thing surprises people the first time.
有一件事第一次会让人意外。
An INSERT returns no rows, so Run just reports that one row changed and shows you nothing — which reads like failure and is not.
INSERT 不返回任何行,所以 Run 只会报告"改动了一行",什么也不给你看—— 这看起来像失败,其实不是。
To see the row, run a SELECT after it.
要看到那一行,就在它后面跑一条 SELECT。
That is exactly what the checker does with your answer: it runs your INSERT, then reads the table back.
检查器对你的答案做的正是这件事: 先运行你的 INSERT,然后把表读回来。
Four things to take with you.
带走四点。
One: INSERT INTO names the table, then its columns.
第一:INSERT INTO 先写表名,再写列名。
Two: VALUES lists them in the same order.
第二:VALUES 按同样的顺序列出这些值。
Three: quote text and leave numbers bare.
第三:文字加引号,数字光着写。
Four: an INSERT returns no rows, so run a SELECT to see it.
第四:INSERT 不返回行,所以用一条 SELECT 去看它。
Now run the tasks below.
现在去做下面的题。