database management system: sql commands lab sql

trapeziumtrapezoid12 46 views 21 slides Jun 09, 2024
Slide 1
Slide 1 of 21
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21

About This Presentation

database management system: sql commands lab sql


Slide Content

Section One : SQL Commands Outline These  SQL commands are mainly categorized into five categories:  DDL – Data Definition Language DQL – Data Query Language DML – Data Manipulation Language DCL – Data Control Language TCL – Transaction Control Language Prepared by Befkadu

Section One : SQL Commands Prepared by Befkadu

What is Data ? SQL commands are like instructions to a table. It is used to interact with the database with some operations. It is also used to perform specific tasks, functions, and queries of data. SQL can perform various tasks like creating a table, adding data to tables, dropping the table, modifying the table, set permission for users. These SQL commands are mainly categorized into five categories:  DDL – Data Definition Language DQL – Data Query Language DML – Data Manipulation Language DCL – Data Control Language TCL – Transaction Control Language

DDL (Data Definition Language) DDL  or Data Definition Language actually consists of the SQL commands that can be used to define the database schema. It simply deals with descriptions of the database schema and is used to create and modify the structure of database objects in the database. DDL is a set of SQL commands used to create, modify, and delete database structures but not data. These commands are normally not used by a general user, who should be accessing the database via an application. List of DDL commands:  CREATE: This command is used to create the database or its objects (like table, index, function, views, store procedure, and triggers). DROP: This command is used to delete objects from the database. ALTER: This is used to alter the structure of the database. TRUNCATE: This is used to remove all records from a table, including all spaces allocated for the records are removed. COMMENT: This is used to add comments to the data dictionary. RENAME: This is used to rename an object existing in the database.

DQL (Data Query Language) DQL  statements are used for performing queries on the data within schema objects. The purpose of the DQL Command is to get some schema relation based on the query passed to it. We can define DQL as follows it is a component of SQL statement that allows getting data from the database and imposing order upon it. It includes the SELECT statement. This command allows getting the data out of the database to perform operations with it. When a SELECT is fired against a table or tables the result is compiled into a further temporary table, which is displayed or perhaps received by the program i.e. a front-end. List of DQL:  SELECT: It is used to retrieve data from the database.

DML(Data Manipulation Language) The SQL commands that deal with the manipulation of data present in the database belong to DML or Data Manipulation Language and this includes most of the SQL statements. It is the component of the SQL statement that controls access to data and to the database. Basically, DCL statements are grouped with DML statements. List of DDL commands:  CREATE: This command is used to create the database or its objects (like table, index, function, views, store procedure, and triggers). List of DML commands:  INSERT: It is used to insert data into a table. UPDATE: It is used to update existing data within a table. DELETE: It is used to delete records from a database table. LOCK: Table control concurrency. CALL: Call a PL/SQL or JAVA subprogram. EXPLAIN PLAN: It describes the access path to data.

DCL (Data Control Language) DCL includes commands such as GRANT and REVOKE which mainly deal with the rights, permissions, and other controls of the database system.  List of  DCL commands:  GRANT : This command gives users access privileges to the database . Syntax: GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER; REVOKE: This command withdraws the user’s access privileges given by using the GRANT command . REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;  

TCL (Transaction Control Language) Transactions group a set of tasks into a single execution unit. Each transaction begins with a specific task and ends when all the tasks in the group are successfully completed. If any of the tasks fail, the transaction fails. Therefore, a transaction has only two results: success or failure. You can explore more about transactions here. Hence, the following TCL commands are used to control the execution of a transaction : COMMIT: Commits a Transaction. ROLLBACK: Rollbacks a transaction in case of any error occurs. SAVEPOINT: Sets a save point within a transaction. 

DDL DDL changes the structure of the table like creating a table, deleting a table, altering a table, etc. All the command of DDL are auto-committed that means it permanently save all the changes in the database. Here are some commands that come under DDL: CREATE ALTER DROP TRUNCATE

