SQL Need to Know Flashcards
SQL CREATE DATABASE
CREATE DATABASE databasename;
SQL DROP DATABASE
DROP DATABASE databasename;
SQL BACKUP DATABASE
BACKUP DATABASE databasename
TO DISK = ‘filepath’;
SQL BACKUP WITH DIFFERENTIAL
A differential back up only backs up the parts of the database that have changed since the last full database backup.
BACKUP DATABASE databasename
TO DISK = ‘filepath’
WITH DIFFERENTIAL;
SQL CREATE TABLE
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
….
);
SQL Create Table Using Another Table
CREATE TABLE new_table_name AS
SELECT column1, column2,…
FROM existing_table_name
(OPTIONAL) WHERE ….;
SQL DROP TABLE
DROP TABLE table_name;
SQL TRUNCATE TABLE
TRUNCATE TABLE table_name;
SQL ALTER TABLE - ADD Column
ALTER TABLE table_name
ADD column_name datatype;
ALTER TABLE - DROP COLUMN
ALTER TABLE table_name
DROP COLUMN column_name;
ALTER TABLE - RENAME COLUMN
ALTER TABLE table_name
RENAME COLUMN old_name to new_name;
ALTER TABLE - ALTER/MODIFY DATATYPE
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
SQL Create Constraints
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
….
);
SQL NOT NULL on CREATE TABLE
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
SQL NOT NULL on ALTER TABLE
ALTER TABLE Persons
MODIFY COLUMN Age int NOT NULL;
SQL UNIQUE Constraint on CREATE TABLE
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
UNIQUE (ID)
);
multiple columns:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT UC_Person UNIQUE (ID,LastName)
);
SQL UNIQUE Constraint on ALTER TABLE
ALTER TABLE Persons
ADD UNIQUE (ID);
multiple columns:
ALTER TABLE Persons
ADD CONSTRAINT UC_Person UNIQUE (ID,LastName);
DROP a UNIQUE Constraint
ALTER TABLE Persons
DROP INDEX UC_Person;
SQL PRIMARY KEY
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);