[Switch] centos sqlite3 installation and simple commands

安装:
方法一:
wget http:
//www.sqlite.org/sqlite-autoconf-3070500.tar.gz
tar xvzf sqlite-autoconf-3070500.tar.gz
cd sqlite-autoconf-3070500
./configure
make
sudo make install

方法二:

 sudo yum install sqlite-devel


Method three:
sudo gem install sqlite3-ruby
 
命令:
查看版本信息:
#sqlite3 -version
 sqlite3终端进入命令:
# sqlite3
 

View database file information command (Note that the command with a character before '.'):
SQLite> .database

See all table creation statement:
SQLite> .schema

view creation statement specified table:
SQLite> .schema table_name

listed in the table of contents in the form of sql statement:
SQLite> .dump table_name

set the display delimiter information:
SQLite> .separator symble
Example: setting display information ':' separator
sqlite> .separator:

set the display mode:
SQLite> .mode MODE_NAME
Example: default list, set column, other modes can view the content mode by .help
SQLite> .mode column

output help:
sqlite> .help

display width of each column is provided:
sqlite> .width width_value
Example: setting a width of 2
sqlite> .width 2

lists the current display format of the configuration:
sqlite> the .Show

exit command terminal sqlite:
sqlite>. quit
or
SQLite> .exit

. 3, the sqlite3 command
instruction format sql: All instructions are sql semicolon (;) End, two minus sign (-) indicates a comment.
Such as:
SQLite> the Create studen_table (Stu_no Interger PRIMARY KEY, the Name text the NOT NULL, UNIQUE Id Interger, Age Interger CHECK (Age> 6), the DEFAULT School text 'xx primary school);
This statement creates a data table records of student information.

3.1 sqlite3 storing data type
NULL: NULL value identifying a
INTERGER: integer type
REAL: floating
TEXT: String
BLOB: binary number

Constraints 3.2 sqlite3 storing data
Sqlite common constraint as follows:
a PRIMARY KEY - the primary key:
a) the primary key value must be unique, for identifying each record, such as students' learning No.
2) primary key is also an index, to find records by primary key faster
3) primary key if it is integer type, the column value grows automatically
nOT NULL - non-empty:
constraint list of records can not be empty, otherwise an error
uNIQUE - unique:
in addition to the primary key, the constraint value unique data of the other columns
CHECK - check the condition:
constraint values in the column must meet the conditions before deposit
dEFAULT - default:
basic value column data is the same, so the field can be set to the default column

3.3 sqlite3常用指令

1)建立数据表
create table table_name(field1 type1, field2 type1, ...);
table_name是要创建数据表名称,fieldx是数据表内字段名称,typex则是字段类型。
例,建立一个简单的学生信息表,它包含学号与姓名等学生信息:
create table student_info(stu_no interger primary key, name text);

2)添加数据记录
insert into table_name(field1, field2, ...) values(val1, val2, ...);
valx为需要存入字段的值。
例,往学生信息表添加数据:
Insert into student_info(stu_no, name) values(0001, alex);

3)修改数据记录
update table_name set field1=val1, field2=val2 where expression;
where是sql语句中用于条件判断的命令,expression为判断表达式
例,修改学生信息表学号为0001的数据记录:
update student_info set stu_no=0001, name=hence where stu_no=0001;

4)删除数据记录
delete from table_name [where expression];
不加判断条件则清空表所有数据记录。
例,删除学生信息表学号为0001的数据记录:
delete from student_info where stu_no=0001;

5)查询数据记录
select指令基本格式:
select columns from table_name [where expression];
a查询输出所有数据记录
select * from table_name;
b限制输出数据记录数量
select * from table_name limit val;
c升序输出数据记录
select * from table_name order by field asc;
d降序输出数据记录
select * from table_name order by field desc;
e条件查询
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查询记录数目
select count (*) from table_name;
g区分列数据
select distinct field from table_name;
有一些字段的值可能会重复出现,distinct去掉重复项,将列中各字段值单个列出。

6)建立索引
当说数据表存在大量记录,索引有助于加快查找数据表速度。
create index index_name on table_name(field);
例,针对学生表stu_no字段,建立一个索引:
create index student_index on student_table(stu_no);
建立完成后,sqlite3在对该字段查询时,会自动使用该索引。

7)删除数据表或索引
drop table table_name;
drop index index_name;

Guess you like

Origin www.cnblogs.com/lyggqm/p/11443095.html