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

Assignment - 158 Talha Khalil (SQL Queries)

The document discusses various SQL commands like creating a table, inserting data, updating and deleting rows, using where, order and logical clauses, arithmetic operators and joining tables. It provides examples of using each command on a sample table.

Uploaded by

gnisar327
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)
9 views19 pages

Assignment - 158 Talha Khalil (SQL Queries)

The document discusses various SQL commands like creating a table, inserting data, updating and deleting rows, using where, order and logical clauses, arithmetic operators and joining tables. It provides examples of using each command on a sample table.

Uploaded by

gnisar327
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/ 19

Name: Talha Khalil

Roll No: 158/536359


Department: BSCS
Semester: 4th Evening
Course Title: Database Systems
Course Code: CSI-406
Submitted to: Mr. Umair Ilays

Talha Khalil 1
Table of Contents
Creating a Table...........................................................................................................................................3
Insert in table..............................................................................................................................................4
To Insert/Delete Column/s in existing table................................................................................................5
Deleting a table in database........................................................................................................................6
Show Columns without repeating rows.......................................................................................................7
Deleting row or rows from column..............................................................................................................8
Where Clause..............................................................................................................................................9
Order Clause..............................................................................................................................................10
Logical Operators.......................................................................................................................................11
And Operator:........................................................................................................................................11
In Operator............................................................................................................................................12
Between operator;................................................................................................................................12
Arithmetic Operators.................................................................................................................................13
+ Operator.............................................................................................................................................13
Sub Operator.........................................................................................................................................13
Multiply operator..................................................................................................................................14
Divide Operator.....................................................................................................................................14
Operator Precedence................................................................................................................................15
Modify Data type of an Existing Table.......................................................................................................15
Update statement.....................................................................................................................................16
Update one Column in a row.................................................................................................................16
Update several columns in a row..........................................................................................................16
Simple Join.................................................................................................................................................17
Equi join.................................................................................................................................................17
Non-Equi-Join........................................................................................................................................18
PRIMARY AND FOREIGN KEY.....................................................................................................................19
Primary Key...........................................................................................................................................19
Foreign key............................................................................................................................................19
Testing Primary key...............................................................................................................................20

Talha Khalil 2
Creating a Table
To Create a table,use query:
//Create table_name (Column1 name Data type, Column2 name data
type);//

Created a table named Directory1 with four fields (First Name, Last
Name, Number, Address)
Using the Query:
Create Table Directory1 (First_name char (50) ,
Last_name char (50) , Address Varchar (50) , Number
numeric (20));
To show Table use Query:
//Select * from student;//

Talha Khalil 3
Insert in table
To insert new rows in table we use query:
//Insert into table_name values (value1, value2, value3) ;//
Or we can insert in individual columns by using query:
// Insert into table_name (column1, column2, column3) values (value1,
value2, value3)//

Inserted four rows using queries:


insert into Directory1
values('Subhan','Ahmad','City1',041);
insert into Directory1
values('Muqeet','Reyaz','City2',042);
insert into Directory1
values('Ali','Ejaz','City3',043);
insert into Directory1
values('Talha','Mugal','City4',042);

Talha Khalil 4
To Insert/Delete Column/s in existing table
ALTER Statements:
The ALTER TABLE Statement is used to add or drop columns in an existing table.
It is also used to change the data type of an existing field of a table.

To insert column in table we use query;


//Alter Table_Name Add column_name data type; //

Using Query:
Alter table Directory1 Add remarks numeric;
To remove Remarks column we use querry:
Alter table Directory1 Drop column remarks;

Talha Khalil 5
Deleting a table in database
To delete a table from database we use command or query:
//Drop table table_name; //

As we can see there is no table named Directory1 after deleting it


Using command:
Drop table Directory1;

Talha Khalil 6
Show Columns without repeating rows
Before using command;

After using command:


Syntax:
//Select Distinct Column-name, Column Name from table- name//
select distinct First_name, Last_name, Address,
Number from Directory1;

Talha Khalil 7
Deleting row or rows from column
Delete statement:
Delete statement is used to delete row or rows in a table.
Syntax:
To delete a single row we use:
Delete from table-name where Column-name = row you want to delete;
To delete all rows:
Delete From Table-name;

As you can see all the rows are deleted from Directory1 After using
command:
Delete from Directory1;

Talha Khalil 8
Where Clause
Where clause is used to retrieve Data from table conditionally it can
appear only after from clause.
Syntax:
Select * from table-name where Column name condition;
We can use (,) between column names to select multiple columns if you
want to.

As we can see row with Subhan as displays after using command;


Select * from Directory1 where First_name='Subhan';

Talha Khalil 9
Order Clause
Order Clause is used to Sort the rows in ascending or descending order
using command:
Select * from table-name order by column-name ASC or DESC;
Or
Select * from table-name order by column-name;

