SQL Database Flashcards

1
Q

create a new SQL database

A

CREATE DATABASE databaseName;

NOTE: must have admin privilege

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

SHOW DATABASES;

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

how can we drop an existing SQL database

A

DROP DATABASE databaseName;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

in SQL server you can backup a database

A

BACKUP DATABASE databaseName
TO DISK = ‘filepath’;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is a differential backup?

A

backs up only parts of database that have changed since last full database backup

BACKUP DATABASE databaseName
TO DISK = ‘filepath’
WITH DIFFERENTIAL;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

create a table

A

CREATE TABLE tableName (
column1 dataType,
column2 dataType,

);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

create table use existing table

A

CREATE TABLE new_table_name AS
SELECT column1, column2, …
FROM existing_table_name
WHERE …;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

drop a table

A

DROP TABLE table_name;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what statement deletes the data inside a table but not the table itself

A

TRUNCATE TABLE table_name;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

A

ALTER TABLE table_name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

ALTER TABLE - ADD Column

A

ALTER TABLE table_name
ADD column_name datatype;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

ALTER TABLE - DROP COLUMN

A

ALTER TABLE table_name
DROP COLUMN column_name;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

ALTER TABLE - RENAME COLUMN

A

ALTER TABLE table_name
RENAME COLUMN old_name to new_name;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

ALTER TABLE - ALTER/MODIFY DATATYPE with MySQL example

A

ALTER TABLE table_name
MODIFY COLUMN column_name datatype;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what are SQL constraints?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

common constraints used in SQL

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

what is a foreign key

A

a field or collection of fields in one table that refers to the primary key in another table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

foreign key syntax

A

FOREIGN KEY (PersonID) References Persons(PersonID)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

CHECK constraint example

A

CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

default constraint example

A

CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT ‘Sandnes’
);

21
Q

what are indexes used for?

A

to retrieve data from database more quickly than otherwise; the users cannot see the indexes, they are just used to speed up searches/queries

22
Q

create index syntax - duplicate values allowed

A

CREATE INDEX index_name
ON table_name (column1, column2, …);

23
Q

create index syntax - duplicate values NOT allowed

A

CREATE UNIQUE IDEX index_name
ON table_name (column1, column2, …);

24
Q

drop index statement - MySQL

A

ALTER TABLE table_name
DROP INDEX index_name;

25
Q

what does Auto-increment do?

A

allows a unique number to be generated automatically when a new record is inserted into a table

26
Q

auto-increment example

A

CREATE TABLE Persons (
Personid int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (Personid)
);

27
Q

MySQL date data types

A

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

28
Q

what is a view in SQL?

A

a virtual table based on result-set of a SQL statement

29
Q

what does a view contain in SQL?

A

contains rows and columns just like a real table

30
Q

create view syntax

A

CREATE VIEW view_name AS
SELECT column1, column2, …
FROM table_name
WHERE condition;

31
Q

does the view shop up-to-date data?

A

YES, always! The database engine recreates the view, every time a user queries it.

32
Q

create view example

A

CREATE VIEW [Brazil Customers] AS
SELECT CustomerName, ContactName
FROM Customers
WHERE Country = ‘Brazil’;

33
Q

sql updating a view

A

CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, …
FROM table_name
WHERE condition;

34
Q

sql dropping a view

A

DROP VIEW view_name;

ex.
DROP VIEW [Brazil Customers];

35
Q

what is a SQL injection?

A

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

36
Q

when does SQL in web pages occur?

A

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

37
Q

sql injection example

A

1 = 1 is always True

SELECT * FROM Users WHERE UserId = 105 OR 1=1;

38
Q

another SQL injection that is always True

A

””=””

SELECT * FROM Users WHERE Name =”” or “”=”” AND Pass =”” or “”=””

39
Q

another way to do sql injection is with batched sql statements

A

SELECT * FROM Users; DROP TABLE Suppliers

40
Q

how to protect website from SQL injection?

A

use SQL parameters

these are values that are added to a SQL query at execution time, in a controlled manner

41
Q

sql hosting

A

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

42
Q

data types of a column defines what?

A

value the column can hold and how SQL can interact with the stored data

43
Q

MySQL string data types

A

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

44
Q

MySQL numeric data types

A

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)

45
Q

MySQL date and time data types

A

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.

46
Q

MySQL String Functions

A

https://www.w3schools.com/sql/sql_ref_mysql.asp