Sie sind auf Seite 1von 38

SQL

Data definition language


SQL includes commands to create
Database objects such as tables, indexes, and views Commands to define access rights to those database objects

Data manipulation language


Includes commands to insert, update, delete, and retrieve data within the database tables

SQL Data Types

SQL> create table product(p_price number(7,2)); Table created. SQL> insert into values('&p_price); Enter value for p_price: 1234567 old 1: insert into values('&p_price) new 1: insert into values('1234567) ERROR: ORA-01756: quoted string not properly terminated

SQL> insert into product values('&p_price'); Enter value for p_price: 1234567 old 1: insert into product values('&p_price') new 1: insert into product values('1234567') insert into product values('1234567') * ERROR at line 1: ORA-01438: value larger than specified precision allows for this column

SQL> / Enter value for p_price: 12345 old 1: insert into product values('&p_price') new 1: insert into product values('12345') 1 row created. SQL> / Enter value for p_price: 123.1234567 old 1: insert into product values('&p_price') new 1: insert into product values('123.1234567') 1 row created. SQL> / Enter value for p_price: .123456 old 1: insert into product values('&p_price') new 1: insert into product values('.123456') 1 row created.

SQL> / Enter value for p_price: +1234.12345 old 1: insert into product values('&p_price') new 1: insert into product values('+1234.12345') 1 row created.

SQL> / Enter value for p_price: -123.123456 old 1: insert into product values('&p_price') new 1: insert into product values('123.123456')
1 row created. //////////////output///////////////////// SQL> select * from product; P_PRICE ---------12345 123.12 .12 1234.12 -123.12

Number data type


SQL> create table product1(p_price number,p_cost number(3)); Table created. SQL> insert into product1 values(123456789,12); SQL> / 1 row created. /*********output**********/ SQL> select * from product1; P_PRICE P_COST ---------------- -------123456789 12 SQL> insert into product1 values(1234,123); 1 row created. SQL> insert into product1 values(1234,1234) 2 ; insert into product1 values(1234,1234) * ERROR at line 1: ORA-01438: value larger than specified precision allows for this column

Data type DATE()

Description

DATE TIME

A date. Format: DD-MM-YYYY Note: The supported range is from '1000-01-01' to '9999-12-31' *A date and time combination. Format: DD-MM-YYYY HH:MM:SS Note: The supported range is from '1000-01-01 00:00:00' to '9999-1231 23:59:59' *A timestamp. TIMESTAMP values are stored as the number of seconds since the Unix epoch ('1970-01-01 00:00:00' UTC). Format: DD-MM-YYYY HH:MM:SS Note: The supported range is from '1970-01-01 00:00:01' UTC to '2038-01-09 03:14:07' UTC A time. Format: HH:MM:SS Note: The supported range is from '-838:59:59' to '838:59:59'

DATETIME()

TIMESTAMP()

TIME()

*Even if DATETIME and TIMESTAMP return the same format, they work very differently. In an INSERT or UPDATE query, the TIMESTAMP automatically set itself to the current date and time. TIMESTAMP also accepts various formats, like YYYYMMDDHHMMSS, YYMMDDHHMMSS, YYYYMMDD, or YYMMDD.

DATE
SQL> create table employee(d_O_b date); Table created. SQL> insert into employee values('01-jan-99'); 1 row created. SQL> insert into employee values('01-feb-1999'); SQL> / 1 row created. SQL> select * from employee; D_O_B ------------01-JAN-99 01-FEB-99

timestamp
SQL> create table employee4(e_name varchar(20),s_date TIMESTAMP(7)) SQL> insert into employee4 values('deep','17-jun87 02.22.22') 1 row created. E_NAME S_DATE ---------- ------------Deep 17-JUN-87 02.22.22.0000000 AM

SQL> create table employee5(e_name varchar(20),s_date TIMESTAMP(4)) SQL> / Table created. 1* insert into employee5 values('deep','17-jun-87 12.22.22') SQL> / 1 row created. E_NAME S_DATE ------------ ---------------deep 17-JUN-87 12.22.22.0000 PM

MySQL Date Functions


Function NOW() CURDATE() CURTIME() DATE() EXTRACT() DATE_ADD() DATE_SUB() DATEDIFF() DATE_FORMAT() Description Returns the current date and time Returns the current date Returns the current time Extracts the date part of a date or date/time expression Returns a single part of a date/time Adds a specified time interval to a date Subtracts a specified time interval from a date Returns the number of days between two dates Displays date/time data in different formats

Different use of Alter table


CREATE TABLE EMP1( ecode number(3)ename varchar2(50) ); /*********************Add column ************

Alter table emp1 add ( age number(3));


/********drop column**********

Alter table emp1 drop column age;


/*********add constraint *********

Alter table emp1 add primary key (ename);


Alter table emp1 add primary key (ecode,ename); //sim.. Alter table emp1 add UNIQUE(ename); //change column name (rename) Alter table emp1 rename column ecode to e_id ;

/********************Modify column ****************** SQL> desc emp1; Name Null? Type ----------------------------------------- -------- --------------ECODE NUMBER(3) ENAME VARCHAR2(50) AGE NUMBER(3)

SQL> Alter table emp1 modify ( ename varchar2(100)); Table altered. SQL> desc emp1; Name Null? Type ----------------------------------------- -------- --------------ECODE NUMBER(3) ENAME VARCHAR2(100) AGE NUMBER(3) SQL> Alter table emp1 modify ( ename int); Table altered. SQL> desc emp1; Name Null? Type ----------------------------------------- -------- ---------------------------ECODE NUMBER(3) ENAME NUMBER(38) AGE NUMBER(3)

Drop column
emp_id emp_name e_post e_salary e_age ----------- ---------- ---------- ----------- --------- --------111 shraddha manager 150000 33 222 sagar clerk 5000 24 333 sarvesh security 5000 33 444 raj cashier 50000 32 /* drop cloumn */ alter table employee drop column e_age

/* output of above query emp_id emp_name e_post e_salary ----------- ------------------- ----------111 shraddha manager 150000 222 sagar clerk 5000 333 sarvesh security 5000 444 raj cashier 50000

delete from employee where emp_name='sagar;


select * from employee /* output of above query emp_id emp_name e_post e_salary ----------------------------- ----------111 shraddha manager 150000 333 sarvesh security 5000 444 raj cashier 50000

Diff. clause (Logical,relational.)


create table employee(emp_id int primary key,emp_name varchar(10),e_post varchar(10),e_salary int); emp_id emp_name e_post e_salary -------------------- ---------- ----------111 shraddha manager 150000 222 sagar clerk 5000 333 sarvesh security 5000 444 raj cashier 50000 /* and clause */ select emp_name,e_salary from employee where e_salary>500 and e_salary<=150000 ; /* output of above query emp_name e_salary -------------------shraddha 150000 sagar 5000 sarvesh 5000 raj 50000

/* as clause */ select e_salary as e_s1 from employee /* output of above query e_s1 ----------150000 5000 5000 50000

/* distinct (eliminate duplicates)

*/

select distinct e_salary from employee /* output of above query

e_salary ----------5000 50000 150000

/* String operations */
create table employee(emp_id int primary key,emp_name varchar(10),e_post varchar(10),e_salary int);
EMP_ID EMP_NAME E_POST E_SALARY ---------- ---------------------------111 shraddha manager 150000 222 sagar clerk 5000 333 sarvesh security 5000 444 raj cashier 50000 SQL> select * from employee where emp_name like 'sa%; EMP_ID EMP_NAME E_POST E_SALARY ---------- ---------------------------222 sagar clerk 5000 333 sarvesh security 5000

select * from employee where emp_name like '%sa%


EMP_ID EMP_NAME E_POST ---------- ------------------222 sagar clerk 333 sarvesh security E_SALARY ---------5000 5000

select * from employee where emp_name like _ _ _


EMP_ID EMP_NAME E_POST E_SALARY ---------- ---------------------------444 raj cashier 50000

select * from employee where emp_name like '_ _ _ _'


no rows selected //
select * from employee where emp_name like '_ _ _%' EMP_ID EMP_NAME E_POST ---------- ------------------111 shraddha manager 222 sagar clerk 333 sarvesh security 444 raj cashier E_SALARY ---------150000 5000 5000 50000

select * from employee where emp_name like '_a%' EMP_ID EMP_NAME E_POST E_SALARY --------------------------------222 sagar clerk 5000 333 sarvesh security 5000 444 raj cashier 50000

1* select * from employee where emp_name like '_ _ _ _ _a%' SQL> / no rows selected 1* select * from employee where emp_name like '_ _ _a_%' SQL> /
EMP_ID EMP_NAME E_POST

E_SALARY ---------- ---------------------------111 shraddha manager 150000 222 sagar clerk 5000 1* select * from employee where emp_name like '_ _ _ _a_%' SQL> / no rows selected

Not like
select * from employee where emp_name not like '%s% ; /* output of above query
emp_id emp_name e_post e_salary

----------- ---------- ---------- ----------444 raj cashier 50000

/* aggregation function */
emp_id emp_name ------------------111 shraddha 222 sagar 333 sarvesh 444 raj e_post e_salary e_age ----------- ----------- --------manager 150000 33 clerk 5000 24 security 5000 33 cashier 50000 32

/* aggregation function sum */


select sum(e_salary) from employe
/*output of above query ----------210000

/* aggregation function avg */ select avg(e_Salary) from employe /*output of above query
----------52500

/* aggregation function max,min */ select max(e_Salary),min(e_salary) from employe /*output of above query
----------- ----------8640 40

/* aggregation function count */


//it count all rowin table select count(*)from employe ----------4 //it count all rows in table that have non null value for column name select count(e_salary)from employe /* rows table have not null value */ ----------4

Employee table
EMPNO ENAME JOB MGR SAL COMM DEPTNO ---------20 30 30 20 ---------- ---------- --------7369 SMITH CLERK 7499 7521 7566 ALLEN SALESMAN WARD SALESMAN JONES MANAGER ---------- --------- ---------7902 800 7698 7698 7839 1600 1250 297 1 300 500

14 rows selected.

Write down the SQL (SELECT..from..where) queries based on emp & dept table a) Write a query to display ename and salary of employees whose salaryis greater than or equal to 2200. b) Write a query to display employee name and salary of those employees who do not have their salary in range of 2500 to 4000. c) Write a query to display details of employee who are not getting commission from table emp. d) Write a query to display name, job, tittle and salary of employee who do not have manager. e) Write a query to display name of employee whose name contain A as third alphabet. f) Write a query to display name of employee whose name contain T as last alphabet. g) Write a query to display name of employee whose name contain M as first alphabet and L as third alphabet.

