Basics of SQL Flashcards
(36 cards)
What does SQL stand for?
Structured Query Language (SQL) - a language for managing data in a RDMS - Relational Database Management System (where related data is grouped together in tables).
What is RDBMS?
RDMS - Relational Database Management System = where related data is grouped together in tables which in turn can be linked by relationships.
What is a PRIMARY KEY and FOREIGN KEY?
Primary key = usually a table column header/field/unique identifier for records stored within the table.
Foreign key is a field (table column header) or collection of fields that refer to the headers/primary key of another table.
Is SQL case sensitive?
No, SQLanguage is not case-sensitive however it is a BEST PRACTICE to always use capitals for KEYWORDS.
How to write a string inside SQL?
Use ‘string’ SINGLE QUOTES.
How would you destroy a database called Flights in SQL?
DROP DATABASE Flights;
How would you create a database called SimpsonsDB?
CREATE DATABASE SimpsonsDB;
How to start working in a database called SevenHells?/Change the active DB to it.
USE SevenHells;
How would you make a table called prime?
CREATE TABLE prime;
Create a SQL table called Animals with a column called Dogs with a INT type of data.
CREATE TABLE Animals (
Dogs INT
);
Add another column/field to the Animals table called CatNames that takes in string values with a maximum string length of 100 chars.
ALTER TABLE Animals
ADD CatNames VARCHAR(100);
Does:
ALTER
TABLE
tableName;
Run the same as:
ALTER TABLE tableName;
Yes! These statements run identically even though they are split over multiples lines as the CLI interpreter reads everything as continuous blocks until there is a ;
Why should every SQL table you make have an ID column/field?
So that if there is the same data on different rows (e.g. two users enter name: Mike, age: 20) then you can tell them apart with the Unique IDentifier column value.
How would you create a table called Painting with a foreign key with a field of: colour linking to a table called anomalies with a field/column of extra?
CREATE TABLE Painting (
colour VARCHAR(20),
FOREIGN KEY (colour)
REFERENCES anomalies(extra)
);
How to add a column called columnName intoto a table called Painting. Add in the values to the column: purple, yellow, red
ALTER TABLE Painting
ADD columnName;
INSERT INTO Painting (columnName)
VALUES (‘purple’), (‘yellow’), (red’);
How to add a value of “X” into a table called Letters in an already added field/column headed LastLetters?
INSERT INTO Letters (LastLetters)
VALUES (‘X’);
What is the INSERT INTO keyword used for? What keyword is it most commonly used with?
To INSERT INTO specific data/rows into a table in combination with the VALUES keyword.
E.g. INSERT INTO TableName (Col/FieldName)
VALUES (1), (2,), (3)
INSERT data INTO the DB. each value/bit of data/ROW needs to be in ()
What is the ADD keyword used for?
To modify the structure of an existing table (e.g. ADD columnName).
ADD Columns
What two steps to add the names of Steve and Frank to a table called people in a new column called Men?
Step 1:
ALTER people
ADD Men VARCHAR (20);
Step 2:
INSERT INTO people (Men)
VALUES (Steve), (Frank);
What does the SELECT keyword do?
SELECT - retrieves data from a table in a DB.
E.g. SELECT * FROM MyFirstTable; - selects all columns from the MyFirstTable table.
What is * in SQL?
- = wildcard/any/all columns.
How to SELECT a maximum of 50 numbers from a column called/headed Nums inside a table called Math?
SELECT Nums FROM Math LIMIT 50
LIMIT = limits the SELECT query to retrieve only the first X results that meet the queries condition (in this case being in the column Nums). The ORDER BY clause can be added below to sort the condition by columnName ASC (ASCending) or DESC (DESCending)
What KEYWORD is generally used to create a unique value ID column in SQL?
AUTO_INCREMENT keyword is often used to create a unique (auto-incrementing) id INT column PRIMARY KEY:
CREATE TABLE MyTable (
id INT AUTO_INCREMENT PRIMARY KEY); – The ‘id’
What does the AS keyword do?
Assigns an AliaS to a column or table for the duration of a query.