Introduction and use SQLite databases

A, Sqlite profile: 

  SQLite (http://www.sqlite.org/) , is a lightweight database, is to comply with ACID relational database management system, its design goal is embedded, and is now used in many embedded products it, it takes up resources are very low in embedded devices, may only need a few hundred K of memory is enough. It supports Windows / Linux / Unix , and so on mainstream operating system, but can be combined with many programming languages, such as Tcl , C # , PHP , the Java , etc., as well as ODBC interfaces, compared to the same Mysql , PostgreSQL both open-source world the famous database management system is concerned, its processing speed is faster than them. SQLite first Alpha version was born in 2000 Nian 5 Yue . It has been 10 years, SQLite also ushered in a version of the SQLite 3 has been released.

Two, Sqlite of words of advice:
   O May you do Good and not Evil. May you do good mo evil o May you find forgiveness for yourself and forgive others. May you forgive yourself forgive others o May you share freely, never taking. May your heart wide to share with others, you have taken no more than administer 
   
  

Three, Sqlite command:
   sqlite3 too.db created called too databases, not necessarily with the suffix db
   .help help .quit leave 
  

Four, Sqlite client tools:

  SQLiteExpertPers

 

Six, Sqlite of sql statement:

Built table: the Create the Table table_name (field1, Field2, Field3, ...); 
  Example: Creating named film of the database table
     create table film (_id Integer primaray key  autoincrement, title, length, year, starring);

Note: The statement should end with a semicolon, without specifying the type of the field, it will automatically convert timely
    can store text, numbers, large text (blub)

Create an index: the Create index index_name ON table_name (field_to_be_indexed);
  example: for table film 's title to create a field called film_title_index index
     create index film_title_index on film (title) ;
  Note: When more data tables, indexes can speed up queries ( the premise is built according to the index field query )

Adding Data: INSERT INTO table_name (field1, Field2, Field3, ...) values (DATAl, DATA2, DATA3, ...);
  Example: to Table film add a record
     insert into film (title, length, year, starring ) values ( 'Contact', 153,1997 , 'Jodie Foster');
  Note: the name of the statement may be omitted, provided that the number of fields with the same number of data
    if a field value is a value not added null, may be added manually null value

Query data: the SELECT the Columns from the WHERE expression The table_name;
  examples: from the table film query data in
     a display of all data table all the fields of the SELECT * from film;  2 if too much information, we might want to limit the items: the SELECT * from limit 10 film;  3 shining movie to arrange the Year: the SELECT * from the Order film year limit by 10;  4 years more recent movies listed first: the SELECT * from the Order by year film desc limit 10;  5 we just want to watch a movie name with the year: the SELECT title, year by year from the Order film desc limit 10;  6 check all played Jodie Foster's movie: the SELECT * from the WHERE film starring = 'Jodie Foster';  7 check all actors at the beginning named Judy the movie ( '%' sign is SQL 's wildcard characters): 
      
     
      
     
      
     
      
     
      
     
      
     
      * Film from the WHERE starring the SELECT like 'Jodie%'; 
    8 check all the actors whose names begin with Judy, years later than the 1985 -year, late in the priority list, up to ten pen, list only the name and year of the movie: the SELECT title, year from film the WHERE starring like 'Jodie%' and year> = 1985  the Order by year desc limit 10;  9 to view the database a number of records total: the SELECT COUNT (*) from film;  10 View 1985 years after the movie has a few: select count (*) from film where year> = 1985; 
      
      
     
      
     
      

Data Update: Update Film starring SET = 'Jodie Foster' = WHERE starring 'Jodee Foster'; 
  the lead field 'Jodee Foster' all records into Jodie Foster .

Delete the data: the Delete Film from the WHERE year <1970; 
  delete all year older than 1970 years (not) movie recording

Comments: Comments single line: -
  Comment multiple lines: / * * /

Create a view: the CREATE VIEW AS View the SELECT-name-of Statement

Fuzzy Match: like%

SQLite Day Date function:
   datetime () creation date and time
   date () creation date
   time () generation time
   strftime () date and time of the above three functions produce formatted
  available string parameters:
   now the time now produce
   YYYY-MM-DD
  YYYY-MM-DD HH: MM
  YYYY-MM-DD HH: MM: SS
  YYYY-MM-DD HH: MM: SS.SSS
  HH: MM
  HH: MM: SS
  HH: MM : SS.SSS
  example:
     SELECT datetime ( 'now');
    SELECT datetime ( '2011-06-12');
    SELECT datetime ( '2006-10-17 00:20:00', '. 1 hour +', '- minute 12 is');
    SELECT DATE ( '2006-10-17', '. 1 Day +', '+ year. 1');
    SELECT datetime ( 'now', 'localtime');

