KEMBAR78
database management system: sql commands lab sql | PPTX
Section One : SQL Commands
Outline
o These SQL commands are mainly categorized into five categories:
o DDL – Data Definition Language
o DQL – Data Query Language
o DML – Data Manipulation Language
o DCL – Data Control Language
o TCL – Transaction Control Language
PreparedbyBefkadu
Section One : SQL Commands
PreparedbyBefkadu
DDL
Create
Alter
Drop
Truncate
DML
Insert
Update
Delete
DCL
Grant
Revoke
TCL
Commit
Rollback
Save Point
DQL
Select
What is Data ?
o 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.
o These SQL commands are mainly categorized into five categories:
o DDL – Data Definition Language
o DQL – Data Query Language
o DML – Data Manipulation Language
o DCL – Data Control Language
o TCL – Transaction Control Language
DDL (Data Definition Language)
o 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.
o List of DDL commands:
o CREATE: This command is used to create the database or its objects (like table, index, function, views, store procedure,
and triggers).
o DROP: This command is used to delete objects from the database.
o ALTER: This is used to alter the structure of the database.
o TRUNCATE: This is used to remove all records from a table, including all spaces allocated for the records are removed.
o COMMENT: This is used to add comments to the data dictionary.
o RENAME: This is used to rename an object existing in the database.
DQL (Data Query Language)
oDQL 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.
oList of DQL:
o SELECT: It is used to retrieve data from the database.
DML(Data Manipulation Language)
o 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.
o List of DDL commands:
o 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:
o INSERT: It is used to insert data into a table.
o UPDATE: It is used to update existing data within a table.
o DELETE: It is used to delete records from a database table.
o LOCK: Table control concurrency.
o CALL: Call a PL/SQL or JAVA subprogram.
o EXPLAIN PLAN: It describes the access path to data.
DCL (Data Control Language)
oDCL includes commands such as GRANT and REVOKE which mainly deal
with the rights, permissions, and other controls of the database system.
oList of DCL commands:
oGRANT: This command gives users access privileges to the database.
Syntax:
o GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;
oREVOKE: This command withdraws the user’s access privileges given by using
the GRANT command.
o REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;
TCL (Transaction Control Language)
oTransactions 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:
o COMMIT: Commits a Transaction.
o ROLLBACK: Rollbacks a transaction in case of any error occurs.
o 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 :
sp_renameDB CRMS Modify Name= ‘CRMS_DEV’;
o 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;
o 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
oDatabase Design/Modeling
oConceptual Design
oLogical Design
oPhysical Design
Objectives
oDesigning a comprehensive Outpatient Management
oSystem (OPMS) database involves conceptual modeling,
ological design, and physical design phases.
oOutpatientMSDB
Conceptual Modeling
oIdentifying Major Entities
oPatients: Information about patients.
oDoctors: Details of doctors providing services.
oAppointments: Scheduled appointments between patients and
odoctors.
oMedicalRecords: Records of medical history, diagnosis, and
oprescriptions.
oBilling: Billing information for appointments.
oMedications: Information about medications prescribed.
Conceptual Modeling: Relationships
o Patients - Appointments: Many-to-Many (A patient can have multiple appointments, and an
o appointment can have multiple patients).
o Doctors - Appointments: One-to-Many (A doctor can have multiple appointments).
o Patients - MedicalRecords: One-to-Many (A patient can have multiple medical records).
o Doctors - MedicalRecords: One-to-Many (A doctor can have multiple medical records).
o Appointments - MedicalRecords: One-to-One (An appointment can have one medical
record).
o Appointments - Billing: One-to-One (An appointment can have one billing record).
o Medications - MedicalRecords: Many-to-Many (A medication can be prescribed in multiple
o medical records, and a medical record can have multiple medications).
Logical Design
oBased on the conceptual model, we create tables with attributes representing
entities and relationships.
o Patients (PatientID, FirstName, LastName, DateOfBirth, Gender, Email, Phone)
o Doctors (DoctorID, FirstName, LastName, Specialization, Email, Phone)
o Appointments (AppointmentID, PatientID, DoctorID, AppointmentDateTime, Notes)
o MedicalRecords (RecordID, PatientID, DoctorID, AppointmentID, DateOfVisit,
o Diagnosis, Prescription)
o Billing (BillID, AppointmentID, Amount, PaymentStatus)
o Medications (MedicationID, MedicationName)
Physical Design
Physical Design
oTranslates 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‘)
);

