JAVA Development: Databases - SQL
JAVA Development: Databases - SQL
JAVA Development: Databases - SQL
Databases - SQL
Databases - What is a database?
● One-to-one: 1 PK - 1 FK
● One-to-many: 1 PK - n FK
● Many-to-many: n PK - m FK
Databases
Primary Key Constraint: a set of one or more columns that must be unique
● External: e.g CNP, email
● Surrogate key: generated ID, e.g automatically incremented number
There can be only one primary key (only one set of columns)
More Examples:
● ID: numeric: 0, 1, 2, 3, …
● ID: GUID: ebec3303-3705-4c3b-8a3a-452519c702c9
● Email: “[email protected]”, “[email protected]”
● [First Name + Last Name]: [“Jim”, “Jones”], [“Some”, “User”]
Databases
Example:
● Order.customerId –> references: Customer.id
● Song.albumId –> references: Album.id
● Song.artistId –> references: Artist.id
Databases
● CHECK constraint:
○ ‘age’ must be greater than 21
○ length of a credit card number must be between 12 and 15 characters
Databases
Types of statements:
● SELECT: fetch some data from the database
● INSERT: create one or more rows
● UPDATE: update the values in one or more existing rows
● DELETE: delete some rows
● others: creating/updated tables, creating/updating permissions, etc.
Databases - selecting data
SELECT column1, column2, ... FROM table_name <WHERE cond1 AND/OR cond2>;
More info:
● https://technet.microsoft.com/en-us/library/bb264565(v=sql.90).aspx
● https://en.wikipedia.org/wiki/Select_(SQL)
Databases - selecting data
More info:
● https://en.wikipedia.org/wiki/Insert_(SQL)
Databases - updating data
UPDATE users
SET emailProvider = 'GMAIL'
WHERE email LIKE '%gmail.com'
UPDATE song_plays
SET played = played+1, lastPlayed = '2018-02-05'
WHERE song_id = 17583 AND user_id = 48971
More info:
● https://en.wikipedia.org/wiki/Insert_(SQL)
Databases - deleting data
● Visual Tools:
○ http://squirrel-sql.sourceforge.net/ , https://sqldbm.com/
○ many IDEs have one built in(IDEA Enterprise, Visual Studio)
Databases - primary keys
Method 1:
CREATE TABLE table_name
(
column1 datatype null/not null,
column2 datatype null/not null, ...
CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ... column_n)
);
Databases - creating primary keys
Method 1:
CREATE TABLE table_name
(
column1 datatype null/not null,
column2 datatype null/not null, ...
CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ... column_n)
);
Example:
CREATE TABLE supplier
(
supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id, supplier_name)
);
Databases - creating primary keys
Method 2:
ALTER TABLE table_name
ADD CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ... column_n);
Databases - creating primary keys
Method 2:
ALTER TABLE table_name
ADD CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ... column_n);
In this example, we've created a primary key on the existing supplier table called supplier_pk.
It consists of the field called supplier_id.
Databases - creating primary keys
Method 2:
ALTER TABLE table_name
ADD CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ... column_n);
In this example, we've created a primary key on the existing supplier table called supplier_pk.
It consists of the field called supplier_id.
We could also create a primary key with more than one field as in the example below:
As with primary keys, there are two ways of creating foreign keys:
Databases - foreign keys
As with primary keys, there are two ways of creating foreign keys:
● when executing a CREATE TABLE statement
Databases - foreign keys
As with primary keys, there are two ways of creating foreign keys:
● when executing a CREATE TABLE statement
● with an ALTER TABLE statement
Databases - create foreign keys
Example:
● we use JOINS to combine rows from two or more tables, based on related
columns
○ INNER JOIN: Returns records that have matching values in both tables
○ LEFT JOIN: Return all records from the left table, and the matched records
from the right table (null is returned from the right side if there is no match)
○ RIGHT JOIN: Return all records from the right table, and the matched
records from the left table (null is returned from the left side if there is no
math)
○ FULL JOIN: Return all records when there is a match in either left or right
table
Databases - SQL Joins
Databases - SQL Joins
Example:
We saw how can we delete records from the database but what about other
entities?
● delete a table
○ DROP TABLE table_name; - yes, it’s that simple…
Databases - Deleting… stuff
We saw how can we delete records from the database but what about other
entities?
● delete a table
○ DROP TABLE table_name; - yes, it’s that simple… to ruin everything.
Databases - Deleting… stuff
We saw how can we delete records from the database but what about other
entities?
● delete a table
○ DROP TABLE table_name; - yes, it’s that simple… to ruin everything.