Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # SQL COURSE
- ## What is a databse?
- - A collection of data
- - A structured set of computeerized data with an accessible interface
- ### SQL vs. MySQL
- - SQL is the language used to talk to databases
- - Relational databases use SQL
- - Examples of relational database management systems: MySQL, SQLite, PostgreSQL
- ### Entering MySQL in terminal
- - `mysql -u root -p`
- ### Database creation/removal:
- `CREATE DATABASE <name>;` Creates a new databse
- `DROP DATABASE <name>;` Deletes a database
- `USE DATABASE <name>;` Selects a databse for use (where you'ree working)
- `SELECT database()` Shows which database we are using
- ### Table Creation
- Creating a table:
- ```SQL
- CREATE TABLE tablename
- (
- name VARCHAR(100),
- age INT
- );
- ```
- Creating a table with a default value:
- ```SQL
- CREATE TABLE tablename
- (
- name VARCHAR(100) DEFAULT 'unnamed',
- age INT DEFAULT 999
- );
- ```
- Creating a table without allowing for null:
- ```SQL
- CREATE TABLE tablename
- (
- name VARCHAR(100) NOT NULL DEFAULT 'unnamed',
- age INT NOT NULL DEFAULT 999
- );
- ```
- Adding in AUTO_INCREMENT:
- ```SQL
- CREATE TABLE unique_cats2 (
- cat_id INT NOT NULL AUTO_INCREMENT,
- name VARCHAR(100),
- age INT,
- PRIMARY KEY (cat_id)
- );
- ```
- ### View Tables
- `SHOW TABLES`
- `DESC <tablename>`
- `SHOW COLUMNS FROM <tablename>`
- ## INSERT
- ```SQL
- INSERT INTO table_name
- (column_name, column_name)
- VALUES(value, value),
- (value, value),
- (value, value);
- ```
- ## SQL QUERIES
- ### SELECT *
- Basic query:
- ```SQL
- SELECT *
- FROM customers;
- ```
- Query with where:
- ```SQL
- SELECT *
- FROM customers
- WHERE customerID = 5;
- ```
- Query with return modifier to return (ASC = ascending DESC = Descending):
- ```SQL
- SELECT *
- FROM customers
- ORDER BY customerID DESC;
- ```
- ### Aliases
- ```SQL
- SELECT cat_id AS id, name FROM cats;
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement