Introduction to Structured Query Language (SQL)

·

11 min read

Structured Query Language is a computer language that we use to interact with a relational database. Data is in tabular form in this.

Install MAMP XAMPP OR LAMP It is all up to you :

Installation -

#login as root user
su
#setup apache or httpd web server
sudo dnf install httpd -y
#to enble apache service
systemctl enable httpd.service
#to start apache service
systemctl start httpd.service

Now permit the HTTPS and HTTP service from the firewell. This is necessary if your firewell is blocking access to these services:

firewall-cmd --permanent --add-service=httpd
#above cmd may/may_not be worked for you
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
#this is about to installation of apache

Installing MariaDB -

It is a fork of the original MySQL database. Am using this database in Fedora i3.

sudo dnf install mariadb-server -y
#after this we can enable and start the MariaDB services
sudo systemctl enable mariadb
sudo systemctl start mariadb
sudo systemctl status mariadb
#to start the secure setup of mariaDB server
mysql_secure_installation
#it asks some questions 
#Login to the MariaDB Database
sudo mysql
#we denied the remote access for the root login in MariaDB we need to 
#generate new user for remote access.
CREATE USER 'myuser'a'localhost'IDENTIFIED BY '123'
GRANT ALL ON ** TO 'myuser'a'localhost'
flush privileges
#that's it.

SQL - CREATE/INSERT data, -READ/SELECT, REMOVE, UPDATE DATA, DELETE DATA

DATABASE Contains one or more tables

CREATING DATABASE -

SHOW DATABASES;
CREATE DATABASE People;
USE People;
CREATE TABLE Users (
 name VARCHAR(50),
 email VARCHAR(50)
);
DESCRIBE Users; 
INSERT INTO Users VALUES ("Abhi","abhi@edu.mail");
INSERT INTO Users VALUES ("Kd","kd@edu.mail");
INSERT INTO Users VALUES ("Sahil","sahil@edu.mail");
SELECT * FROM Users;

SHOW DATABASES - Show all the databases present in the machine.

CREATE DATABASE People - Creating the People named database.

USE People - By this, we getting entered into the People database.

CREATE TABLE Users( name VARCHAR(50),email VARCHAR(50)) - Called METADATA

DESCRIBE Users - used to retrieve the metadata about the table

INSERT - Statement inserts a row into a table

INTO - insert into table

VALUES - serial wise - name then email

SELECT FROM Users - means retrieve all(\) the records or a subset of the records with a WHERE clause.

SELECT * FROM Users WHERE email = "abhi@edu.mail" ;

SORTING WITH ORDER BY -

You can add an ORDER BY clause to SELECT statements to get the results sorted in ascending or descending order

SELECT * FROM Users ORDER BY  email ;
SELECT * FROM Users ORDER BY  email DESC;

LIKE OPERATOR-

You can do wildcard matching in a WHERE clause using the LIKE operator

SELECT * FROM Users WHERE name LIKE "%s%" ;

COUNTING ROWS with SELECT -

You can request to receive the count of the rows that would be retrieved instead of rows.

SELECT COUNT(*) FROM Users;
SELECT COUNT(*) FROM Users WHERE email="abhi@edu.mail";

DELETE/UPDATE/RETRIEVE -

Delete a row in a table based on a selection criteria

DELETE FROM Users WHERE email = "sahil@edu.mail" ;
UPDATE Users SET name = "Abhishek Verma" WHERE email = "abhi@edu.mail" ;

DELETE FROM Users WHERE email = "kd@edu.mail" - By this command entire row has to be deleted

SET - It allows you to modify the values of one or more columns for the records that match a certain condition.

DATA TYPES IN SQL -

String Fields -

  • CHAR allocates the entire space (faster for small strings where length is known)

  • VARCHAR allocates a variable amount of space depending on the data length(less space)

Binary Large Object (BLOB) -

  • Large raw data, files, images, Word documents, PDFs, movies, etc.

  • No translation, indexing or character set

    -TINYBLOB(n) -up to 255

    -BLOB(n) up to 65K

    -MEDIUMBLOB(n) -up to 16M

    -LONGBLOB(n) - up to 4G