DDL DDL changes the structure of the table like creating a table, deleting a table, altering a table, etc. All the command of DDL are auto-committed that means it permanently save all the changes in the database. Here are some commands that come under DDL: CREATE ALTER DROP TRUNCATE

CREATE Command for DATABASE SQL Server Database can be Created, Alter by Graphically using SQL Server Management Studio(SSMS) or Using Query CREATE DATABASE [ Database_Name_You_Want_To_Create ] Example : CREATE DATABASE CRMS;

Alter Command for DATABASE Rename the Database Graphically using SQL Server Management Studio(SSMS) or Using Alter Command ALTER DATABASE [ Database_Name_You_Want_To_Rename ] Modify Name = [ NEW_DB_NAME ] Example : ALTER DATABASE CRMS Modify Name= ‘ CRMS_DEV’; Example : s p_renameDB CRMS Modify Name= ‘ CRMS_DEV’; Using System Stored Procedure

DROP Command for DATABASE DROP the Database Graphically using SQL Server Management Studio(SSMS) or Using DROP Command DROP DATABASE [ Database_Name_You_Want_To_DROP ] Example : DROP DATABASE CRMS; Example : -- This command close database connection form other user -- and delete the database ALTER DATABASE CRMS set SINGLE_USER With ROLLBACK immediate ; Delete Database Using ALTER

CREATE Command for TABLE CREATE TABLE Department ( depid char ( 5 ) NOT NULL, depname char ( 40 ), budget FLOAT , PRIMARY KEY ( depid ), UNIQUE ( depname ) ) CREATE TABLE Employee ( empid char ( 5 ) NOT NULL, empname char ( 40 ), depid char ( 5 ), salary float , PRIMARY KEY ( empid ), FOREIGN KEY ( depid ) REFERENCES Department ON UPDATE CASCADE ON DELETE CASCADE );

Part One Database Design/Modeling Conceptual Design Logical Design Physical Design

Objectives Designing a comprehensive Outpatient Management System (OPMS) database involves conceptual modeling, logical design, and physical design phases . OutpatientMSDB

Conceptual Modeling Identifying Major Entities Patients: Information about patients . Doctors: Details of doctors providing services . Appointments: Scheduled appointments between patients and doctors . MedicalRecords : Records of medical history, diagnosis, and prescriptions . Billing: Billing information for appointments . Medications: Information about medications prescribed.

Conceptual Modeling: Relationships Patients - Appointments: Many-to-Many (A patient can have multiple appointments, and an appointment can have multiple patients ). Doctors - Appointments: One-to-Many (A doctor can have multiple appointments ). Patients - MedicalRecords : One-to-Many (A patient can have multiple medical records ). Doctors - MedicalRecords : One-to-Many (A doctor can have multiple medical records ). Appointments - MedicalRecords : One-to-One (An appointment can have one medical record ). Appointments - Billing: One-to-One (An appointment can have one billing record ). Medications - MedicalRecords : Many-to-Many (A medication can be prescribed in multiple medical records, and a medical record can have multiple medications).

Logical Design Based on the conceptual model, we create tables with attributes representing entities and relationships . Patients ( PatientID , FirstName , LastName , DateOfBirth , Gender, Email, Phone ) Doctors ( DoctorID , FirstName , LastName , Specialization, Email, Phone ) Appointments ( AppointmentID , PatientID , DoctorID , AppointmentDateTime , Notes ) MedicalRecords ( RecordID , PatientID , DoctorID , AppointmentID , DateOfVisit , Diagnosis, Prescription ) Billing ( BillID , AppointmentID , Amount, PaymentStatus ) Medications ( MedicationID , MedicationName )

Physical Design

Physical Design Translates logical design into actual database tables, columns , and relationships . CREATE TABLE Patients ( PatientID INT PRIMARY KEY IDENTITY , FirstName VARCHAR(50 ), LastName VARCHAR(50 ), DateOfBirth DATE , Gender VARCHAR(10 ), Email VARCHAR(100) UNIQUE , Phone VARCHAR(20 ), CONSTRAINT CHK_Gender CHECK (Gender IN ('Male', 'Female‘) );
Tags