加入,更新和删除数据
2014-04-26 09:31
246 查看
Inserting data
The INSERT statement is used to insert data into tables. We will create a new table in which to execute our examples.sqlite> CREATE TABLE Books(Id INTEGER PRIMARY KEY, Title TEXT, Author TEXT, ...> ISBN TEXT DEFAULT 'not available');
We create a new table Books, with Id, Title, Author and ISBN columns.
sqlite> INSERT INTO Books(Id, Title, Author, ISBN) ...> VALUES(1, 'War and Peace', 'Leo Tolstoy', '978-0345472403');
This is the classic INSERT SQL statement. We have specified all column names after the table name and all values after the VALUES keyword. We add our first row into the table.
sqlite> INSERT INTO Books(Title, Author, ISBN) ...> VALUES('The Brothers Karamazov', 'Fyodor Dostoyevsky', '978-0486437910');
We add a new title into the Books table. We have omitted the Id column. The Id column is defined as INTEGER PRIMARY KEY. Such columns are auto-incremented in SQLite. This means the SQLite
library will add a new Id.
sqlite> SELECT * FROM Books; Id|Title|Author|ISBN 1|War and Peace|Leo Tolstoy|978-0345472403 2|The Brothers Karamazov|Fyodor Dostoyevsky|978-0486437910
Here is what we have in the Books table.
sqlite> INSERT INTO Books VALUES(3, 'Crime and Punishment', 'Fyodor Dostoevsky', ...> '978-1840224306');
In this SQL statement, we did not specify any column names after the table name. In such a case, we have to supply all values.
sqlite> .nullvalue NULL
The
.nullvaluecommand tells the SQLite to show NULL values as NULL. SQLite shows empty strings for NULL values by default.
sqlite> INSERT INTO Books(Id, Title) VALUES(4, 'Paradise Lost');
The INSERT statement omits the last 2 columns. Such columns are filled with the default value, or NULL if there is no default value. The Author column does not have a default value, so there
is a NULL value. In the CREATE TABLE statement, we have specified the ISBN column to have the 'not available' default value.
sqlite> SELECT * FROM Books WHERE Id=4; Id|Title|Author|ISBN 4|Paradise Lost|NULL|not available
In the third column we have a NULL value. The fourth has the default 'not available' string.
sqlite> INSERT INTO Books VALUES(4, 'Paradise Lost', 'John Milton', '978-0486442877'); Error: PRIMARY KEY must be unique sqlite> INSERT OR REPLACE INTO Books VALUES(4, 'Paradise Lost', 'John Milton', ...> '978-0486442877');
Say we want to put all information into the fourth column. Trying to insert new data into existing row produces the following error: 'PRIMARY KEY must be unique'. In such a case we can use
the INSERT OR REPLACE statement. The same could be accomplished with the UPDATE statement.
sqlite> SELECT * FROM Books WHERE Id=4; Id Title Author ISBN ---------- ------------- ----------- -------------- 4 Paradise Lost John Milton 978-0486442877
Now we have all information in the fourth row.
Since SQLite version 3.7.11 it is possible to insert multiple rows using one INSERT statement.
sqlite> CREATE TABLE Ints(Id INTEGER PRIMARY KEY, Val INTEGER);
We will use a one-column Ints table to show a multi-row INSERT statement. The table's lone column stores integers.
sqlite> INSERT INTO Ints(Val) VALUES (1), (3), (5), (6), (7), (8), (6), (4), (9);
We insert nine rows into the table in one shot. The rows follow the VALUES keyword and are separated by a comma character.
sqlite> SELECT * FROM Ints; Id Val ---------- ---------- 1 1 2 3 3 5 4 6 5 7 6 8 7 6 8 4 9 9
These are the contents of the Ints table.
We can use the INSERT and SELECT statements together in one statement.
sqlite> CREATE TABLE Books2(Id INTEGER PRIMARY KEY, Title TEXT, ...> Author TEXT, ISBN TEXT);
First, we create a new table called Books2. Its schema is identical to Books table.
sqlite> INSERT INTO Books2 SELECT * FROM Books;
Here we insert all data from the Books table into the Books2 table.
sqlite> SELECT * FROM Books2; Id|Title|Author|ISBN 1|War and Peace|Leo Tolstoy|978-0345472403 2|The Brothers Karamazov|Fyodor Dostoyevsky|978-0486437910 3|Crime and Punishment|Fyodor Dostoevsky|978-1840224306 4|Paradise Lost|NULL|not available
We verify it. All is OK.
Deleting data
The DELETE keyword is used to delete data from tables. First, we are going to delete one row from a table. We will use the Books2 table, which we have created previously.sqlite> DELETE FROM Books2 WHERE Id=1;
We delete a row with Id 1.
sqlite> SELECT * FROM Books2; Id|Title|Author|ISBN 2|The Brothers Karamazov|Fyodor Dostoyevsky|978-0486437910 3|Crime and Punishment|Fyodor Dostoevsky|978-1840224306 4|Paradise Lost|NULL|not available
We verify that the first row is missing.
sqlite> DELETE FROM Books2;
This SQL statement deletes all data in the table.
Updating data
The UPDATE statement is used to change the value of columns in selected rows of a table.Say we wanted to change 'Leo Tolstoy' to 'Lev Nikolayevich Tolstoy' in our Books table. The following statement shows how to accomplish this.
sqlite> UPDATE Books SET Author='Lev Nikolayevich Tolstoy' WHERE Id=1;
The SQL statement sets the author column to 'Lev Nikolayevich Tolstoy' for the column with id=1.
sqlite> SELECT * FROM Books WHERE Id=1; Id|Title|Author|ISBN 1|War and Peace|Lev Nikolayevich Tolstoy|978-0345472403
The row is correctly updated.
相关文章推荐
- hibernate一对多,更新主表时删除旧的多方数据加入新的多方数据
- MySQL学习笔记 第八讲:增加、删除、更新数据和备份还原
- 【iOS开发-76】Private Contacts案例:导航控制器使用、数据传递、第三方类库使用、tableViewCell的加入删除、数据存储等
- MySQL 入门 之 数据的插入、查询、更新与删除
- SQL读书笔记(十一)更新和删除数据
- C# 向数据库批量更新数据(插入、更新、删除)
- SQL入门(二)添加更新与删除数据
- MySQL常用指令(三) 插入、更新与删除数据
- 三种DML操作:插入数据,更新数据,删除数据.
- MySQL入门(四) 数据表的数据插入、更新、删除
- MySQL基础(6)——插入数据、更新和删除数据
- SQL 必知必会·笔记<14>更新和删除数据
- hibernate批量删除和更新数据
- MySQL笔记(三)之数据插入更新与删除
- hbase中创建表、插入数据,更新数据,删除数据
- hibernate (多对多) 执行update更新操作时,关联表数据会被删除
- MSSQL 中使用modify()方法,此方法使用XML DML语句在XML 数据中插入,更新或删除节点
- arcengine,深入理解游标Cursors,实现数据的快速查找,插入,删除,更新 (转)
- Django中更新多个对象数据与删除对象的方法
- oracle学习 六 删除表空间,数据文件的语句以及导入导出dmp文件的方法(持续更新中)