SQL Database Flashcards
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;
what does Auto-increment do?
allows a unique number to be generated automatically when a new record is inserted into a table
auto-increment example
CREATE TABLE Persons (
Personid int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (Personid)
);
MySQL date data types
DATE - format YYYY-MM-DD
DATETIME - format: YYYY-MM-DD HH:MI:SS
TIMESTAMP - format: YYYY-MM-DD HH:MI:SS
YEAR - format YYYY or YY
what is a view in SQL?
a virtual table based on result-set of a SQL statement
what does a view contain in SQL?
contains rows and columns just like a real table
create view syntax
CREATE VIEW view_name AS
SELECT column1, column2, …
FROM table_name
WHERE condition;
does the view shop up-to-date data?
YES, always! The database engine recreates the view, every time a user queries it.
create view example
CREATE VIEW [Brazil Customers] AS
SELECT CustomerName, ContactName
FROM Customers
WHERE Country = ‘Brazil’;
sql updating a view
CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, …
FROM table_name
WHERE condition;
sql dropping a view
DROP VIEW view_name;
ex.
DROP VIEW [Brazil Customers];
what is a SQL injection?
a code injection technique that might destroy your database
one of most common web hacking techniques
placement of malicious code in SQL statements, via web page input
when does SQL in web pages occur?
usually when you ask a user for input like a username and user gives you a SQL statement that you will unknowingly run on your database
sql injection example
1 = 1 is always True
SELECT * FROM Users WHERE UserId = 105 OR 1=1;
another SQL injection that is always True
””=””
SELECT * FROM Users WHERE Name =”” or “”=”” AND Pass =”” or “”=””
another way to do sql injection is with batched sql statements
SELECT * FROM Users; DROP TABLE Suppliers
how to protect website from SQL injection?
use SQL parameters
these are values that are added to a SQL query at execution time, in a controlled manner
sql hosting
if you want your website to be able to store and retrieve data from a database, your web server should have access to a database-system that uses the SQL language
if your web server is hosted by an ISP, you will have to look for SQL hosting plans
most common SQL hosting databases: MS SQL Server, Oracle, MySQL, and MS Access
data types of a column defines what?
value the column can hold and how SQL can interact with the stored data
MySQL string data types
CHAR(size) A FIXED length string (can contain letters, numbers, and special characters). The size parameter specifies the column length in characters - can be from 0 to 255. Default is 1
VARCHAR(size) A VARIABLE length string (can contain letters, numbers, and special characters). The size parameter specifies the maximum string length in characters - can be from 0 to 65535
BINARY(size) Equal to CHAR(), but stores binary byte strings. The size parameter specifies the column length in bytes. Default is 1
VARBINARY(size) Equal to VARCHAR(), but stores binary byte strings. The size parameter specifies the maximum column length in bytes.
TINYBLOB For BLOBs (Binary Large Objects). Max length: 255 bytes
TINYTEXT Holds a string with a maximum length of 255 characters
TEXT(size) Holds a string with a maximum length of 65,535 bytes
BLOB(size) For BLOBs (Binary Large Objects). Holds up to 65,535 bytes of data
MEDIUMTEXT Holds a string with a maximum length of 16,777,215 characters
MEDIUMBLOB For BLOBs (Binary Large Objects). Holds up to 16,777,215 bytes of data
LONGTEXT Holds a string with a maximum length of 4,294,967,295 characters
LONGBLOB For BLOBs (Binary Large Objects). Holds up to 4,294,967,295 bytes of data
ENUM(val1, val2, val3, …) A string object that can have only one value, chosen from a list of possible values. You can list up to 65535 values in an ENUM list. If a value is inserted that is not in the list, a blank value will be inserted. The values are sorted in the order you enter them
SET(val1, val2, val3, …) A string object that can have 0 or more values, chosen from a list of possible values. You can list up to 64 values in a SET list
MySQL numeric data types
BIT(size) A bit-value type. The number of bits per value is specified in size. The size parameter can hold a value from 1 to 64. The default value for size is 1.
TINYINT(size) A very small integer. Signed range is from -128 to 127. Unsigned range is from 0 to 255. The size parameter specifies the maximum display width (which is 255)
BOOL Zero is considered as false, nonzero values are considered as true.
BOOLEAN Equal to BOOL
SMALLINT(size) A small integer. Signed range is from -32768 to 32767. Unsigned range is from 0 to 65535. The size parameter specifies the maximum display width (which is 255)
MEDIUMINT(size) A medium integer. Signed range is from -8388608 to 8388607. Unsigned range is from 0 to 16777215. The size parameter specifies the maximum display width (which is 255)
INT(size) A medium integer. Signed range is from -2147483648 to 2147483647. Unsigned range is from 0 to 4294967295. The size parameter specifies the maximum display width (which is 255)
INTEGER(size) Equal to INT(size)
BIGINT(size) A large integer. Signed range is from -9223372036854775808 to 9223372036854775807. Unsigned range is from 0 to 18446744073709551615. The size parameter specifies the maximum display width (which is 255)
FLOAT(size, d) A floating point number. The total number of digits is specified in size. The number of digits after the decimal point is specified in the d parameter. This syntax is deprecated in MySQL 8.0.17, and it will be removed in future MySQL versions
FLOAT(p) A floating point number. MySQL uses the p value to determine whether to use FLOAT or DOUBLE for the resulting data type. If p is from 0 to 24, the data type becomes FLOAT(). If p is from 25 to 53, the data type becomes DOUBLE()
DOUBLE(size, d) A normal-size floating point number. The total number of digits is specified in size. The number of digits after the decimal point is specified in the d parameter
DOUBLE PRECISION(size, d)
DECIMAL(size, d) An exact fixed-point number. The total number of digits is specified in size. The number of digits after the decimal point is specified in the d parameter. The maximum number for size is 65. The maximum number for d is 30. The default value for size is 10. The default value for d is 0.
DEC(size, d) Equal to DECIMAL(size,d)
MySQL date and time data types
DATE A date. Format: YYYY-MM-DD. The supported range is from ‘1000-01-01’ to ‘9999-12-31’
DATETIME(fsp) A date and time combination. Format: YYYY-MM-DD hh:mm:ss. The supported range is from ‘1000-01-01 00:00:00’ to ‘9999-12-31 23:59:59’. Adding DEFAULT and ON UPDATE in the column definition to get automatic initialization and updating to the current date and time
TIMESTAMP(fsp) A timestamp.
TIMESTAMP values are stored as the number of seconds since the Unix epoch (‘1970-01-01 00:00:00’ UTC). Format: YYYY-MM-DD hh:mm:ss. The supported range is from ‘1970-01-01 00:00:01’ UTC to ‘2038-01-09 03:14:07’ UTC. Automatic initialization and updating to the current date and time can be specified using DEFAULT
CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP in the column definition
TIME(fsp) A time. Format: hh:mm:ss. The supported range is from ‘-838:59:59’ to ‘838:59:59’
YEAR A year in four-digit format. Values allowed in four-digit format: 1901 to 2155, and 0000.
MySQL 8.0 does not support year in two-digit format.
MySQL String Functions
https://www.w3schools.com/sql/sql_ref_mysql.asp