The document discusses various SQL commands used for database and table operations. It covers DDL commands like CREATE, ALTER, and DROP used to create, modify and delete database objects. It also discusses DML commands like INSERT, UPDATE, DELETE used to manipulate data. Additional clauses like WHERE, ORDER BY, DISTINCT are described. The document also briefly mentions data control language commands like GRANT and REVOKE used to manage privileges.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
11 views
SQL DAY-2
The document discusses various SQL commands used for database and table operations. It covers DDL commands like CREATE, ALTER, and DROP used to create, modify and delete database objects. It also discusses DML commands like INSERT, UPDATE, DELETE used to manipulate data. Additional clauses like WHERE, ORDER BY, DISTINCT are described. The document also briefly mentions data control language commands like GRANT and REVOKE used to manage privileges.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23
DAY-2
CHAPTER 3
WORKING WITH DATABASES
AND TABLES DDL commands (Data Definition Language) • DDL statements are used to create, drop, or Alter the objects of a database. DDL statements • CREATE DATABASE • CREATE TABLE • CREATE INDEX • CREATE PROCEDURE • CREATE VIEW • CREATE TRIGGER • ALTER TABLE • ALTER TRIGGER • ALTER VIEW • ALTER PROCEDURE • DROP DATABASE • DROP TABLE • DROP TRIGGER • DROP VIEW • DROP PROCEDURE Create database Syntax: CREATE DATABASE database_name Example CREATE DATABASE mydb1 (Or) Database right click new data base enter database name click ok Create table CREATE TABLE command creates a new table in the current database. Syntax: CREATE TABLE table_name (column_name_1 data_type [column_attributes],[column_name_2 data_type [column_attributes] ] …………) Example: CREATE TABLE employee (Empid int, empname varchar (25), DOJ datetime, deptno int, job varchar (20), salary numeric (9, 2))
Viewing table structure
EXEC sp_help <tablename> Example: EXEC sp_help employee Querying the database Alter table ALTER TABLE statement is used to modify an existing table. We can use this statement to add columns or constraints, Drop columns or constraints or change of an existing column, including changing the column’s Data Type. To add a new column to a table: ALTER TABLE tablename ADD new_column_name datatype [column attributes],…. Adding a new column named gender to employee table: ALTER TABLE employee ADD gender char (1) When we add a new column to a table, it is placed to the right of the last existing column. All fields in new column are initially NULL or default value if any default value is assigned. To add more than one column, use commas to separate each column from the next. To drop an existing column: • ALTER TABLE tablename DROP COLUMN_NAME • ALTER TABLE employee DROP COLUMN deptno. To modify an existing column: • ALTER TABLE tablename ALTER COLUMN Column_name new_data_type [null|NOT NULL] Increasing the name column’s with • ALTER TABLE tablename ALTER COLUMN empname varchar (30) Drop table Tables that are not required can be deleted. DROP table command removes a table from the database. When a table is dropped from a database, all the Data from the table is deleted. syntax DROP TABLE tableName Example Drop table items DML Commands Adding row to a table We can use INSERT statement to add a new row to a table. Syntax • INSERT INTO table_name VALUES (value1, value2,….) You can also specify the columns for which you want to insert data: • INSERT INTO table_name (column1, column2,…) VALUES (value1, value2,…) Example To provide values for all the columns in the table: • INSERT INTO employee VALUES (111,’arun’, ‘5-nov-2003’, 10,’analyst’, 10500) Select statement SELECT COMMAND retrieves rows from the database and allows the selection of one or many rows or columns from one or many tables. Syntax: SELECT select_list FROM table_source • Select clauses • WHERE • DISTINCT • GROUP BY • HAVING • ORDER BY • ASC| DESC • COMPUTE • INTO • TOP • ALL Example: • SELECT * from employee • SELECT empname, job, salary from employee Displaying specific columns with new column headings Syntax • SELECT column_name column_heading,…….. FROM tablename (or) • SELECT column_name as columnheading,…… FROM tablename • SELECT empid ‘employee id’, job ‘designation’ from employee Top clause To limit the number of rows retrieved by a SELECT statement. Syntax SELECT TOP [n | percent] column_list FROM tablename Examples To retrieve the first 5 records from an employee table • SELECT TOP 5 * from employee To retrieve the 5 percent of the total number of records from employee table • SELECT TOP 5 PERCENT * from employee WHERE clause Used to display selected rows from a table based on condition Syntax SELECT select_list FROM table_name WHERE condition With the WHERE clause, the following operators can be used: OPERATORS Operator Description = Equal <> Not equal > Greater than < Less than >= Greater than or equal <= Less than or equal BETWEEN Between an inclusive range Like Search for a pattern • Select * from employee WHERE job = ‘CLERK’ • Select empid, empname, job, salary from employee WHERE job = ‘CLERK’ Using comparison operators: To retrieve those employees whose salary is greater than or equal to 5000 • SELECT * from employee WHERE salary>=5000 To retrieve those employees who have joined before May 2013 • SELECT * from employee WHERE salary>= 5000 To display those employees those who have joined before May 2003 • SELECT * from employees WHERE doj < ‘1-May-2003’ To display those employees who are not working in department 20 • SELECT * from employees who are not working in department 20 • SELECT * from employee WHERE deptno <> 20 Using logical operators: Using OR: • SELECT * from employee where job=’CLERK’ OR job=’”MANAGER” • SELECT empid, empname, deptno, salary from employee WHERE salary>4000 OR deprno=30 Using AND: • SELECT * from employee WHERE salary >= 6000 AND salary <= 10000 Using between: • SELECT * from employee WHERE salary BETWEEN 6000 AND 10000 • SELECT * from employee WHERE doj between ‘1- April-2002’ and ’30-march-2003’ USING IS NULL and IS NOT NULL: • SELECT empname , job from employee WHERE deptno IS NULL • SELECT empname , job from employee WHERE deptno IS NOT NULL. Using IN • SELECT * from employee WHERE job IN (‘CLERK’, ‘MANAGER’) • SELECT * from employee WHERE job NOT IN (‘CLERK’, ‘MANAGER’) Using LIKE • Select * from employee where empname LIKE ‘A%’ (ie.. Anu, Aravind , Akash) • Select * from employee where empname LIKE ‘BAL_’ (ie.. BALA, BALU etc) • Select * from employee where empname LIKE ‘A[NRK]%’ (ie.. Anand, Aravind, Akash, Anu) • Select * from employee where empname LIKE ‘A[B-K]%’ (ie.. Abirami , Akash, Adam, Afridi, Akila) Updating Data in a table: The UPDATE statement change Data in Existing Rows in a Table. We can UPDATE statement to modify the contents of single rows, group of rows or all the rows of a table. Syntax: • UPDATE table_name SET column_name1 = expression, [, column_name2 = expression2] ……….[WHERE condition] Example: • UPDATE employee SET Deptno = 40 WHERE epid=121 • UPDATE employee SET salary = salary + 500 WHERE JOB=’Accountant’ • UPDATE employee SET salary = salary + 250 • UPDATE employee SET empname =’Reena’,DOJ = ’13- Jun-2004’, Deptno = 20 WHERE empid=113 Deleting Data from a Table DELETE Statement is used to remove specific rows from a table or all rows from a table. Example: • Delete from employee WHERE empid = 111 • Delete employee WHERE Doj < ‘1-April-2003’ • Delete from employee Truncate table Truncate table statement removes all rows from a table. Truncate table is always faster than DELETE statement. Syntax: TRUNCATE TABLE table_name Example: • TRUNCATE TABLE items Order by clause SELECT select_list FROM tablename ORDER BY column_name | column_number ASC | DSC • SELECT empname, job, salary from employee ORDER BY salary ASC • Select empname, job, deptno, salary from employees ORDER BY 3 DESC Ordering by multiple columns:- • Select from employees ORDER BY empname, salary Eliminating duplicating rows: DISTINCT clause DISTINCT keyword is used to eliminate DUPLICATE rows from a query’s output SELECT DISTINCT Job from employees. Syntax: SELECT [ALL | DISTINCT] select_list FROM tablename WHERE condition. DATA CONTROL LANGUAGE (DCL) The DML language statements i.e., Insert, Delete, Update are effected i.e., written in the database unless the user specifically says so. PRIVILEGES: Privileges are the right to access another user’s object. If two users 1 and user 2 are present and if user 2 wishes to access user1’s objects then he should get permission from user1. Giving permission is called as granting and withdrawing the permission is called as revoking. We can grant privileges (insert, select, delete, update) to others and can also withdraw these privileges using Grant privilege command and Revoke privilege command respectively. GRANT PRIVILEGE COMMAND: Object privilege such as grant, insert, delete, and update can be granted to other users using the SQL command GRANT.