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

Lab Full Slides Short Note Except 7&8

Oop

Uploaded by

Lozan Getnet
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lab Full Slides Short Note Except 7&8

Oop

Uploaded by

Lozan Getnet
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Introduction to Database Management System

Lab Slides
 ER
The ER model is a high-level data model which stands for the Entity-Relationship Model. It
is used for defining the relationship and elements.
ER-Diagram is a pictorial representation of data that describes how data is communicated
and related to each other
 Entities - Table name
 Attributes - Column (Key, Multi Value, Derived). It describes the entity property.
 Relationships - (Unary, Binary). Is the relation between each entity.
 Cardinality – (1:1, 1:N, N:M)
 SQL (Structured Query Language)
SQL lets you access and manipulate databases. It can execute queries against a database
SQL can retrieve data from a database. Insert Update Delete records in a database and set
permission on tables.
 DDL (Data Definition Language) - Create
 DML (Data Manipulation Language) - Insert
 DCL (Data Control Language) - Grant, Deny
 DQL (Data Query Language) – Select
 MSSQL
Relational database management system (RDBMS), that is developed by Microsoft.
SQL language. Contains T-SQL its own implementation. SSMS (SQL Server Management
Studio)
 System Databases
 Master - contains information about SQL server configuration
 Model - The model database sets a template for every database that was newly create
 MSDB - Is used by SQL Server Agent for scheduling alerts and jobs.
 tempdb - Is a workspace for holding temporary objects or intermediate result sets.
 Relations
2D Table with specific characteristics. Rows contains instance of entity while Cells hold
single value. No two rows are identical
All value in column are same kind (Data type) and each column has unique name.
 Data Types
 bigint | int | smallint | tinyint (0 to 255) | Bit | numeric
 char | nchar - UNICODE
 varchar | nvarchar – UNICODE
 decimal (P, S) - exact P - Precision (total number of digits ) | S - Scale, number
of digits after the decimal point
 float - approximate
 Datetime(3.33 milisec accuracy) | Smalldatetime(1 min accuracy) | Date | Time
 text | ntext - (2,147,483,647) byte
 money | Smallmoney
 SQL Constraints
Constraints are used to limit the type of data that can go into a table. Constraints can be
specified when a table is created (with the CREATE TABLE statement) or after the table is
created (with the ALTER TABLE statement).

 NOT NULL - Ensures a column cannot have a NULL value


 UNIQUE - Ensures all values in a column are unique
 PRIMARY KEY - Identifies a record in a table, is NOT NULL & UNIQUE. 1 per
table
 FOREIGN KEY - References a unique record from another table. It’s used to link
two tables together. (Referencing key)
FOREIGN KEY is attribute in one table that that refers to the PRIMARY KEY in
another table.
Referential Integrity: is a property of data stating that all its references are valid.
 CHECK - Ensures all column values satisfy a condition. It limit the value range that
can be placed in a column. Predefined conditions.
 DEFAULT - Set a default value for a column if none is entered
 INDEX - Quick way of retrieving records from database
 Auto Increment | Identity
Auto-increment allows a unique number to be generated automatically when a new
record is inserted into a table.
Syntax: CREATE TABLE Student ( Insert using 2nd syntax
id int PRIMARY KEY IDENTITY ); MYSQL: AUTO_INCREMENT
Customizing Auto Increment: IDENTITY (start, increment by)
IDENTITY (10,2)
 Query Statements
 Dealing with Database: CREATE DATABASE SIS
ALTER DATABASE SIS MODIFY NAME = “SIS-NEW”
DROP DATABASE SIS-NEW
 USE Keyword is used to select existing database when multiple databases exist.
USE DATABASE You cannot drop database that is currently in use.
 Dealing with table: CREATE TABLE table_name (
column1 datatype,
column2 datatype,
.... );
 INSERT INTO is used to add new rows of data to a table in the database.
Syntax: INSERT INTO TABLE_NAME VALUES (value1 , value2 , value3);
INSERT INTO TABLE_NAME (col1 , col2 ) VALUES (value1 , value2 );
 SELECT Statement is used to fetch the data from a database table. These result
