SQL Flashcards
how do I create a database
sqlite3 title.db or CREATE DATABASE title;
how do I create a table
CREATE TABLE title (
column1 data type constraints,
column2 data type constraints,
etc.);
how do I delete a table
DROP TABLE title;
how do I put data in a table
INSERT INTO title (column, column, etc) VALUES (value, value, etc);
how do I view a table
SELECT * FROM title;
how do I view certain columns of a table
SELECT column, column, etc FROM table;
how do I filter out the rows in a selection
SELECT column, column etc FROM table WHERE constraint;
How do I change data once in a table already
UPDATE table SET column = new value WHERE constraint;
how do I delete rows
DELETE FROM table WHERE constraint;
How do I add a column to a table
ALTER TABLE table ADD new column;
How do I delete a column in a table
ALTER TABLE table DROP column;
How do I rename a column in a table
ALTER TABLE table RENAME column TO new name;
How do I clear all data from a table
TRUNCATE TABLE table;
What are some common constraints when creating columns in a table
NOT NULL means it needs to be given a value, UNIQUE means it can not have the same value as another row, DEFAULT allows you to set a default value if left empty, AUTO_INCREMENT will increase the id each time a row is created
How can we do more complicated constraints on columns
can use CONSTRAINT label of constraint CHECK (set of constraints with logical connectives), or something like CONSTRAINT label of constraint UNIQUE(column, column) to make sure no two rows have the same values in both those columns