Skip to content
This repository was archived by the owner on Oct 25, 2018. It is now read-only.

Latest commit

 

History

History
54 lines (47 loc) · 1.87 KB

File metadata and controls

54 lines (47 loc) · 1.87 KB

Structured Query Language Cheat Sheet

Working with Tables

Creating a new table

CREATE TABLE IF NOT EXISTS
users(
  id SERIAL PRIMARY KEY NOT NULL,
  first_name VARCHAR(256) NOT NULL,
  last_name VARCHAR(256) NOT NULL,
  ssn INTEGER NOT NULL,
  ninja_status BOOLEAN NOT NULL,
  biography TEXT NOT NULL
);

Deleting a table

DROP TABLE users;

Working with Records

Reading all record from the DB

SELECT * FROM users;

Reading a specific record from the DB

SELECT * FROM users WHERE id=3;

Inserting a new record

INSERT INTO users(first_name, last_name, ssn, ninja_status, biography)
VALUES('Flibbity', 'Jibbit', 333-44-5555, TRUE, 'By the time of the Meiji Restoration (1868), the tradition of the shinobi had become a topic of popular imagination and mystery in Japan. Ninja figured prominently in legend and folklore, where they were associated with legendary abilities such as invisibility, walking on water and control over the natural elements. As a consequence, their perception in popular culture is often based on such legend and folklore than on the spies of the Sengoku period.');

Updating an existing record

UPDATE users
SET
  first_name='Flibbity',
  last_name='Jibbit',
  ssn=333-44-5555,
  ninja_status=FALSE, -- We changed this value!!
  biography='By the time of the Meiji Restoration (1868), the tradition of the shinobi had become a topic of popular imagination and mystery in Japan. Ninja figured prominently in legend and folklore, where they were associated with legendary abilities such as invisibility, walking on water and control over the natural elements. As a consequence, their perception in popular culture is often based on such legend and folklore than on the spies of the Sengoku period.'
WHERE id=3;

Deleting a record from the DB

DELETE FROM users WHERE id=3;