database management system: sql commands lab sql

  • 1.
    Section One :SQL Commands Outline o These SQL commands are mainly categorized into five categories: o DDL – Data Definition Language o DQL – Data Query Language o DML – Data Manipulation Language o DCL – Data Control Language o TCL – Transaction Control Language PreparedbyBefkadu
  • 2.
    Section One :SQL Commands PreparedbyBefkadu DDL Create Alter Drop Truncate DML Insert Update Delete DCL Grant Revoke TCL Commit Rollback Save Point DQL Select
  • 3.
    What is Data? o 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. o These SQL commands are mainly categorized into five categories: o DDL – Data Definition Language o DQL – Data Query Language o DML – Data Manipulation Language o DCL – Data Control Language o TCL – Transaction Control Language
  • 4.
    DDL (Data DefinitionLanguage) o 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. o List of DDL commands: o CREATE: This command is used to create the database or its objects (like table, index, function, views, store procedure, and triggers). o DROP: This command is used to delete objects from the database. o ALTER: This is used to alter the structure of the database. o TRUNCATE: This is used to remove all records from a table, including all spaces allocated for the records are removed. o COMMENT: This is used to add comments to the data dictionary. o RENAME: This is used to rename an object existing in the database.
  • 5.
    DQL (Data QueryLanguage) oDQL 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. oList of DQL: o SELECT: It is used to retrieve data from the database.
  • 6.
    DML(Data Manipulation Language) oThe 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. o List of DDL commands: o 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: o INSERT: It is used to insert data into a table. o UPDATE: It is used to update existing data within a table. o DELETE: It is used to delete records from a database table. o LOCK: Table control concurrency. o CALL: Call a PL/SQL or JAVA subprogram. o EXPLAIN PLAN: It describes the access path to data.
  • 7.
    DCL (Data ControlLanguage) oDCL includes commands such as GRANT and REVOKE which mainly deal with the rights, permissions, and other controls of the database system. oList of DCL commands: oGRANT: This command gives users access privileges to the database. Syntax: o GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER; oREVOKE: This command withdraws the user’s access privileges given by using the GRANT command. o REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;
  • 8.
    TCL (Transaction ControlLanguage) oTransactions 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: o COMMIT: Commits a Transaction. o ROLLBACK: Rollbacks a transaction in case of any error occurs. o SAVEPOINT: Sets a save point within a transaction.
  • 9.
    DDL • DDL changesthe 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
  • 10.
    DDL • DDL changesthe 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
  • 11.
    CREATE Command forDATABASE 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;
  • 12.
    Alter Command forDATABASE 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 : sp_renameDB CRMS Modify Name= ‘CRMS_DEV’; o Using System Stored Procedure
  • 13.
    DROP Command forDATABASE 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; o Delete Database Using ALTER
  • 14.
    CREATE Command forTABLE 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 );
  • 15.
    Part One oDatabase Design/Modeling oConceptualDesign oLogical Design oPhysical Design
  • 16.
    Objectives oDesigning a comprehensiveOutpatient Management oSystem (OPMS) database involves conceptual modeling, ological design, and physical design phases. oOutpatientMSDB
  • 17.
    Conceptual Modeling oIdentifying MajorEntities oPatients: Information about patients. oDoctors: Details of doctors providing services. oAppointments: Scheduled appointments between patients and odoctors. oMedicalRecords: Records of medical history, diagnosis, and oprescriptions. oBilling: Billing information for appointments. oMedications: Information about medications prescribed.
  • 18.
    Conceptual Modeling: Relationships oPatients - Appointments: Many-to-Many (A patient can have multiple appointments, and an o appointment can have multiple patients). o Doctors - Appointments: One-to-Many (A doctor can have multiple appointments). o Patients - MedicalRecords: One-to-Many (A patient can have multiple medical records). o Doctors - MedicalRecords: One-to-Many (A doctor can have multiple medical records). o Appointments - MedicalRecords: One-to-One (An appointment can have one medical record). o Appointments - Billing: One-to-One (An appointment can have one billing record). o Medications - MedicalRecords: Many-to-Many (A medication can be prescribed in multiple o medical records, and a medical record can have multiple medications).
  • 19.
    Logical Design oBased onthe conceptual model, we create tables with attributes representing entities and relationships. o Patients (PatientID, FirstName, LastName, DateOfBirth, Gender, Email, Phone) o Doctors (DoctorID, FirstName, LastName, Specialization, Email, Phone) o Appointments (AppointmentID, PatientID, DoctorID, AppointmentDateTime, Notes) o MedicalRecords (RecordID, PatientID, DoctorID, AppointmentID, DateOfVisit, o Diagnosis, Prescription) o Billing (BillID, AppointmentID, Amount, PaymentStatus) o Medications (MedicationID, MedicationName)
  • 20.
  • 21.
    Physical Design oTranslates logicaldesign 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‘) );