tables are called result-sets.
Syntax: SELECT COL1 , COL2 , COL3 FROM TABLE_NAME
 ALTER TABLE statement is used to add, delete, or modify columns in an
existing table. Some database systems don't allow deleting a column.
Syntax: ALTER TABLE table_name ADD column_name data_type
ALTER TABLE table_name DROP COLUMN column_name
ALTER TABLE table_name ALTER COLUMN column_name datatype
(Changing Data type)
 Drop Statement is used to delete table. DROP TABLE TABLE_NAME
 Dealing with Constraints: ALTER TABLE Student
MODIFY Age int NOT NULL;
OR CREATE TABLE Student (
ID int NOT NULL,
LastName varchar(255) NOT NULL
Named Constraint: CONSTRAINT UC_Stud UNIQUE (ID,LastName) );
 Using Alter Table: ALTER TABLE Students
ADD CONSTRAINT UC_Students UNIQUE (ID,LastName);
OR ALTER TABLE Persons
ADD UNIQUE (ID, LastName); Can also be for single column
 Primary Key Constraint: ALTER TABLE Students
ADD PRIMARY KEY (ID); (Can also be done on create)
Named Constraint: ALTER TABLE Students
ADD CONSTRAINT PK_Students PRIMARY KEY (ID);
 Foreign Key Constraint: CREATE TABLE Students(
Studentid int PRIMARY KEY,
StudentName varchar(255) NOT NULL
DepartmentId int FOREIGN KEY REFERENCES Department(id)
//If it was named constraint:
CONSTRAINT FK_StudentDepartment FOREIGN KEY (did ));
ALTER TABLE Student
ADD FOREIGN KEY (did) REFERENCES Department(id);
ALTER TABLE Student
Named Constraint: ADD CONSTRAINT FK_StudentDepartment
FOREIGN KEY (did) REFERENCES Department(id);
 Check Constraint: CREATE TABLE Students (
ID int NOT NULL,
FirstName varchar(255),
Age int CHECK (Age>=18)
//If it was named constraint:
CONSTRAINT CHK_Students CHECK (Age>=18 AND ........) );

ALTER TABLE Students


ADD CHECK (Age>=18);
ALTER TABLE Students
ADD CONSTRAINT CHK_StudentsAge CHECK (Age>=18 AND ...);
 Dropping a constraint: MSSQL MYSQL
ALTER TABLE Students ALTER TABLE Persons
DROP CONSTRAINT ConstarintName; DROP INDEX UC_
CREATE TABLE Students(
FirstName varchar(255),
ProfileUrl varchar(255) DEFAULT 'Default.png' //Default Constraint
RegstrationDate date DEFAULT GETDATE() ); //System Time (System Function)
ALTER TABLE Students
ADD CONSTRAINT df_StudentsProfile
DEFAULT 'Default.png' FOR ProfilePicture;
Dropping Default: ALTER TABLE Students
ALTER COLUMN ProfilePicture DROP DEFAULT;
 Select Distinct: Returns only distinct (different) values.
SELECT DISTINCT address FROM Customers;
 SQL Where: used to filter records based on the condition provided
SELECT * FROM Customers where condition;
Operators: =,>,<,>=,<=,<>(!=),Between (Range), Like(Pattern), In(Possible Value)
Multiple Conditions are combined using: AND, OR, NOT
For complex expressions involving both AND/OR use parenthesis
 ORDER BY: ORDER BY keyword is used to sort the result-set by a specified column.
It’s in ascending order by default.
If you want to sort the records in a descending order, you can use the DESC keyword.
SELECT * FROM Customers ORDER BY Name DESC
SELECT * FROM Customers ORDER BY Name , Address
Order By Name. For duplicate name use Address as additional parameter
 UPDATE: modify the existing records in a table.
UPDATE table_name
SET column1 = value1, column2 = value2,
WHERE condition; //WITHOUT WHERE - Updates all records
 DELETE: used to delete the existing records from a table.
DELETE FROM table_name WHERE [condition];
//WITHOUT WHERE - Deletes all records

You might also like