sql
sql
to craete database:
craete database database_name;
create database sample;
to drop database:
drop database database_name;
drop database sample;
to backup database:
backup database database_name to disk ='file path';
BACKUP DATABASE sample TO DISK = 'D:\backups\sample;.bak';
to create table:
create table table_name(column1 datatype,column2 datatype,column3 datatype,....);
create table sample(id int,name varchar(10),salaray number,....);
to drop table:
drop table table_name; //The DROP TABLE statement is used to drop an existing
table in a database.
drop table sample;
or
TRUNCATE TABLE table_name; //The TRUNCATE TABLE statement is used to delete the
data inside a table, but not the table itself.
TRUNCATE TABLE sample;
to alter table: //The ALTER TABLE statement is used to add, delete, or modify
columns in an existing table.
The ALTER TABLE statement is also used to add and drop various constraints on an
existing table.
ALTER TABLE table_name ADD column_name datatype;
alter table sample add state varchar(10);
4.renaming a column:
alter table testing rename column sno to snumber;
-->SQL constraints are used to specify rules for the data in a table.
The following constraints are commonly used in SQL:
CREATE TABLE Persons ( ID int NOT NULL,LastName varchar(255) NOT NULL,Age int);
*The NOT NULL constraint enforces a column to NOT accept NULL values.
*The UNIQUE constraint ensures that all values in a column are different.
ex:CREATE TABLE Persons ( ID int NOT NULL UNIQUE,LastName varchar(255) NOT NULL,Age
int);
*The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables.
A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the
PRIMARY KEY in another table.
CREATE TABLE Orders (OrderID int NOT NULL,PersonID int,PRIMARY KEY
(OrderID),FOREIGN KEY (PersonID) REFERENCES Persons(PersonID));
*The CHECK constraint is used to limit the value range that can be placed in a
column.
If you define a CHECK constraint on a column it will allow only certain values for
this column
CREATE TABLE Persons (ID int NOT NULL, Age int,CHECK (Age>=18));
A view contains rows and columns, just like a real table. The fields in a view are
fields from one or more real tables in the database.
In MySQL there are three main data types: string, numeric, and date and time.
->CHAR(size)
A FIXED length string (can contain letters, numbers, and special characters). The
size parameter specifies the column length in characters - can be from 0 to 255.
Default is 1
->VARCHAR(size)
A VARIABLE length string (can contain letters, numbers, and special characters).
The size parameter specifies the maximum column length in characters - can be from
0 to 65535
->BINARY(size)
->TEXT(size)
->BLOB(size)
->int
->float
->tinyint
->bool
->double
->bit
->date
->datetime
***********************************************************************
--->The SQL SELECT Statement
The SELECT statement is used to select data from a database.
SELECT Syntax:
SELECT column1, column2, ...FROM table_name;
SELECT CustomerName, City FROM Customers;
Inside a table, a column often contains many duplicate values; and sometimes you
only want to list the different (distinct) values.
WHERE Syntax
SELECT column1, column2, ...FROM table_name WHERE condition;
SELECT * FROM Customers WHERE Country='Mexico';
SELECT * FROM Customers WHERE CustomerID=1;
The AND and OR operators are used to filter records based on more than one
condition:
The AND operator displays a record if all the conditions separated by AND are TRUE.
The OR operator displays a record if any of the conditions separated by OR is TRUE.
The NOT operator displays a record if the condition(s) is NOT TRUE.
AND Syntax
SELECT column1, column2, ...FROM table_name WHERE condition1 AND condition2 AND
condition3 ...;
SELECT * FROM Customers WHERE Country='Germany' AND City='Berlin';
SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... ASC|
DESC;
SELECT * FROM Customers ORDER BY Country;
SELECT * FROM Customers ORDER BY Country DESC;
IS NOT NULL:
to check the particular column contains not null value.
select name from emp1 where salary is not null;
UPDATE Syntax
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
DELETE Syntax
DELETE FROM table_name WHERE condition;
it delete the row from table
delete from emp1 where name='aaa'
The MAX() function returns the largest value of the selected column.
The COUNT() function returns the number of rows that matches a specified criterion.
COUNT() Syntax
SELECT COUNT(column_name) FROM table_name WHERE condition;
SUM() Syntax
SELECT SUM(column_name) FROM table_name WHERE condition;
create table emp1(id int not null , name varchar(20), age int,primary key(id));
insert into emp1(id,name,age)values(101,'hyder',25);
insert into emp1(id,name,age,salary)values(105,'aaa',28,20000);
alter table emp1 add salary number;
select name from emp1 where id=101;
select count(age) from emp1
select * from emp1
desc emp1
select max(salary) from emp1
select min(salary) from emp1
aggregate functions:
count
sum
avg
min
max
group by:
arrange the identical data into group
arrange similar type of data into group
select count(salary), age from emp1 group by age
having:
it is using in group by for the condition.
select count(salary), age from emp1 group by age having sum(salary)>1000
order by:
it will sort the result in ascending or decending order
IS NULL:
to check the particular column contains null value.
select name from emp1 where salary is null;
IS NOT NULL:
to check the particular column contains not null value.
select name from emp1 where salary is not null;
UPDATE:
it is used to modify the content of table
update emp1 set name='shabeer' where id=104
DELETE:
it delete the row from table
delete from emp1 where name='aaa'
IN OPERATOR:
it is used to specify multiple values in where cluase
select id,name,age from emp1 where city IN('Mangalore','Bangalore','Mysoor')
BITWEEN:
it is used to select particular vale within the specified range.
select name,age from emp1 where age between 25 and 26;
ALLIASES :
giving a temporary or another name to table and column.
table : employee
Query 1:
get all the records from employee table
select * from employee;
DDL:
create
alter
drop
DML:
insert
update
delete
DQL: SQL
DCL:
grant
revoke
SQL (Structure query language)
trying to retrieve information from database
Database:
collection of:
tables
triggers
indexes
views
Stored procedures
functions
packages
types
sequences
========================
select
count
where
or
in
not in
is not null
max
min
avg
as
select min(sal) from (select distinct sal from emp order by sal desc)
where rownum<&n;
key words:
select
count
where
or
in
not in
is not null
max
min
avg
as
distinct
asc
desc
order by
rownum
group by
update:
this will use to update the existing data
update single column value:
update emp set sal=10000 where empno=7902;
update multiple column values:
update emp set sal=20000,ename='FORDD' where empno=7902;
desc table;
this command will give you the details of the table
Control + left mouse button: this will also give all the details
insert:
insert into emp values (8000,'Shahbaz','MANAGER',7839,sysdate,45000,6000,30);
commit;
select * from employee;
data types:
number
varchar2
varchar
date
integer
create:
create table employee (
eno number ,
ename varchar2(20),
esal number,
dept number,
ejobrole varchar2(20));
2.
Create:
create table student(
sno number primary key,
sname varchar2(20) NOT NULL,
saddress varchar2(50));
alter table:
when we wanted to:
-> add a new column
-> modifying a existing column
-> delete a column
alter table student add(mobile number);
ex:
alter table testing add(smobile number unique
4.renaming a column:
alter table testing rename column sno to snumber;
drop:
this command will drop the table (delte the table along with the data)
syntax:
drop table student;
delete : delete will delte the rows one by one (will take lot of time)
truncate: (actual purpose is to delete data)
-> delete the table (data and table will be deleted)
-> recreate a fresh table
DCL:
grant and revoke
ex:
grant insert,update on testing to training;
revoke insert,update on testing from training;
grant insert,update on employee to testing
revoke insert,update on employee from testing
Grant and revoke will make sure the data security in database,
generally this task belongs to DBA.
View:
is nothing but a virtual table
snapshot of a table
part of a table
syntax:
1. create view stu_view as
select * from student;
2. create view stu_view as
select sno,sname from student;
3. create view stu_view as
select sno,sname from student where sno <5;
Dropping a view:
drop view stu_view;
Triggers:
A trigger is a stored procedure which automatically
invokes/runs whenever a special event happens in
the database.
special event:
before insert/update/delete
after insert/update/delete
normalization:
-> restructuring the relational database.
-> to reduce the duplicate entries (redundency)
-> improve the data integrity
-> we can maintain data hiding,security
Joins:
joining/combining the data from 2 or more tables
2 tables: table1,table2
select e.empno,e.ename,e.mob,e.age,d.deptname
from emp e,dept d
where e.deptcode=d.deptcode;
Syntax:
2.
create index stu_ind
on student(eno,ename)
exec myproc;
Indexes:
To delete duplicate records in SQL, you can use the DELETE statement with a
subquery that identifies the duplicates. Here's an example:
Suppose you have a table named mytable with columns id, name, and email, and you
want to delete duplicate records based on the email column. You can use the
following SQL statement:
sql
Copy
DELETE FROM mytable
WHERE id NOT IN (
SELECT MIN(id)
FROM mytable
GROUP BY email
);