SQL Database Flashcards
(48 cards)
create a new SQL database
CREATE DATABASE databaseName;
NOTE: must have admin privilege
SHOW DATABASES;
how can we drop an existing SQL database
DROP DATABASE databaseName;
in SQL server you can backup a database
BACKUP DATABASE databaseName
TO DISK = ‘filepath’;
what is a differential backup?
backs up only parts of database that have changed since last full database backup
BACKUP DATABASE databaseName
TO DISK = ‘filepath’
WITH DIFFERENTIAL;
create a table
CREATE TABLE tableName (
column1 dataType,
column2 dataType,
…
);
create table use existing table
CREATE TABLE new_table_name AS
SELECT column1, column2, …
FROM existing_table_name
WHERE …;
drop a table
DROP TABLE table_name;
what statement deletes the data inside a table but not the table itself
TRUNCATE TABLE table_name;
how to alter table statement to add, delete, or modify columns in an existing table or even add and drop various constraints on an existing table
ALTER TABLE table_name
…
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 with MySQL example
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
what are SQL constraints?
used to specify rules for data in a table and can be used with CREATE TABLE or ALTER TABLE
They can be table level or column level
common constraints used in SQL
NOT NULL - Ensures that a column cannot have a NULL value
UNIQUE - Ensures that all values in a column are different
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table
FOREIGN KEY - Prevents actions that would destroy links between tables
CHECK - Ensures that the values in a column satisfies a specific condition
DEFAULT - Sets a default value for a column if no value is specified
CREATE INDEX - Used to create and retrieve data from the database very quickly
what is a foreign key
a field or collection of fields in one table that refers to the primary key in another table
foreign key syntax
FOREIGN KEY (PersonID) References Persons(PersonID)
CHECK constraint example
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);
default constraint example
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT ‘Sandnes’
);
what are indexes used for?
to retrieve data from database more quickly than otherwise; the users cannot see the indexes, they are just used to speed up searches/queries
create index syntax - duplicate values allowed
CREATE INDEX index_name
ON table_name (column1, column2, …);
create index syntax - duplicate values NOT allowed
CREATE UNIQUE IDEX index_name
ON table_name (column1, column2, …);
drop index statement - MySQL
ALTER TABLE table_name
DROP INDEX index_name;