SQL Basic Flashcards
(36 cards)
SQL and MySQL
SQL Structured Query Language
MySQL Database management sysytem-DBMS, other are PostgreSQL, SQLite, Oracle, There are slight differences in the syntax. Implement SQL standard
changing/selecting database
Use Database_name
to check what db i am using
Select Database_Name()
To drop a database:
DROP DATABASE <database-name>;</database-name>
To use a database:
USE <database-name>;</database-name>
To list available databases:
show databases;
The general command for creating a database:
CREATE DATABASE <database_name>;</database_name>
CREATE DATABASE soap_store;
2 main data types
numeric and string
show tables;
show columns from Table1;
DESC Table1;
Insert a cat into a table
INSERT INTO cats (name, age)
VALUES (‘Blue Steele’, 5);
Primary key
means unique,
auto-increment
cat_id INT PRIMARY KEY
at the begging or
PRIMARY KEY (cat_id)
at the end
Update example
UPDATE cats SET breed=’Shorthair’ WHERE breed=’Tabby’;
using SET and where
What to use before Deleting/Updating
Select before UPDATE/ DELETE!
String functions
- concat
- substring
- replace
- reverse
- char_length
- upper&lower
using select
SELECT CONCAT(author_fname,’ ‘, author_lname) AS author_name FROM books;
SELECT SUBSTRING(‘Hello World’, 1, 4);
Refining selections
distinc- only unique values -> Select Distinct
SELECT DISTINCT author_lname FROM books;
Order by example
SELECT author_lname FROM books ORDER BY author_lname;
ASCENDING By Default
Limit example
SELECT title, released_year FROM books
ORDER BY released_year DESC LIMIT 5;
LIKE
Better searching
WHERE author_fname LIKE ‘%da%’
WILDCARDS
Aggregate functions
Count
Group By
Min/Max
Sum
Avg
Count example
SELECT COUNT(*) FROM books;
SELECT COUNT(author_fname) FROM books;
SELECT COUNT(DISTINCT author_fname) FROM books;
SELECT COUNT(*) FROM books
WHERE title LIKE ‘%the%’;
GROUP BY
GROUP BY summarizes or aggregates identical data into single rows
Data types- String data types
char- fixed in size length, for text of similar length
varchar- text of different sizes
nvarchar- 1 character takes 2 bytes, max length is 50
Data types- numeric
tinyint- from -127 to 128
smallint- from -32k to +32k
mediumint- from -8 milion to + 8 milion
int- from -2b to +2b
Data types- decimal
decimal- (total number of digits (maxium/ up to), digits after decimal)
float- not as precise as double
double- very precise, dobule as precise