The ALTER command is used to modify existing database tables. It allows adding, dropping, or modifying columns and constraints. Examples show using ALTER to add a primary key, drop a unique constraint, modify a check constraint, add and modify columns, drop columns, and rename a table and column.
The ALTER command is used to modify existing database tables. It allows adding, dropping, or modifying columns and constraints. Examples show using ALTER to add a primary key, drop a unique constraint, modify a check constraint, add and modify columns, drop columns, and rename a table and column.
• It is used to add, delete or modify columns in an existing table.
• Also used to add and drop various constraints on an existing table. • Ex: CREATE TABLE Emp(emp_id NUMBER, emp_name varchar2(10) UNIQUE, salary NUMBER CHECK(Salary >=5000), phone varchar2(13), pincode varchar2(6)); • ALTER TABLE Emp ADD PRIMARY KEY(emp_id); • ALTER TABLE Emp DROP UNIQUE(emp_name); • ALTER TABLE Emp MODIFY CHECK(salary >= 10000); • ALTER TABLE Emp ADD(address varchar2(30), dob date); • ALTER TABLE Emp MODIFY(phone varchar2(20), emp_name varchar2(30)); • ALTER TABLE Emp DROP(pincode, address); • ALTER TABLE Emp RENAME COLUMN phone TO mobile; • ALTER TABLE Emp RENAME to Employee; or RENAME Emp to Employee; SELECT • Used to retrieve data from database • Syntax: SELECT <attr_list> FROM <table_list> WHERE <conditions>; • attr_list: List of attributes whose values are to be retrieved by the query • table_list: List of tables from which we retrieve the data • condition: Conditional [Boolean] expressions that identifies the rows retrieved by the query SELECT
dept_id dept_name emp_id dept emp_name Job
1 CE 1 5 Abhay a 2 IT 2 2 Kunal b 3 EC 3 1 Chintan a 4 CH 5 IC 4 4 Hardik a 5 2 Prapti b 6 3 Zarana c SELECT • Q: Retrieve the names of the employees who works for department 1 ➢ SELECT emp_name FROM Emp WHERE dept = 1;
• Q: Retrieve the names, emp_id of the employees who works for IT dept. ➢ SELECT emp_id, emp_name FROM Emp, Dept WHERE dept = dept_id AND dept_name = ‘IT’;
• Retrieve the jobs of all the employees
➢SELECT job FROM Emp; ➢SELECT DISTINCT job FROM Emp; Aliasing • SQL Aliases are used to temporarily rename a table (or) a column in a table.
Emp_id dept_id Emp_name Sup_id Sal
Dept_id Dept_name 1 Accounting 1 1 Abhay 7 50000
2 Sales 2 3 Kunal 4 55000
3 Research 3 1 Chintan 7 59000 4 Finance 4 4 Hardik 7 50000 5 2 Prapti 4 35000 6 1 Zarana 7 90000 7 1 Mohit NULL 30000 Aliasing • Q: For each employee retrieve employee’s id, name, salary and the name of his/her immediate supervisor. ➢ SELECT E.Emp_id AS “Employee_id”, E.Sal AS “Salary”, E.Emp_name AS “Employee_name”, S.Emp_name AS “Supervisor_name” FROM Emp E, Emp S WHERE E.Sup_id = S.Emp_id; Aliasing • Q: Retrieve the names of the employees and their corresponding name of the department. ➢ SELECT E.Emp_name AS “Employee_name”, D.Dept_name AS “Department_name” FROM Emp E, Dept D WHERE E.dept_id = D.Dept_id; Arithmetic Operators • +, -, *, / • SQL Allows the use of arithmetic operators in queries on numeric domains. ❖Increase the salary of the employee by 10 percent and display the salary and employee name. ➢SELECT Emp_name, Sal*1.1 FROM Emp; • CONCATENATE • For string data type, the concatenate operator ‘||’ can be used in a query to append two string values. ➢SELECT FNAME || LNAME AS “FULL_NAME” FROM Emp;