0% found this document useful (0 votes)
180 views

SQL Coursera Course

The document describes how to create, read, update, and delete (CRUD) data from an SQL database by following the CRUD operations. It provides examples of how to create a table, insert values, delete entries, update entries, and read/select entries from the table. It also covers different data types that can be used in SQL like strings, text, binary types, blobs, integers, floats, and dates. Finally, it discusses database keys, indexes, relational database design, and joining tables in one-to-one and many-to-many relationships.

Uploaded by

Nichola Gouveia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
180 views

SQL Coursera Course

The document describes how to create, read, update, and delete (CRUD) data from an SQL database by following the CRUD operations. It provides examples of how to create a table, insert values, delete entries, update entries, and read/select entries from the table. It also covers different data types that can be used in SQL like strings, text, binary types, blobs, integers, floats, and dates. Finally, it discusses database keys, indexes, relational database design, and joining tables in one-to-one and many-to-many relationships.

Uploaded by

Nichola Gouveia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

CREATING AN SQL DATABASE following the C.R.U.

D (CREATE, READ, UPDATE, DELETE)

1. Create the table within the database.

CREATE TABLE Users (

name VARCHAR(128),

email VARCHAR(128)

);

2. INSERT values into the columns of the table.

INSERT INTO Users (name, email) VALUES ('Greg Monroe','[email protected]');


INSERT INTO Users (name, email) VALUES ('Simone Lovey','[email protected]');
INSERT INTO Users (name, email) VALUES ('Ken Chris','[email protected]');
INSERT INTO Users (name, email) VALUES ('Timothy Crawford','[email protected]');
INSERT INTO Users (name, email) VALUES ('Tiffany Price','[email protected]');
INSERT INTO Users (name, email) VALUES ('Jenny Whitehouse','[email protected]');

3. DELETE an entry that was INSERTED into the table.

DELETE FROM User WHERE email = '[email protected]';

4. Update an entry that was made into the table

UPDATE Users SET name = 'Krissy' WHERE email = '[email protected]'

5. READ / SELECT an entry from the table.

SELECT * FROM Users WHERE email = '[email protected]'; (To see one record in the table)

SELECT * FROM Users; (To view the entire table)

SELECT * FROM Users ORDER BY email; (SORT IN ALPHABETICAL ORDER)

SELECT COUNT (*) FROM Users; (COUNT ROWS WITH SELECT)

SELECT COUNT (*) FROM Users WHERE email = '[email protected]'; (COUNT ROWS WITH
SELECT)

SELECT * FROM Users WHERE name LIKE ‘%e%’; (This means show me all the entries that
have ‘e’ in their names.)
DATA TYPES
STRING FIELD
CHAR – ALLOCATES THE ENTIRE SPACE (FASTER FOR SMALLER STRINGERS WHERE THE
LENGTH IS KNOWN)

VARCHAR – ALLOCATES A VARIABLE AMOUNT OF SPACE DEPENDING ON THE DATA


LENGTH (LESS SPACE)

TEXT FIELD
Have a CHARACTER SET - If you are inserting a paragraph, a blog or a facebook post, etc,
then you would use one of the following:
TINYTEXT – UP TO 255 CHARACTERS
TEXT – UP TO 65K CHARACTERS
MEDIUM TEXT – UP TO 16M
LONGTEXT – UP TO 4GIGABYTES

BINARY TYPES
Characters = 8-32 bits of information depending on the character set.
BYTE (n) = UP TO 255 BYTES
VARBINARY (n) = UP TO 65K BYTES

- Small data Images

BINARY LARGE OBJECTS (BLOB)

Large raw data, files, images, word documents, PDFS, movies, etc
No translation, character set or indexing

TINYBLOB – UP TO 255
BLOB – UP TO 65K
MEDIUMBLOB – UP TO 16M
LONGBLOB – UP TO 4G

INTEGER NUMBERS

They are very efficient, take little storage, and are very easy to process because CPUs can
usually compare them with a single instruction. They are used for comparing, sorting and
indexing.

TINYINT – (-128,128)

SMALLINT – (-32768, 32768)

INT OR INTEGER – (2BILLION)

BIGINT – (10**18 ish)

FLOAT

They can represent a wide range of values, but accuracy is limited. They measure temperature,
etc.

Float (32 bit) = 10**38 with seven digits of accuracy

Double (64 bit) = 10**308 with fourteen digits of accuracy

DATES

TIMESTAMP – This is only limited to time between the years 1970 and 2037. ‘YYYY-MM-DD HH:MM:SS’

DATETIME – This can facilitate any time from any year. ‘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

Often when we make multiple tables and need to join them, we use an integer primary key for each
row so that we can efficiently add a reference to a row in some other table as a foreign key.

CREATE TABLE Users (

user_id INT UNSIGNED NOT NULL AUTO-INCREMENT,

name VARCHAR(128),

email VARCHAR(128),

PRIMARY KEY (user_id),

INDEX (email)

);

RELATIONAL DATABASE DESIGN


1. Do not replicate vertical data. Point to it as a single entry.
JOINING TABLES

ONE TO ONE RELATIONSHIP

CREATE DATABASE Music DEFAULT CHARACTER SET utf8;

CREATE TABLE Artist (


artist_id INTEGER NOT NULL AUTO_INCREMENT,
name VARCHAR(255),
PRIMARY KEY(artist_id)
)
ENGINE = InnovDB;

