0% found this document useful (0 votes)
2 views6 pages

Unit-3

The syllabus covers basic SQL concepts including database schema, data types, table definitions, and DML operations such as insert, delete, and update. It also discusses SQL querying techniques, key constraints, views, and the use of various data types. Additionally, it provides examples of creating tables and performing operations to manage data effectively.
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)
2 views6 pages

Unit-3

The syllabus covers basic SQL concepts including database schema, data types, table definitions, and DML operations such as insert, delete, and update. It also discusses SQL querying techniques, key constraints, views, and the use of various data types. Additionally, it provides examples of creating tables and performing operations to manage data effectively.
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/ 6

SYLLABUS

UNIT III: BASIC SQL:Simple Database schema, data types, table definitions (create, al-
ter), different DML operations (insert, delete, update),Basic SQL querying (select and
project) using where clause, arithmetic & logical operations, SQL functions(Date and
Time, Numeric, String conversion).Creating tables with relationship, implementation of
key and integrity constraints, nested queries, sub queries, grouping, aggregation, ordering,
implementation of different types of joins, view(updatable and non-updatable), relational
set operations.

Simple Database Schema


Definition
A database schema includes various objects such as tables, views, indexes, and procedures.
Each schema is owned by a user and can be used to manage permissions and organize data
logically.
Example
For instance, consider a simple schema for a bookstore:

CREATE SCHEMA bookstore;


CREATE TABLE bookstore.books
(
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author VARCHAR(255) NOT NULL,
publication_date DATE,
price DECIMAL(10, 2) NOT NULL
);

In this example:
 A schema named bookstore is created.
 A table named books is defined within that schema with columns
for id, title, author, publication_date, and price.

components of an SQL schema


The main components of an SQL schema are essential for defining how data is organized and
managed within a database. Here are the key components:
1. Tables
Tables are the fundamental building blocks of a database schema. They consist of rows and columns,
where:
 Rows represent individual records.
 Columns define the attributes or fields of those records.
For example, a books table might include columns for id, title, author, and price.
2. Data Types
Each column in a table has a specific data type that determines what kind of data can be stored in
that column. Common data types include:
 INT: Integer values.
 VARCHAR(n): Variable-length strings.
 DATE: Date values.
 DECIMAL(M,D): Fixed-point numbers with specified precision.
3. Keys
Keys are crucial for maintaining data integrity and establishing relationships between tables:
 Primary Key: A unique identifier for each record in a table (e.g., id in the books table).
 Foreign Key: A field that links to the primary key of another table, establishing a relationship
between the two tables.
4. Constraints
Constraints enforce rules on the data in the tables to maintain integrity. Common constraints
include:
 NOT NULL: Ensures that a column cannot have NULL values.
 UNIQUE: Ensures all values in a column are different.
 CHECK: Validates that values meet specific conditions.
5. Views
Views are virtual tables created by querying data from one or more tables. They simplify complex
queries and provide a way to present data without altering the underlying tables.
6. Indexes
Indexes improve the speed of data retrieval operations on a database table by providing quick access
paths to rows based on indexed columns.
7. Stored Procedures and Functions
Stored procedures are precompiled SQL statements that can be executed as needed, while functions
return values and can be used within SQL expressions.
8. Triggers
Triggers are special types of stored procedures that automatically execute in response to certain
events on a particular table, such as insertions, updates, or deletions.
Data Types in SQL
SQL supports various data types to define the nature of data that can be stored in each column of a
table. These data types can be broadly categorized into several groups:
1. Numeric Data Types
 INT: A standard integer type.
 DECIMAL(M,D): A fixed-point number where MM is the total number of digits and DD is the
number of digits after the decimal point.
 FLOAT: A floating-point number.
2. Character Data Types
 CHAR(n): A fixed-length string.
 VARCHAR(n): A variable-length string with a maximum length of nn.
 TEXT: A string with a maximum length of 65,535 characters.
3. Date and Time Data Types
 DATE: Stores dates in YYYY-MM-DD format.
 DATETIME: Combines date and time in YYYY-MM-DD HH:MM:SS format.
 TIMESTAMP: Stores a timestamp.
4. Binary Data Types
 BINARY(n): Fixed-length binary data.
 VARBINARY(n): Variable-length binary data.
Example of Data Types Usage
Here’s how you might define different columns in a table using various data types:
CREATE TABLE employee
(
employee_id INT PRIMARY KEY,
name VARCHAR(100),
salary DECIMAL(10, 2),
hire_date DATE,
profile_picture VARBINARY(MAX)
);

In this example:
 employee_id is an integer that uniquely identifies each employee.
 name is a variable-length string for storing employee names.
 salary uses the DECIMAL type for precise monetary values.
 hire_date stores the date when the employee was hired.
 profile_picture can store large binary files like images.

DML Operations: INSERT, DELETE, UPDATE


DML (Data Manipulation Language) operations are used to manage data within database tables. The
three primary DML operations are INSERT, DELETE, and UPDATE. Let's explore each with examples.
1. INSERT Operation
The INSERT statement adds new rows to a table.
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Examples:
Inserting a single row:
INSERT INTO employees (emp_id, name, department, salary)
VALUES (101, 'John Smith', 'Marketing', 50000);
Inserting multiple rows at once:
INSERT INTO employees (emp_id, name, department, salary)
VALUES
(102, 'Sarah Johnson', 'Sales', 55000),
(103, 'Michael Brown', 'IT', 60000),
(104, 'Emily Davis', 'HR', 48000);

Inserting without specifying columns (must provide values for all columns in order):
INSERT INTO employees
VALUES (105, 'Robert Wilson', 'Finance', 65000);

2. DELETE Operation
The DELETE statement removes rows from a table.
DELETE FROM table_name
WHERE condition;
Examples:
Deleting specific rows:
DELETE FROM employees
WHERE emp_id = 104;
Deleting with multiple conditions:
DELETE FROM employees
WHERE department = 'Sales' AND salary < 50000;
Deleting all rows (be careful!):
DELETE FROM employees;
-- This removes all data but keeps the table structure

3. UPDATE Operation
The UPDATE statement modifies existing data in a table.
Basic Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Examples:
Updating a single column:
UPDATE employees
SET salary = 52000
WHERE emp_id = 101;
Updating multiple columns:
sql
UPDATE employees
SET department = 'Operations', salary = salary * 1.1
WHERE department = 'IT';
Updating with calculations:
sql
UPDATE employees
SET salary = salary * 1.05
WHERE hire_date < '2020-01-01';
Basic SQL Querying (SELECT and PROJECT)
SELECT with WHERE Clause
Retrieve specific columns (projection) and filter rows (selection):

Arithmetic & Logical Operations

You might also like