Linux sqlite3 basic commands

Source: Internet
Author: User

From: http://blog.sina.com.cn/s/blog_74dfa9f401017s69.html
System Platform: ubuntu10.04

Introduction
Sqlite3 is mainly used for Embedded lightweight databases. This article aims to provide technical documents for familiar with sqlite3 basic commands.
Note: All operations in this article are performed under the root user.

1. Install sqlite3
Install sqlite3 in Ubuntu and run the following command directly on the terminal:
# Apt-Get install sqlite3
View version information:
# Sqlite3-version

2. Common sqlite3 commands
Create or open the test. DB database file in the current directory, and enter the SQLite command terminal, marked with SQLite> Prefix:
# Sqlite3 test. DB

Command for viewing database file information (note that the command is prefixed with the character '.'):
SQLite>. Database

View creation statements for all tables:
SQLite>. Schema

View the statement for creating a specified table:
SQLite>. schema table_name

List table content in the form of SQL statements:
SQLite>. Dump table_name

Set the delimiter for display information:
SQLite>. Separator symble
Example: Set the display information to be separated ':'
SQLite>. separator:

Set the display mode:
SQLite>. Mode mode_name
Example: The default value is list and column. For other modes, you can use. Help to view the mode content.
SQLite>. Mode Column

Output help information:
SQLite>. Help

Set the display width of each column:
SQLite>. Width width_value
Example: Set the width to 2.
SQLite>. Width 2

List the configurations of the current display format:
SQLite>. Show

Exit SQLite terminal command:
SQLite>. Quit
Or
SQLite>. Exit

3. sqlite3 commands
SQL command format: All SQL commands end with a semicolon (;), and two minus signs (--) indicate comments.
For example:
SQLite> Create studen_table (stu_no interger primary key, Name text not null, Id interger unique, age interger check (age> 6), school text default 'xx primary school );
This statement creates a data table that records student information.

3.1 sqlite3 data storage type
Null: identifies a null value.
Interger: Integer type
Real: Floating Point Number
Text: String
BLOB: Binary Number

3.2 constraints on sqlite3 Data Storage
SQLite has the following constraints:
Primary Key-primary key:
1) The value of the primary key must be unique and used to identify each record, such as the student's student ID.
2) The primary key is also an index, and it is faster to query records through the primary key.
3) if the primary key is of the integer type, the value of this column can automatically increase
Not null-not null:
The constraint column record cannot be blank; otherwise, an error is returned.
Unique-unique:
Except the primary key, the data values of other columns are restricted to be unique.
Check-condition check:
The value of this column must meet the conditions before it can be saved.
Default-default value:
The values in column data are basically the same. Such field columns can be set as default values.

3.3 sqlite3 Common commands
1) create a data table
Create Table table_name (field1 type1, field2 type1 ,...);
Table_name is the name of the data table to be created, fieldx is the field name in the data table, and Typex is the field type.
For example, create a simple student information table that contains student information such as student ID and name:
Create Table student_info (stu_no interger primary key, Name text );

2) add data records
Insert into table_name (field1, field2,...) values (val1, val2 ,...);
Valx is the value of the field to be saved.
For example, add data to the student information table:
Insert into student_info (stu_no, name) values (0001, Alex );

3) modify data records
Update table_name set field1 = val1, field2 = val2 where expression;
Where is the command used for condition judgment in SQL statements. expression is the judgment expression.
For example, modify the data record of the student information table whose student ID is 0001:
Update student_info set stu_no = 0001, name = Hence where stu_no = 0001;

4) delete data records
Delete from table_name [where expression];
If no judgment condition is added, all data records in the table are cleared.
For example, delete the data record with the student information table student ID 0001:
Deleted from student_info where stu_no = 0001;

5) query data records
Basic SELECT command format:
Select columns from table_name [where expression];
A. query and output all data records
Select * From table_name;
B. Limit the number of output data records
Select * From table_name limit val;
C output data records in ascending order
Select * From table_name order by field ASC;
D. Output data records in descending order
Select * From table_name order by field DESC;
E-condition Query
Select * From table_name where expression;
Select * From table_name where field in ('val1', 'val2', 'val3 ');
Select * From table_name where field between val1 and val2;
F query the number of records
Select count (*) from table_name;
G-partition data
Select distinct field from table_name;
Some field values may be repeated. Distinct removes the repeated items and lists each field value in the column.

6) Create an index
When there are a large number of records in the data table, the index helps to speed up data table searching.
Create index index_name on table_name (field );
For example, create an index for the stu_no field in the student table:
Create index student_index on student_table (stu_no );
After the index is created, sqlite3 automatically uses the index when querying this field.

7) delete a data table or index
Drop table table_name;
Drop index index_name;

References:

Http://www.sqlite.com.cn/MySqlite/4/378.Html

Http://www.cnblogs.com/myqiao/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.