inserting, updating and deleting data

5
Home Contents Inserting, updating and deleting data In this part of the SQLite tutorial, we will insert, update and delete data from SQLite tables. We will use the INSERT , DELETE and UPDATE statements. These statements are part of the SQL Data Manipulation Language, DML . Inserting data The INSERT statement is used to insert data into tables. We will create a new table, where we will do our examples. 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 increment in SQLite. This means, the SQLite library will add a new id.

Upload: punu82

Post on 21-Jan-2015

1.565 views

Category:

Education


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Inserting, updating and deleting data

Home Contents

Inserting, updating and deleting data

In this part of the SQLite tutorial, we will insert, update and delete data from SQLite tables. We will use

the INSERT, DELETE and UPDATE statements. These statements are part of the SQL Data

Manipulation Language, DML.

Inserting data

The INSERT statement is used to insert data into tables.

We will create a new table, where we will do our examples.

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 increment in SQLite. This means, the SQLite library will add

a new id.

Page 2: Inserting, updating and deleting data

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

sqlite> INSERT INTO Books(id, title) VALUES(4, 'Paradise Lost');

sqlite> SELECT * FROM Books;

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

Page 3: Inserting, updating and deleting data

The .nullvalue command tells the SQLite to show NULL values as NULL. SQLite shows empty strings for

NULL values by default. 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> INSERT INTO Books VALUES(4, 'Paradise Lost', 'John Milton',

'978-0486442877');

SQL 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. We have to use the REPLACE keyword,

otherwise we get an SQL error saying: 'PRIMARY KEY must be unique'.

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.

We can use the INSERT and SELECT statements together in one statement.

sqlite> CREATE TEMP TABLE BooksTemp(id integer primary key, title text, author

text, isbn text);

First, we create a temporary table called BooksTemp.

sqlite> INSERT INTO BooksTemp SELECT * FROM Books;

Here we insert all data into the BooksTemp that we select from the Books table.

Page 4: Inserting, updating and deleting data

sqlite> SELECT * FROM BooksTemp;

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|John Milton|978-0486442877

We verify it. All 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 BooksTemp table, that we have created previously.

sqlite> DELETE FROM BooksTemp WHERE id = 1;

We delete a row with id = 1.

sqlite> SELECT * FROM BooksTemp;

id|title|author|isbn

2|The Brothers Karamazov|Fyodor Dostoyevsky|978-0486437910

3|Crime and Punishment|Fyodor Dostoevsky|978-1840224306

Page 5: Inserting, updating and deleting data

4|Paradise Lost|John Milton|978-0486442877

Verify that.

sqlite> DELETE FROM BooksTemp;

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.

In this part of the SQLite tutorial, we have inserted, deleted and updated data in database tables.