1. 程式人生 > >加入,更新和刪除資料

加入,更新和刪除資料

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 .nullvalue command 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.

相關推薦

加入更新刪除資料

Inserting data The INSERT statement is used to insert data into tables. We will create a new table in which to execute our examples. sqli

3.插入更新刪除資料-mysql

一、插入資料插入資料的關鍵詞是INSERT INTO,給一張表插入資料可分給所有欄位插入資料,給指定欄位欄位插入資料。1.1 給所有欄位插入資料INSERT INTO 表名 VALUES(值 1,值 2,值 3,...,值 n);表名的後面沒有加上具體的欄位,後面的value

MySQL 新增、更新刪除資料

一、新增資料 在MySQL中使用INSERT新增資料分為: 為表中所有欄位新增資料 為表的指定欄位新增資料 同時新增多條資料 1.為表中所有欄位新增資料 使用insert的MySQL語句為: INSERT INTO 表名(欄位名1,欄位名2,,,,,欄位名

thinkphp更新刪除資料

$Dao = M('User'); $data['email'] = "..."; //需要更新的資料 $condition['username'] = "admin";//條件 $Dao->where($condition)->save($data);//更新username=admin的ema

SQL 更新刪除資料

這條語句很容易理解。DELETE FROM要求指定從中刪除資料的表名,WHERE子句過濾要刪除的行。在這個例子中,只刪除顧客1000000006。如果省略WHERE子句,它將刪除表中每個顧客。因為vend_id列是作為外來鍵連線到Vendors表的。那麼,這與DELETE有什麼關係呢?使用外來鍵確保引用完

更新刪除資料

更新資料,即對錶中存在的資料進行修改。 SQL語句: UPDATE 語句 基本語法:UPDATE 表名 SET 欄位名1=值1[,欄位名2=值2,...] [WHERE 條件表示式]語法說明: 欄位名1,欄位名2,用於指定更新的欄位名稱 值1,值2,用於表示欄位更新

Android的SharedPreferences儲存讀取刪除資料

SharedPreferences類是一個輕量級的儲存類,特別適合用於儲存軟體配置引數。使用SharedPreferences儲存資料,其背後是用xml檔案存放資料,檔案存放在/data/data/&

MySQL更新刪除資料

更新資料:UPDATE table1 SET column1=‘ ’ WHERE 直接update表,一定要寫條件where 更新多個列,直接在前一句SET後加英文逗號即可。 可使用子查詢。 更新多列時,如果有其中有一列出錯,之前更新好的資料會恢復原來的值,避免這種情=況,可以用IGNORE忽略錯誤。

更新刪除資料(UPDATE\DELETE)

1. 更新資料可使用UPDATE語句。可採用兩種方式:     (1)更新表中特定行   (2)更新表中所有行 簡單例子:客戶10005要更新電子郵件地址,可使用 UPDATE customers SET cust_email = '[email protect

Day2(11.1):(3)插入、更新刪除資料

-- 3 **************************************************** -- 插入、更新和刪除資料 -- 3.1 -------------------------------------------------- -- INSE

SQL讀書筆記(十一)更新刪除資料

筆記參考來自SQL必知必會,摘抄下書中的一些關鍵方便以後自己查詢 更新和刪除都是比較敏感的操作,因為沒辦法撤銷,所以修改時要注意,更新主要分為更新特定行和所有行。所以要十分注意避免更高錯誤 不要省略WHERE子句 在使用UPDATE時一定要細心。因為稍不注意

MYSQL學習筆記(十六)更新刪除資料

更新資料 UPDATE Customers SET cust_email = ‘[email protected]’, cust_name=’The Fudds’ WHERE

第十六課 更新刪除資料

第十六課、更新和刪除資料        預習與回顧:        第十五課學習如何將行插入到資料庫表中;第十六課講學習UPDATE和DELETE進一步操作表資料。        16.1、更新資料        UPDATE更新表中的特定行;更新表中的所有行。!!不要省略

MongoDB 創建更新刪除文檔

mongo 類型 cti $set view 並且 n個元素 value done 插入並保存文檔 可以使用insert方法想目標集合插入一個文檔: > db.foo.insert({"bar": "baz"}) ,這個操作會給文檔自動添加一個“_id&

AngularJS進階(十一)AngularJS實現表格資料的編輯,更新刪除

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興! AngularJS實現表格資料的編輯,更新和刪除 效果   實現 首先,我們先建立一些資料,當然你可以從你任何地方讀出你的資料 var app = angular.module('plu

資料結構】大小堆的理解建立增加刪除元素操作

什麼是大小堆? 大小堆是基於完全二叉樹的結構; 大堆:任意一個結點的左右孩子的資料都小於此結點的資料,位於堆頂的結點的資料最大。 小堆:任意一個結點的左右孩子的資料都大於此結點的資料,位於堆頂的結點的資料最小。 下面以小堆為例,圖解: 以下都是以小堆為例 如何

資料結構作業程式碼儲存-2.1 單向迴圈連結串列的建立插入刪除指標移動

在此感謝我親愛的大神同學們!一個Z妹子講情了思路,一個M妹子提供了更高階的雙向迴圈連結串列的程式碼借鑑(雖然有些還是沒怎麼看懂。。。 我的作業就完成啦!儲存程式碼當個紀念! 1,往操作位置順時針的下一位插入一個元素,並將操作位置移到新元素上。 2,刪掉操作位置順時針方

增加刪除資料為什麼LinkedList通常比ArrayList快?

實驗  首先我們做一個實驗:將10萬條String型別的資料分別新增到一個LinkedList和一個ArrayList中,且每次都是在第0位(即首位)插入資料,程式碼如下 結果是LinkedList比

EF結合SqlBulkCopy實現高效的批量資料插入 |EF外掛EntityFramework.Extended實現批量更新刪除

批量插入 (17597條資料批量插入耗時1.7秒) using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; na

分別使用PreparedStatementStatement對mysql資料庫進行建立表增加資料查詢資料刪除資料過程

在使用eclipse工具編寫Java程式碼連線資料庫並對資料庫進行處理時,總會用到對資料的增刪改查操作。那麼這個 時候就用到了java自帶的sql庫中的PreparedStatement或者Statement了。 其實PreparedStatement和Statement使