SQLite

De Banane Atomic
Révision datée du 17 juillet 2013 à 09:10 par Nicolas (discussion | contributions) (→‎Configuration)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)
Aller à la navigationAller à la recherche

Installation

Bash.svg
sudo pacman -S sqlite

# sqlite3 module for PHP 
sudo pacman -S php-sqlite

Configuration

/etc/php/php.ini
extension=sqlite3.so

GUI

Requêtes

Datetime

Sqlite.svg
INSERT INTO `ma_table` VALUES ('2004-12-15');

Formats de date :

  • 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
  • now

INSERT or UPDATE

Sqlite.svg
-- Ajoute 1 Hello commentaire si l'entrée avec l'id 1 n'existe pas
-- Sinon écrase l'ancien text par Hello et conserve comment
INSERT OR REPLACE INTO ma_table (id, text, comment) 
  VALUES ( 1, 'Hello', coalesce((select comment from ma_table where id = 1),'commentaire') );

Création

PRIMARY KEY

Sqlite.svg
CREATE TABLE ma_table (
  id INTEGER PRIMARY KEY ASC,
  text VARCHAR(45) NULL
);

# ajout d'un nouvel élément, la clé primaire est assignée automatiquement
INSERT INTO ma_table (text) VALUES ('hello');
INSERT INTO ma_table VALUES (null, 'hello');

In SQLite, every row of every table has an 64-bit signed integer ROWID. The ROWID for each row is unique among all rows in the same table.
If a table contains a column of type INTEGER PRIMARY KEY, then that column becomes an alias for the ROWID.
Il n'est donc pas utile d'utiliser NOT NULL.

AUTOINCREMENT

Sqlite.svg
CREATE TABLE ma_table (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  text VARCHAR(45) NULL
);

Force à assigner une clé primaire supérieure à la plus grande clé primaire de la table.

Index

Permet d'optimiser les recherches par l'indexation de colonnes.

Sqlite.svg
-- Création d'un index sur une colonne unique
CREATE INDEX index_idx ON table (colonne ASC);
La BdD créé automatiquement des index pour les contraintes PRIMARY KEY et UNIQUE

Vue

Table virtuelle crée à partir d'un requête prédéfinie stockée dans la BdD.

MySQL vs SQLite

PRIMARY KEY

Mysql.svg
CREATE TABLE ma_table (
  id INT NOT NULL AUTO_INCREMENT ,
  PRIMARY KEY (id) );
Sqlite.svg
CREATE TABLE ma_table (
  id INTEGER PRIMARY KEY ASC );

INDEX

Mysql.svg
CREATE TABLE ma_table (
  table1_id INT NOT NULL ,
  table2_id INT NOT NULL ,
  PRIMARY KEY (table1_id, table2_id) ,
  INDEX fk_table1_idx (table1_id ASC) ,
  INDEX fk_table2_idx (table2_id ASC) ,
  CONSTRAINT fk_table1
    FOREIGN KEY (table1_id)
    REFERENCES table1 (id)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT fk_table2
    FOREIGN KEY (table2_id)
    REFERENCES table2 (id)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);
Sqlite.svg
CREATE TABLE ma_table (
  table1_id INTEGER NOT NULL ,
  table2_id INTEGER NOT NULL ,
  PRIMARY KEY (table1_id, table2_id) ,
  CONSTRAINT fk_table1
    FOREIGN KEY (table1_id)
    REFERENCES table1 (id)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT fk_table2
    FOREIGN KEY (table2_id)
    REFERENCES table1 (id)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);
    
CREATE INDEX fk_table1_idx ON ma_table (table1_id ASC);
CREATE INDEX fk_table2_idx ON ma_table (table2_id ASC);