DBMS SQL Commands (Autosaved) (Autosaved)
DBMS SQL Commands (Autosaved) (Autosaved)
; is the terminator – without semi colon , command are not accepted in sql
Create Table command
• This command is used to create e table
• Syntax is –
• CREATE TABLE is the keyword telling the database system what you want to do. In this case, you
want to create a new table. The unique name or identifier for the table follows the CREATE TABLE
statement.
• Then in brackets comes the list defining each column in the table and what sort of data type it is.
The syntax becomes clearer with the following example.
• Int, Char, Varchar, Varchar2, Varchar3, decimal, Date
Command to create the structure of a table (framework) -Columns
SQL> CREATE TABLE CUSTOMERS( ID INT NOT NULL, NAME VARCHAR (20)
NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2),
PRIMARY KEY (ID) );
• If you are adding values for all the columns of the table, you do not need to specify the column
names in the SQL query. However, make sure the order of the values is in the same order as the
columns in the table. The INSERT INTO syntax would be as follows:
• INSERT INTO table_name
VALUES (value1, value2, value3, ...);
• Put values for all the columns
Insert into customers values(14, ‘Anil’,20,’Mumbai’,30000.50);
Example
Customer Customer ContactNa Address City PostalCod Country
ID Name me e
89 White Karl 305 - 14th Seattle 98128 USA
Clover Jablonski Ave. S.
Markets Suite 3B
90 Wilman Matti Keskuskat Helsinki 21240 Finland
Kala Karttunen u 45
Write command to put record into customers2 table with following table
customer_id int ,
customer_name varchar(20),
Contact_Name varchar2(20),
Address varchar2(30),
City varchar2 (20), postal_code varchar(10), country char(20) primary key
(customer_id));
Select * from Tablename; (This displays all the columns all the records from the
table)
• To display all the columns (attributes), display the whole
table
Select * from customers2;
Where Clause
• The SQL WHERE Clause is not a command
• The WHERE clause is used to filter records. To give condition/criteria
• The WHERE clause is used to extract only those records that fulfill a specified condition.
• WHERE Syntax
• SELECT column1, column2, ...
FROM table_name
WHERE condition;
• Example
• SELECT * FROM Customers2
WHERE Country='Mexico’;
• The WHERE clause is not only used in SELECT statement, it is also used in UPDATE, DELETE statement, etc.!
• Select * from customers2 where city=‘New York’ and country = ‘USA’
Perform following questions- table name is customers
• Display the data of person s name naina and city is Raipur
• City Average(fees)
• Delhi 1234
Mumbai 3547
Chennai 4758
Kolkata 3658
• Q. Display the total marks of every class
select class, sum(marks) from student group by class ;
20. Change the country and city of person name Mike to Britain and London
Update customers2 set city=‘London’ , country =‘Britain’ where customer_name=‘Mike’;
Example
• UPDATE Customers
SET ContactName = 'Alfred Schmidt',
WHERE CustomerID = 1;
• UPDATE Customers
SET ContactName = 'Alfred Schmidt',
City= 'Frankfurt'
WHERE CustomerID = 1;
• UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';
12. Display the list of people sorted alphabetically by their names.
Select * from customers2 order by customer_name;
15. Display the total salary of all employees from employee table (emp_id int(2), name char(20), dept, designation, emp_sal, bonus, sales )
Select sum(emp_sal) from employee ;
• 16. Display the average salary of all employees from employee table (emp_id, name, dept, designation, emp_sal, bonus,sales)
• Select average(emp_sal) from employee ;
• 17. Display number of employees in each dept. (dept wise) (every dept)
• Select dept, count(emp_id) from employee group by dept;
• Accounts 5
• Finance 3
• HR 4
Create table student (doj date
Insert into student (‘11/01/2020’
Table to be created
FIRST NAME LAST NAME AGE CITY Region Fees Marks Date of joining Grade
Abhijeet Mishra 37 Delhi East 2300 45 43427 A
Abhijeet Salvi 45 Mumbai West 3000 34 43570 A
Anil Patil 56 Delhi North 3200 46 43575 A
Nagrajan Sheshan 20 Chennai South 3400 42 43225 A
Naina Rastogi 35 Mumbai West 4570 15 44111 B
Priya Agrawal 40 Delhi East 9867 27 43689 B
Priya Mishra 39 Patna West 3456 38 44175 A
Rahul Agrawal 30 Mumbai North 2300 37 43391 A
Rahul Sharma 29 Chennai East 9812 27 43770 B
Renu Dave 55 Chennai East 2367 19 43558 B
Practice Commands. write in the notebook and leave 2 lines blank for the
answers
1. Create the table student
2. Insert 5 records
3. Display the records for the people residing in Mumbai or delhi
Select * from student where city=‘Mumbai’ or City =‘Delhi’;
4. Display the total fees paid in every city
Select city, sum(fees) from student group by city;
5. Display the average age of all people
Select average(age) from student;
6.Display the count of persons in each region in ascending orde of region
Select region,Count (first_name) from student group by region order by region;
7. Change the City to Bangalore for the person whose name is Naina.
Update student set city=‘Bangalore’ where first_name=‘Naina’
8. Change the Region and fees paid to West and 35000 for the person whose name is Anil.
Update student set region=‘West’ ,fees=35000 where first_name=‘Anil’ ;
9. Delete record of Priya Agrawal
Delete from student where first_name=‘Priya’ and Last_name =‘Agrawal’;
10. Delete the records of people from Pune.
Delete from student where city=‘Pune’;
11.Display the average fees paid in every city where average fees is more than 20000
Select city, average (fees) from student group by city having average(fees) > 20000;
12. Display first name and last name of all the students with first name starting with A or last name starting with A.
Select first_name, Last_Name from student where first_name like ‘A’ or Last_Name like ‘A’;
13. Change the marks and city to 45 and Chennai for person with age from 30 to 40
Update student set marks=45, city= ‘Chennai’ where age between 30,40;
• Master file – permanent nature (name, last name, dob, mobile nu,
email)
• Transaction file- volatile data , changes frequently – (attendance,
marks in various tests)
• Having is also another clause
• Which is used for giving condition in group by
• Usually conditions are given using where class
• But in group by u can use having
13. Display all the records of students in descending order of their age
Select * from student order by age desc ;
14. Display all the records of students alphabetically arranged first names.
Select * from student order by name ;
15. Display the total of individual student by cacluating total of mngmnt marks and it marks (table structure
student – rollno, name, city, age, mngmnt_marks, it_marks)
Select mngmnt_marks/2 + it_marks/2 as halftotal from student ;
halftotal
23
45
Relationship
• There are 2 types of tables
• Master – contains permanent types of data, (data deosn’t change very
frequently)
• Transaction (contains data which changes or updates frequently)
To establish relationship between 2 tables we need to have one common
column
The common column is primary key in master table and foreign key in
transaction table.
Referential Integrity
• For a primary key in master table there can or can not be matching
record in transaction table
• If a record gets deleted from master file the record should be deleted
from transaction file as well
• If a record gets updated from master file the record should be
updated from transaction file as well
• If a record is added in transaction file it is must that it MUST have a
matching record in master file.