As we can see have arranged table Alphabetical order by using


command:
select * from Directory1 order by First_name;

Talha Khalil 10
Logical Operators
Operator Description
And Display if all conditions are true
Or Display if any condition is true
Not Negates an Expression or condition
In Display if any listed condition is true
Between Display if it Operand is in within range of comparison

And Operator:

We display column Ali Ejaz using And Operator;


Select * from Directory1 where First_name='Ali' and
Last_name='Ejaz';

In

Operator

Talha Khalil 11
Command used:
Select * from Directory1 where Networth IN
(1000,2000,10000);

Between operator;
Command used:
Select * from Directory1 where Networth between 1000
and 5000;

Arithmetic Operators
Operators Description
+ To Add
- To Sub
* To Multiply
/ To Divide
+ Operator
After using command:
Select Networth+1000 from Directory1;

Talha Khalil 12
Sub Operator
After using command:
Select First_name,Networth-1000 from Directory1;

Multiply operator
Query Used:
Select First_name,Networth*10 from Directory1;

Divide Operator
Query Used:
Select First_name,Networth/10 from Directory1;

Talha Khalil 13
Operator Precedence
The Order in which the different type of operators is evaluated is known
as an operator Precedence of operators it is also known as hierarchy of
operator’s multiplication and division is performed before addition and
subtraction.
Query Used:
Select First_name,Networth+300*12 from Directory1;

In the above example we can see multiply is first evaluated before


addition.

Modify Data type of an Existing Table


To modify data type of an existing table we use query:
Alter Table-name Modify (column-name data type);

Talha Khalil 14
Update statement
The update statement is used to modify the existing data in a table.
Its syntax is:
Update table-name SET column name =new value WHERE Column
name=some value;
We can use (,) between column names to update multiple columns data.
Update one Column in a row
Query used:
update Directory1 set Networth=3000 Where
First_name='Subhan';

As we can see Subhan net worth has changed to 3000.


Update several columns in a row
Query used:
update Directory1 set Networth=1000,Number=92105 Where First_name='Subhan';

Talha Khalil 15
Simple Join
Simple join is the most common type of join.it retrieves rows from
tables. these table should have a common column or set of columns that
can be Logically related.
Equi join
A type of join that is based on Equalities is called equi-join.
Syntax:
Select table1.column,table2,column
From table1,table
Where table1.column=table2,column
Query Used;
Select
Directory1.Address,Directory1.First_name,Directory2.Address,
Directory2.Networth from Directory1,Directory2 Where
Directory1.Address=Directory2.Address;
Before Columns

After

Talha Khalil 16
Non-Equi-Join
A Non-Equi-join specifies the relation between columns of tables by
using (>,<,>=,<=,<>) other than =. The following example is illustrating
Non-Equi-Join.
Query Used
SelectDirectory1.Networth,Directory1.First_name,Directory2.First_name,
Directory2.Networth from Directory1,Directory2 Where
Directory2.Networth>Directory1.Networth;
Tables Before

Tables After
As we can see after using > in non equi
join greater net worth are organized

Talha Khalil 17
PRIMARY AND FOREIGN KEY
Primary Key
A Primary key is used to define a column or set of columns as primary key. It
restricts duplication of rows and does not allow null values.
Query I used to create a table with foreign key:
CREATE TABLE EMP(DEPTNO INT PRIMARY KEY , DNAME CHAR(50),
LOCATION CHAR(50));

Here DEPTNO is the Primary key (10,20,30)


The table which has primary key is known as
Parent table. DEPTNO Also act as a common
Field for Foreign key of Child table to link
Data between them.

Foreign key
Used to define a column or set of columns as foreign key. Foreign key is used to
create link between two tables by using Primary key of the parent table. The table
in which the foreign is used is known as Child
table
Using Query:
CREATE TABLE SALARY(DEPTNO INT FOREIGN
KEY REFERENCES EMP(DEPTNO),EMPID
NUMERIC(10),ENAME CHAR(20),SALARY MONEY);
Here DEPTNO is common in both table and act
as a Foreign key.

Talha Khalil 18
Testing Primary key
We can check working of Primary key by inserting data into Child table using the
Primary key in Parent table
Using Query:
INSERT SALARY VALUES (10 ,101,'TALHA',50000);
INSERT SALARY VALUES (20 ,102,'Subhan',60000);
INSERT SALARY VALUES (30 ,103,'Umar',70000);

As we can see we have successfully


entered
Data into rows using (10,20,30) of
DEPTNO column as Primary key.

Now let see if Data is successfully entered


if we use 40 as Primary key

Using Query
INSERT SALARY VALUES (40 ,103,'Umar',70000);

As we can see if we 40 as primary key then data cannot be entered so the primary
key is working properly.

Talha Khalil 19

You might also like