h) List employee name and their department number for employee number between 4000 and 5000. i) List the minimum salary and maximum salary of each job type. j) Show the average salary for all departments with more then three people for a job. k) Display only the jobs with minimum salary greater than or equal to 3000. l) Find out the number of employees having manager as job. m) Find the average salary for each job type, remember that salesman earns commission. n) Show the average salary for all departments for more then three people for a job. o) List the count of employee grouped by department number. p) List the sum of employees, salaries grouped by department. q) List the maximum salary of employee grouped by their department number.

a) Write a query to display ename and salary of employees whose salary is greater than or equal to 2200.

Query : SQL> select ename,sal from emp where sal>=2200;

ENAME ---------JONES BLAKE CLARK SCOTT KING FORD

SAL ---------2975 2850 2450 3000 5000 3000

b) Write a query to display employee name and salary of those employees who do not have their salary in range of 2500 to 4000.

Query: SQL> select ename,sal from emp where sal not between 2500 and 4000; ENAME ---------SMITH ALLEN WARD MARTIN CLARK KING TURNER ADAMS JAMES MILLER SAL ---------800 1600 1250 1250 2450 5000 1500 1100 950 1300

c) Write a query to display details of employee who are not getting commission from table emp.

Query: SQL> select * from emp where comm is NULL; EMPNO ENAME JOB MGR HIREDATE SAL ---------- ---------- --------- ---------- --------- ---------- ---------DEPTNO ---------7369 SMITH CLERK 7902 17-DEC-80 800 20 7566 JONES 20 7698 BLAKE 30 MANAGER 7839 02-APR-81 2975 COMM

MANAGER

7839 01-MAY-81

2850

d) Write a query to display name, job, tittle and salary of employee who do not have manager.

Query: SQL> select empno,ename,job,sal from emp where mgr is null; EMPNO ENAME JOB SAL ---------- ---------- -----------------7839 KING PRESIDENT 5000

e) Write a query to display name of employee whose name contain A as third alphabet. Query: SQL> select ename from emp where ename like '__A%'; ENAME ---------BLAKE CLARK ADAMS

f) Write a query to display name of employee whose name contain T as last alphabet. Query: SQL> select ename from emp where ename like '%T';

ENAME ---------SCOTT
g) Write a query to display name of employee whose name contain M as first alphabet and L as third alphabet. Query: SQL> select ename from emp where ename like 'M_L%';

ENAME ---------MILLER

Das könnte Ihnen auch gefallen