Dates -

  • TIMESTAMP -' YYYY-MM-DD HH:MM:SS '(1971,2037)

  • DATETIME - 'YYYY-MM-DD HH:MM:SS'

  • DATE -'YYYY-MM-DD'

  • TIME -'HH:MM:SS'

  • Built-in MySQL function NOW()

DATABASE KEYS AND INDEXES -

AUTO_INCREMENT

First, drop the previous table. Then create a new table.

DROP TABLE Users;
CREATE TABLE Users (
user_id INT  UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(128),
email VARCHAR(128),
PRIMARY KEY (user_id),
INDEX(email)
);
Describe Users;

AUTO_INCREMENT - means auto-indexing in increasing order.

UNSIGNED - means only positive value can be entered

NOT NULL - this means you cannot leave the particular column.

PRIMARY KEY - used to establish relationships between tables.

INDEX - used to speed up the retrieval of data from a table. Without indexes, the database would need to scan the entire table to find the desired rows, which can be very slow for large tables.

INSERT INTO Users (name, email) VALUES ('Abhi','abhi@cu.edu');
INSERT INTO Users (name, email) VALUES ('Rohit','rohit@cu.edu');
INSERT INTO Users (name, email) VALUES ('Mohit','mohit@cu.edu');
INSERT INTO Users (name, email) VALUES ('Sahil','sahil@cu.edu');
INSERT INTO Users (name, email) VALUES ('Kd','kd@cu.edu');

To check the database

SELECT * FROM Users ;

MySQL Functions -

Many Operations in MySQL need to use the built-in function(like NOW() for dates).

MySQL Index Types -

  • PRIMARY KEY - Very little space, exact match, requires no duplicates, extremely fast for integer fields.

  • INDEX - Good for individual row lookup and sorting /grouping results - works best with exact matches or prefix lookups - can suggest HASH or BTREE

ASSIGNMENT 1

CREATE TABLE Ages (
name VARCHAR(128),
age INTEGER
);

Now make sure the table is empty

DELETE FROM Ages;
INSERT INTO Ages (name ,age) VALUES ('Nicola',28);
INSERT INTO Ages (name ,age) VALUES ('Kasja',29);
INSERT INTO Ages (name ,age) VALUES ('Tegan',19);
INSERT INTO Ages (name ,age) VALUES ('Tayo',15);
INSERT INTO Ages (name ,age) VALUES ('Amilie',25);
INSERT INTO Ages (name ,age) VALUES ('Darien',30);

SELECT sha1(CONCAT(name,age)) AS X FROM Ages ORDER BY X;

Relational Database Design -

Database Normalization (3NF) -

  • Do not replicate data. Instead, reference data. Point at data.

  • Use INTEGER for Keys and for references.

  • Add a special key column to each table, which you will refer to.

Integer Reference Pattern -

We use integer columns in one table to reference (or look up) rows in another table.

Three Kinds of Keys -

  • Primary Key - generally an integer auto-increment field

  • Logical Key - ensures that each row is uniquely identifiable

  • Foreign Key - generally an integer key pointing to a row in another table

Primary Key Rule -

  • Never use your logical key as the primary key.

  • Logical keys can and do change, albeit slowly.

  • Relationships that are based on matching string fields are less efficient than integers.

Foreign Keys -

  • A Foreign Key is when a table has a column containing a key that points to the Primary Key of another table.

  • When all primary keys are integers, then all foreign keys are integers. This is a good practice.

Creating our Music Database-