CREATE TABLE Album(


album_id INTERGER NOT NULL AUTO_INCREMENT,
title VARCHAR(255),
artist_id INTEGER,
PRIMARY KEY(album_id),
INDEX USING BTREE(title),

CONSTRAINT FOREIGN KEY (artist_id)


REFERENCE Artist (artist_id)
ON DELETE CASCADE ON UPDATE CASCADE
)
ENGINE = InnoDB;

CREATE TABLE Genre (


genre_id INTEGER NOT NULL AUTO_INCREMENT,
name VARCHAR (255),
PRIMARY KEY(genre_id)
)
ENGINE = InnoDB;

CREATE TABLE Track(


track_id INTEGER NOT NULL AUTO_INCREMENT,
title VARCHAR(255),
len INTEGER,
rating INTEGER,
count INTEGER,
album_id INTEGER,
genre_id INTEGER,

PRIMARY KEY (track_id),


INDEX USING BTREE(title),

CONTRAINT 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 = InnovDB;

INSERT INTO Artist (name) VALUES ('Nicki Minaj');


INSERT INTO Artist (name) VALUES ('Beyonce');
INSERT INTO Artist (name) VALUES ('Rihanna');

INSERT INTO Album (title, artist_id) VALUES ('Lemonade', 2);

INSERT INTO Album (title, artist_id) VALUES ('The Lion King', 2);

INSERT INTO Album (title, artist_id) VALUES ('Loud', 3);

INSERT INTO Album (title, artist_id) VALUES ('Anti', 3);

INSERT INTO Album (title, artist_id) VALUES ('Queen', 1);

INSERT INTO Genre (name) VALUES ('R&B');


INSERT INTO Genre (name) VALUES ('Hip Hop');

INSERT INTO Track (title, rating, len, count, album_id, genre_id) VALUES ('Hold up', 10, 230, 0, 1,
1);
INSERT INTO Track (title, rating, len, count, album_id, genre_id) VALUES ('Sorry', 10, 300, 0, 1, 1);

INSERT INTO Track (title, rating, len, count, album_id, genre_id) VALUES ('All Night', 10, 240, 0,
1, 1);

INSERT INTO Track (title, rating, len, count, album_id, genre_id) VALUES ('Transformation', 10,
310, 0, 1, 1);

INSERT INTO Track (title, rating, len, count, album_id, genre_id) VALUES ('Brown Skin Girl', 10,
230, 0, 2, 1);

INSERT INTO Track (title, rating, len, count, album_id, genre_id) VALUES ('Spirit', 10, 230, 0, 2, 1);

INSERT INTO Track (title, rating, len, count, album_id, genre_id) VALUES ('Return to Pride Rock',
10, 230, 0, 2, 1);

INSERT INTO Track (title, rating, len, count, album_id, genre_id) VALUES ('The King', 10, 230, 0,
2, 1);

INSERT INTO Track (title, rating, len, count, album_id, genre_id) VALUES ('What's my name', 10,
230, 0, 3, 2);

INSERT INTO Track (title, rating, len, count, album_id, genre_id) VALUES ('Skin', 10, 230, 0, 3, 2);

INSERT INTO Track (title, rating, len, count, album_id, genre_id) VALUES ('California King bed',
10, 230, 0, 3, 2);

INSERT INTO Track (title, rating, len, count, album_id, genre_id) VALUES ('Only girl', 10, 230, 0, 3,
2);

INSERT INTO Track (title, rating, len, count, album_id, genre_id) VALUES ('Kiss it better', 10, 230,
0, 3, 1);

INSERT INTO Track (title, rating, len, count, album_id, genre_id) VALUES ('James Joint', 10, 230,
0, 3, 1);

INSERT INTO Track (title, rating, len, count, album_id, genre_id) VALUES ('Consideration', 10,
230, 0, 3, 1);

INSERT INTO Track (title, rating, len, count, album_id, genre_id) VALUES ('Higher', 10, 230, 0, 3,
1);
INSERT INTO Track (title, rating, len, count, album_id, genre_id) VALUES ('High School', 10, 230,
0, 1, 2);

INSERT INTO Track (title, rating, len, count, album_id, genre_id) VALUES ('Moment for Life', 10,
230, 0, 1, 2);

INSERT INTO Track (title, rating, len, count, album_id, genre_id) VALUES ('Roman Holiday', 10,
230, 0, 1, 2);

INSERT INTO Track (title, rating, len, count, album_id, genre_id) VALUES ('Knock Out', 10, 230, 0,
1, 2);

select DISTINCT Artist.name, 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 where Artist.name = 'Beyonce'

MANY TO MANY RELATIONSHIP


DROP TABLE IF EXISTS Member;
DROP TABLE IF EXISTS 'User';
DROP TABLE IF EXISTS Course;

CREATE TABLE 'User' (


user_id INTEGER NOT NULL AUTO_INCREMENT,
name VARCHAR (128) UNIQUE,
PRIMARY KEY (user_id)
)Engine = InnoDB Character set=utf8;

CREATE TABLE Course (


course_id INTEGER NOT NULL AUTO_INCREMENT,
title VARCHAR (128) UNIQUE,
PRIMARY KEY (course_id)
)Engine = InnoDB Character set=utf8;

CREATE TABLE Member(


user_id INTEGER,
course_id INTEGER,
role INTEGER,

CONSTRAINT ON FOREIGN KEY (user_id) REFERENCES ‘User’ (user_id)


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

PRIMARY KEY (user_id, course_id)

)ENGINE = InnoDB Character set=utf8;

You might also like