CS2855-SQL-JDBC
CS2855 Database Revision Notes: SQL & JDBC
1. SQL Basics (SQL Basics.pdf)
Core Components
SELECT Structure:
1
2
3
4
5SELECT [DISTINCT] col1, aggregate(col2)
FROM table
[WHERE condition]
[GROUP BY col]
[HAVING group_condition]String Operations:
||
for concatenationLIKE 'pattern'
with%
(any string) and_
(single char)
Example:
1
2
3SELECT customer_name
FROM customer
WHERE customer_street LIKE '%Main%';Set Operations:
UNION
(duplicates removed)INTERSECT
EXCEPT
(difference)
Example for union:(SELECT name FROM depositor) UNION (SELECT name FROM borrower);
Aggregate Functions:
COUNT(*)
,SUM(col)
,AVG(col)
,MIN/MAX(col)
- Use
DISTINCT
to eliminate duplicates
Example:SELECT AVG(balance) FROM account WHERE branch_name='Perryridge';
Exam Alert (Past Papers)
- Frequently asked to write queries using
GROUP BY
/HAVING
and string matches (e.g., 2023 Q3b/c).
2. Intermediate SQL (6.SQL_intermediate.pptx)
Integrity Constraints
Domain Constraints:
CREATE DOMAIN Dollars NUMERIC(12,2) NOT NULL;Table Constraints:
NOT NULL
:
CREATE TABLE branch (name VARCHAR(15) NOT NULL);UNIQUE
:
UNIQUE (branch_name, customer_name)CHECK
:
CHECK (balance >= 0)- Foreign Keys:
FOREIGN KEY (acc_number) REFERENCES account
ON DELETE CASCADE
Assertions (Rarely Tested):
CREATE ASSERTION balance_limit
CHECK (NOT EXISTS (SELECT * FROM account WHERE balance < 0));
Exam Alert
- Constraints like primary/foreign keys frequently appear in schema design questions (2024 Q2: Convert ER to tables with constraints).
3. JDBC (7.SQL_JDBC Part 1 & 2)
Core Steps
Connect to Database:
String url = "jdbc:postgresql://teachdb.cs.rhul.ac.uk/CS2855%2Fuser123";
Connection conn = DriverManager.getConnection(url, "user123", "password");
Execute Queries:
- Use
PreparedStatement
to prevent SQL injection:1
2
3
4String sql = "SELECT * FROM students WHERE name = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, inputName);
ResultSet rs = stmt.executeQuery();
- Use
Process Results:
1 |
|
- Close Resources:
Always closeResultSet
,Statement
,Connection
infinally
block.
Maven Setup (pom.xml)
Exam Alert
- 2022 Q4: JDBC code snippets to identify resource leaks.
- 2023 Q4: Use
PreparedStatement
for secure inserts.
4. Past Paper Analysis
Recurring Question Types
ER to Relational Model (2022 Q1, 2023 Q1):
- Map entities to tables, relationships to foreign keys.
- Handle weak entities (use parent’s PK + partial key).
Relational Algebra (2024 Q2a):
- Write expressions for queries (σ for selection, ⨝ for joins).
Example: Find customers with loans:
π_{customer_name, loan_no, amount} (borrower ⨝ loan)
- Write expressions for queries (σ for selection, ⨝ for joins).
SQL Optimization (2023 Q3d):
- Avoid correlated subqueries; use JOINs.
- Use aggregate subqueries in HAVING.
BCNF/3NF Decomposition (2022 Q5):
- Check all FDs left-side are superkeys for BCNF.
- For 3NF, allow transitive dependencies if RHS is prime.
Transaction Schedules (2023 Q5):
- Test conflict serializability via precedence graphs.
- Check if schedules are view serializable.
5. Exam Tips
Time Management: Spend 30 mins on ER/relational algebra, 45 mins on SQL/JDBC.
ACID Definitions (2024 Q4):
- Atomicity: All or nothing.
- Consistency: Valid state transitions.
- Isolation: Concurrent runs mimic serial.
- Durability: Committed survives crashes.
Normalization Steps:
- Check for BCNF violations (non-superkey determinants).
- Decompose using violating FD: R1(αβ), R2(R-β).
- Verify lossless join: α must be key in R1/R2.