CREATE DATABASE Music;
USE Music;
---table 1
CREATE TABLE Artist (
artist_id INTEGER NOT NULL AUTO_INCREMENT,
name VARCHAR(21),
PRIMARY KEY(artist_id)
) ENGINE = InnoDB;
---table 2
CREATE TABLE Album (
album_id INTEGER NOT NULL AUTO_INCREMENT,
title VARCHAR(21),
artist_id INTEGER,

PRIMARY KEY (album_id),
INDEX USING BTREE (title),

CONSTRAINT FOREIGN KEY (artist_id)
REFERENCES Artist (artist_id)
ON  DELETE CASCADE ON UPDATE CASCADE 
) ENGINE = InnoDB;
---table 3
CREATE TABLE Genre (
genre_id INTEGER NOT NULL AUTO_INCREMENT,
name VARCHAR(21),
PRIMARY KEY (genre_id)
) ENGINE = InnoDB;
---table 4
CREATE TABLE Track (
track_id INTEGER NOT NULL AUTO_INCREMENT,
title VARCHAR(21),
len INTEGER,
rating INTEGER,
count INTEGER,
album_id INTEGER,
genre_id INTEGER,

PRIMARY KEY (track_id),
INDEX USING BTREE (title),

CONSTRAINT FOREIGN KEY (album_id)
REFERENCES Album (album_id)
ON  DELETE CASCADE ON UPDATE CASCADE,

CONSTRAINT FOREIGN KEY (genre_id)
REFERENCES Genre (genre_id)
ON  DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB;

---inserting data into tables

-- Artist table
INSERT INTO Artist (name) VALUES ("Led Zepplin");
INSERT INTO Artist (name) VALUES ("AC/DC");
---Genre
INSERT INTO Genre (name) VALUES ("Rock");
INSERT INTO Genre (name) VALUES ("Metal");
---Album
INSERT INTO Album (title,artist_id) VALUES ("Who Made Who",2);
INSERT INTO Album (title,artist_id) VALUES ("IV",1);
---Track
INSERT INTO Track (title,rating,len,count,album_id,genre_id) VALUES ("Black Dog",5,297,0,2,1);
INSERT INTO Track (title,rating,len,count,album_id,genre_id) VALUES ("Stairway",5,482,0,2,1);
INSERT INTO Track (title,rating,len,count,album_id,genre_id) VALUES ("About to Rock",5,313,0,1,2);
INSERT INTO Track (title,rating,len,count,album_id,genre_id) VALUES ("Who Made Who",5,207,0,1,2);

UTF8 - It allows you to store and handle text data in multiple languages and character sets without issues. It's widely supported and recommended for internationalization and multilingual applications.

BTREE is a widely used data structure for organizing and optimizing data retrieval in databases. The B-TREE clause is used to specify that a particular index on a table should be created using the B-tree index structure.

INDEX USING BTREE (title) - is not necessary, as B-tree indexing is the default indexing method for the InnoDB storage engine (which is the default).

FOREIGN KEY constraint establishes a relationship between the "Album" table's "artist_id" column and the "Artist" table's "artist_id" primary key. It ensures that data changes in the "Artist" table are propagated to the "Album" table, maintaining data integrity and referential consistency. The CASCADE options specify the actions to take when data in the referenced table changes.

ON DELETE CASCADE - This means that when a referenced row in the parent table is deleted, the corresponding rows in the child table will also be deleted.

ON UPDATE CASCADE - This means that if the primary key value of a referenced row in the parent table is updated, the corresponding foreign key values in the child table will also be updated.

When you use ENGINE = InnoDB in your SQL statements, you're instructing the database to create or alter the table using the InnoDB storage engine.

--- ALTER TABLE YourTableNameHere
--- ENGINE = InnoDB;

Relational Power -

  • Imagine if instead of storing the same information multiple times, we just kept one main copy and made notes about where to find it. This creates a super-efficient system in databases that allows us to find things fast, even when dealing with huge amounts of data.

  • Often when you want some data it comes from several tables linked by these foreign keys.

JOIN Operator -

  • The JOIN operations link across several tables as part of a SELECT operation.

  • You must tell the JOIN how to use the keys that make the connection between the tables using an ON clause.

  • ON clause is used when performing a JOIN operation between two or more tables in a relational database

SELECT Album.title,Artist.name FROM Album JOIN Artist ON Album.artist_id = Artist.artist_id ;
SELECT Album.title,Album.artist_id,Artist.artist_id,Artist.name FROM Album JOIN Artist ON Album.artist_id = Artist.artist_id ;
---without ON clause
SELECT Track.title,Track.genre_id,Genre.genre_id,Genre.name FROM Track JOIN Genre ; 
SELECT Track.title,Genre.name FROM Track JOIN Genre  ON Track.genre_id = Genre.genre_id;

Joining two tables without an ON clause gives all possible combinations of rows from both tables, even if they might not have a real connection.

It can get complex -

SELECT Track.title,Artist.name,Album.title,Genre.name FROM Track JOIN Genre JOIN Album JOIN Artist ON Track.genre_id = Genre.genre_id AND Track.album_id = Album.album_id AND Album.artist_id = Artist.artist_id ;

ON DELETE CASCADE -

DELETE FROM Genre WHERE name = "Metal" ;

ON DELETE Choices -

  • Default/RESTRICT - Don't allow changes that break the constraint.

  • CASCADE - Adjust child rows by removing or updating them to maintain consistency

  • SET NULL - See the foreign key column in the child rows to null

Many-to-Many Relationship -

Start with a Fresh Database -

---table 1
CREATE TABLE Account (
account_id INTEGER NOT NULL AUTO_INCREMENT,
email VARCHAR(21) UNIQUE,
name VARCHAR(21),
PRIMARY KEY (account_id)
) ENGINE = InnoDB CHARACTER SET = utf8;
---TABLE 2

CREATE TABLE Course (
course_id INTEGER NOT NULL AUTO_INCREMENT,
title VARCHAR(21) UNIQUE ,
PRIMARY KEY(course_id)
) ENGINE = InnoDB ;

---table 3
CREATE TABLE Member (
account_id INTEGER ,
course_id INTEGER ,
role INTEGER,
PRIMARY KEY (account_id,course_id),


CONSTRAINT FOREIGN KEY (account_id)
REFERENCES Account (account_id)
ON  DELETE CASCADE ON UPDATE CASCADE,

CONSTRAINT FOREIGN KEY (course_id)
REFERENCES Course (course_id)
ON  DELETE CASCADE ON UPDATE CASCADE


) ENGINE=InnoDB ;
---inserting data into Account table
INSERT INTO Account (name,email) VALUES ("Jane","jane@tsugi.org");
INSERT INTO Account (name,email) VALUES ("Ed","ed@tsugi.org");
INSERT INTO Account (name,email) VALUES ("Sue","sue@tsugi.org");
--- Inserting data into Course table
INSERT INTO Course (title) VALUES ("Python");
INSERT INTO Course (title) VALUES ("SQL");
INSERT INTO Course (title) VALUES ("PHP");
---Insert Memberships
INSERT INTO Member (account_id,course_id,role) VALUES (1,1,1);
INSERT INTO Member (account_id,course_id,role) VALUES (2,1,0);
INSERT INTO Member (account_id,course_id,role) VALUES (3,1,0);
INSERT INTO Member (account_id,course_id,role) VALUES (1,2,0);
INSERT INTO Member (account_id,course_id,role) VALUES (2,2,1);
INSERT INTO Member (account_id,course_id,role) VALUES (2,3,1);
INSERT INTO Member (account_id,course_id,role) VALUES (3,3,0);

UNIQUE - constraint is used to ensure that all values in a specific column (or a combination of columns) are unique across the table. This means that no two rows can have the same value(s) in the column(s) with the "UNIQUE" constraint. It's often used to enforce data integrity and prevent duplicate entries in the table.

SELECT Account.name,Member.role,Course.title
FROM Account JOIN Member JOIN Course
ON Member.account_id = Account.account_id AND
Member.course_id = Course.course_id
ORDER BY Course.title,Member.role DESC,Account.name ;

Complexity Enables Speed -

  • Complexity makes speed possible and allows you to get very fast results as the data size grows.

  • By normalizing the data and linking it with integer keys, the overall amount of data that the relational database must scan is far lower than if the data were simply flattened out.

  • It might seem like a tradeoff - spend some time designing your database so it continues to be fast when your application is a success.

Summary -

  • Relational databases allow us to scale to very large amounts of data.

  • The key is to have one copy of any data element use relations and joins to link the data to multiple places.

  • This greatly reduces the amount of data that must be scanned when doing complex operations across large amounts of data.

  • Database and SQL design is a bit of an art form.

Did you find this article valuable?

Support Abhishek by becoming a sponsor. Any amount is appreciated!