0% found this document useful (0 votes)
9 views28 pages

DB Session 4 Slides

Uploaded by

mavismuunganirwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views28 pages

DB Session 4 Slides

Uploaded by

mavismuunganirwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Structured Query Language

Chapter 6
Structured Query Language

Objectives
 Identify the various SQL commands
Introduction

 To manage databases we use a software termed DBMS (Database


Management System)
Introduction

 We have several DBMS and these are broadly categorized into two
Introduction

 In relational Databases, we store data in tables, and the tables have


relationships and that is why its called relational databases
Introduction

 Most common DBMS are as follows, most of the sql code you will learn in
this module will work with any DBMS, in this module we will be using
MySQL, wich is the nost popular DBMS in the world
Introduction

 In NoSQL, they dont use SQL check MongoDB


Introduction

 SQL stands for Structured Query Language


 SQL lets you access and manipulate databases
What Can SQL do?

 SQL can execute queries against a database


 SQL can retrieve data from a database
 SQL can insert records in a database
 SQL can update records in a database
 SQL can delete records from a database
 SQL can create new databases
 SQL can create new tables in a database
 SQL can create stored procedures in a database
 SQL can create views in a database
 SQL can set permissions on tables, procedures, and views
SQL commands

 DDL: Data Definition Language


 This includes changes to the structure of the table like creation of table,
altering table, deleting a table etc.
 All DDL commands are auto-committed. That means it saves all the
changes permanently in the database
SQL commands

 DML: Data Manipulation Language


 DML commands are used for manipulating the data stored in the table and
not the table itself.
 DML commands are not auto-committed. It means changes are not
permanent to database, they can be rolled back.
SQL commands

 TCL: Transaction Control Language


 These commands are to keep a check on other commands and their affect
on the database. These commands can annul changes made by other
commands by rolling the data back to its original state. It can also make
any temporary change permanent
SQL commands

 DCL: Data Control Language


 Data control language are the commands to grant and take back authority
from any database user.
SQL commands

 DQL: Data Query Language


 Data query language is used to fetch data from tables based on conditions
that we can easily apply.
 .
SQL data types

 .
SQL data types

 .
SQL create command

 create is a DDL SQL command used to create a table or a database in


relational database management system.
 To create a database in RDBMS, create command is used
 SQL inbuilt commands should be un upper case
 We terminate the statements in SQL using a semi colon
 CREATE DATABASE student;
 The above command will create a database named student, which will be
an empty schema without any table
 To create a table we need to specify the table name and the attributes
 CREATE TABLE module(studentID INT, name VARCHAR (100), module_name
VARCHAR(100));
SQL create command
SQL alter command

 Alter command is used alter the table structure such as the following
 Add column to existing table
 Rename any existing column
 Change data type of any column or modify its size
 Drop a column from a table
 Syntax
 ALTER TABLE tablename ADD(address VARCHAR(100));
 Using our example
 ALTER TABLE modules ADD(address VARCHAR(200));
 We put comments in SQL using #
SQL alter command
SQL alter command

 Modifying an existing column


 Used to modify the data type of an existing column
 ALTER TABLE modules MODIFY COLUMN address VARCHAR(400);
 DEPENDING WITH THE SQL VERSION

 SQL SERVER
 ALTER TABLE table_name
ALTER COLUMN column_name datatype;
 MY SQL
 ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
SQL alter command
SQL alter command
SQL alter command

 Renaming a column
 Used to rename an existing column
 We want to rename the address column to location
 ALTER TABLE modules CHANGE address location VARCHAR(500);
SQL alter command

 Renaming a column
SQL alter command

 Drop a column
 The drop command can be used to remove columns from the table.
 Here we want to remove the location column from our table
 ALTER TABLE modules DROP location;
 So the location column will be removed from the modules table.
Truncate command command

 The truncate command removes all the records from the table but
however, the table structure will still remain in place
 The command is simple
 TRUNCATE TABLE modules;

 The Drop command


 The command removes the table from the database
 DROP TABLE modules;
 You can also use the Drop command on databases also

 DROP DATABASE student;


 The database will be deleted
Homework

 Using MySQL and the appropriate Graphical tool


Create a database called university
Create the following tables with four columns names per table
Human_resources
Marketing
Student_admissions
Accounts

Perform the following operations:


List all the tables in the university database
Alter the human_resources table and add a column called town
Show the changes done to the table on the screen
Rename any column in Marketing table to location

You might also like