- Modify Table Structure
   - add a field ALTER Table Film director2 the Add column;  - Delete a field not alter table film drop (column director2) ; 
    
  
    

  - Delete a table

    drop table test;

sqlite special use sqlite can shell executed under the direct command:     output HTML tables: sqlite3 -html film.db "from the SELECT * Film;"      database "pour out": sqlite3 film.db ".dump"> output.sql      use output data, the establishment of an identical database (plus instructions above, is the standard SQL database backup): sqlite3 film.db <output.sql      when a large number of insert data, you may need to hit this command: the begin;      insert after to remember playing this command, the data will be written into the database: the commit; 
   


 
    

Seven Exercise:
   - Creating Employee table
     the CREATE TABLE the EMPLOYEES (
      employee_id Integer PRIMARY KEY, 
      department_id Integer, 
      location_id Integer, 
      first_name, 
      last_name, 
      salary,
      hire_date DATE 
    );
  - create a department table
     the CREATE TABLE the DEPARTMENT (
      department_id Integer Primary Key, 
      name
    ) ;
  create jobs table
     the cREATE tABLE LOCATION (
      location_id Integer PRIMARY KEY, 
      name
    );

  添加测试数据
    insert into [employees](department_id,location_id , first_name, last_name, salary,hire_date)
      values (1,1, 'A', 'z', 50000, '2005-02-21'); 
    insert into [employees](department_id,location_id, first_name, last_name, salary,hire_date)
      values (1,2, 'B', 'x', 20000, '2009-03-21'); 
    insert into [employees](department_id,location_id , first_name, last_name, salary,hire_date)
      values (2,3, 'C', 'v', 10000, '2009-08-23'); 
    insert into [employees](department_id,location_id, first_name, last_name, salary,hire_date)
      values (4,2, 'D', 'n', 30000, '2004-09-28'); 
    insert into [employees](department_id,location_id, first_name, last_name, salary,hire_date)
      values (3,5, 'E', 'm', 3000, '2009-04-11'); 
    insert into [employees](department_id,location_id, first_name, last_name, salary,hire_date)
      values (5,5, 'F', 'l', 5000, '2008-03-11'); 
    insert into [employees](department_id,location_id, first_name, last_name, salary,hire_date)
      values (5,3, 'G', 'p', 20000, '2005-05-09'); 
    insert into [employees](department_id,location_id, first_name, last_name, salary,hire_date)
      values (5,4, 'H', 'o', 8000, '2006-07-21'); 
    insert into [employees](department_id,location_id, first_name, last_name, salary,hire_date)
      values (5,5, 'I', 'u', 6000, '2006-09-21'); 
    insert into [employees](department_id,location_id, first_name, last_name, salary,hire_date)
      values (5,5, 'J','And' 5500, '2007-08-21'); 
    insert into [employees](department_id,location_id, first_name, last_name, salary,hire_date)
      values (5,5, 'K', 't', 6500, '2006-12-21'); 
    insert into [employees](department_id,location_id, first_name, last_name, salary,hire_date)
      values (5,1, 'L', 'r', 100000, '2001-05-21'); 

    insert into department(name) values ('人事');
    insert into department(name) values ('财务');
    insert into department(name) values ('后勤');
    insert into department(name) values ('公关');
    insert into department(name) values ('研发');

    insert into location (name) values ( ' general manager ');
     insert into location (name) values ( ' manager ');
     insert into location (name) values ( ' competent ');
     insert into location (name) values ( ' Group long ');
     INSERT INTO LOCATION (name) values (' employee ');

  - check employee information development staff portion
     select * from employees e where e.location_id = (select l.location_id from location l where l.name = ' employee ')
     and
    E [DEPARTMENT_ID] = (SELECT d.department_id from. department d where d.name = ' R & D ');

  - Creating a query to a table
     create table TEMP_EMPLOYEES AS select employee_id, first_name , last_name from EMPLOYEES where salary> 6000;

  - queries can be computed
     select salary * 13 salary from employees where salary = 260000;!
     Select salary * 13 salary from employees where salary BETWEEN 50000 and 100000;
   --first_name is A, B, C staff information
     select * from employees where first_name in ( 'A', ' B', 'C');

  --测试is null
    select * from film where title is null;

  - Query salary greater than 10000 executives
     select * from employees where salary> 10000 and location_id = 3;

  - Query salary greater than 10,000 competent or understanding
     SELECT * WHERE from the Employees the salary> 10000 and (or location_id location_id = = 2. 3);
    SELECT * WHERE from the Employees the salary> 10000 and location_id Not in (4,5,1);

  --测试order by
    select * from employees order by location_id,salary;

Guess you like

Origin www.cnblogs.com/zhaye/p/10923046.html