SQL Concepts
SQL Concepts
SQL- CONCEPTS
******************************************
//concept 1 how to create table
******************************************
create table table_name{
column1 datatype,
column2 datatype,
column3 datatype,
......
}
*************************************************************************
//concept 1 how we will insert data in table
*************************************************************************
INSERT INTO table_name(column1,column2,column3,...)
VALUES(value1,value2,value3.....);
*************************************************************************
//concept 1 insert multiple rows in the table.
*************************************************************************
*************************************************************************
constraint means restriction
*************************************************************************
//concept 1 constaints in database
1.NOTNULL
2.check(age>18)
3.unique
4.default 'xyz'
5.primary key
6.foreign key
*************************************************************************
//concept 1 select some column with data from the required table
SELECT column1,column2,column3.....
FROM table_name;
//concept 2 select all column with data from the required table
SELECT *
FROM table_name;
//concept 5 we can also change the name of the column with the keyword as,
select name as Student,phone as Mobile_Number from personal;
select column1,column2,column3,column4,......
from table_name
where condition;
*************************************************************************
AND OR NOT Operator
*************************************************************************
select column1,column2,column3,....
from table_name
where condition1 AND condition2 AND Conditon3....
//concept for OR
select column1,column2,column3,....
from table_name
where condition1 OR condition2 OR Conditon3....
select column1,column2,column3,....
from table_name
where NOT condition1
*************************************************************************
IN operator
*************************************************************************
//concept 1 IN operator to specify multiple possible value for a column.
we use it where we need to check multiple value/record for a column
SELECT column1,column2,column3...
FROM table_name
where column_name IN (value1,value2,....)
*************************************************************************
BETWEEN, NOT BETWEEN operator
*************************************************************************
//concept between and not between operator
*************************************************************************
LIKE operator and wildcard characters
*************************************************************************
//concept 1 like operator and wildcard characters
we use two wild cards charecters '%' (percentage) and '_' (under score)
SELECT column1,column2,column3...
FROM table_name
WHERE column_name LIKE pattern
SELECT column1,column2,column3...
FROM table_name
WHERE column_name LIKE pattern
select * from student where name like "r%"; // it will display record from name column where value start with r or
R.
select * from student where BINARY name like "r%";//it will display record from name column where value start
with only r because of binary keyword.
select * from student where BINARY name like "R%";//it will display record from name column where value start
with only R because of binary keyword.
select * from student where name like "r%";//it will display record from name column where value start with either
r or R because i removed the binary keyword.
*************************************************************************
Regular Expression
*************************************************************************
//concept 1 REGEXP Regular Expression
*************************************************************************
Order by and Distinct
*************************************************************************
//sort in accending order ASC
SELECT column1,column2,column3....
FROM table_name
ORDER BY column1,column2,...ASC; //default
SELECT column1,column2,column3...
FROM table_name
WHERE condition1,condition2
ORDER BY column_name1,column_name2,column_name3.....ASC;
SELECT column1,column2,column3...
FROM table_name
WHERE condition1,condition2
ORDER BY column_name1,column_name2,column_name3.....DESC;
*************************************************************************
IS NULL and IS NOT NULL
*************************************************************************
//IS NULL and IS NOT NULL
SELECT column1,column2,column3...
FROM table_name
WHERE column IS NULL;
//example
SELECT *
FROM student
WHERE city IS NULL;
//concept 2 IS NOT NULL some i left some column value we can check it
SELECT column1,column2,column3...
FROM table_name
WHERE column IS NOT NULL;
//example
SELECT *
FROM student
WHERE city IS NOT NULL;
*************************************************************************
LIMIT and offset
*************************************************************************
//Limit and offset
as we know we select * from table name to show recode it will give you all the record.
if we need to show few recode of all recode then we can also do this with the help of LIMIT keyword.
//example
SELECT name
FROM student
LIMIT 3;
//Example
SELECT name
FROM student
WHERE age>17
LIMIT 3;
//Example
SELECT name
FROM student
WHERE age>17
ORDER BY name
LIMIT 3;
SELECT column1,column2,column3....
FROM table_name
WHERE condition
LIMIT offset,number;
//example
SELECT name
FROM student
LIMIT 2,3;(it will start after row 2 and three recode will display)
//Example
SELECT name
FROM student
WHERE age>17
LIMIT 1,2;(it will start after row 1 and 2 recode will display)
//Example
SELECT name
FROM student
WHERE age>17
ORDER BY name
LIMIT 4,2;(it will start ater row 4 and 2 recode will display )
*************************************************************************
COUNT sum min max avg function
*************************************************************************
//count sum min max avg function to perform operation recode.it will return a single row
//example
SELECT count(name)
FROM employee;
//example
SELECT count(*)
FROM employee;
//example
SELECT count(DISTINCT city)
FROM employee;
//example
SELECT MAX(percentage)
FROM employee;//but i dont know which employee get maximum number we can know by providing column_nam
e after max_function.
//example
SELECT MAX(percentage),name//but it will not working
FROM employee;
//example
SELECT MIN(percentage)
FROM employee;//but i dont know which employee get minimum number we can know by providing column_name
after min_function.
//example
SELECT MIN(percentage),name//but it will not working
FROM employee;
//example
SELECT sum(percentage) as total_marks
FROM employee;
//example
SELECT AVG(percentage) as AVERAGE
FROM employee;
*************************************************************************
Update table record or date or value
*************************************************************************
//Update table record or date or value
UPDATE table_name
SET column1=value1,column2=value2,column3=value3....
WHERE condition;
//example concept 2
UPDATE employee
SET city="Ranchi"
WHERE city='Bokaro';
//example concept 3
UPDATE employee
SET city="Dhanbad", salary=15000
WHERE id=7 OR id=8;
//example concept 3
UPDATE employee
SET name="ANIL",gender="M"
WHERE id=2
UPDATE employee//1
SET city="Jamshedpur",salary=43000
WHERE id=7;
UPDATE employee//2
SET city="Patna",salary=80000
WHERE id=8;
UPDATE employee//3
SET city="Dhanbad",salary=30000
WHERE id=9;
UPDATE employee//3
SET percentage=60 city='jamshedpur'
WHERE id IN(1,2,4,5,7);
*************************************************************************
COMMIT and ROLLBACK
*************************************************************************
//concept of commit and rollback
this keyword work with insert,update and delete commond
1. i want to update a student recode rehan ,but unfortunatly i make changes at different place then i need to revert/ro
llback it
for this i will use ROLLBACK commond. it will rollback all the recode you change .
2. if you do not want to rollback all the recode then you have to use COMMIT commond.
//important concept
NOTE:- if you update the table and commit then it will not able to rollback. Rollback only possible if the record is n
ot commited.
once you commit can't able to rollback.
//example
select * from employee;
UPDATE employee
SET name="Anil",age=26
WHERE id=3;
ROLLBACK;
//example
select * from employee;
//when i use rollback this common will not rollback because it is already commit
UPDATE employee
SET name="sohan",age=27
WHERE id=3;
commit;
//when i use rollback this common will rollback because it is after commit
UPDATE employee
SET name="Rohan",age=25
WHERE id=3;
ROLLBACK;
*************************************************************************
DELETE Record
*************************************************************************
//delete common we use to delete table Record/rows/values not table it self.
//example
DELETE FROM employee
WHERE id=6; //if you not use where clouse it will delete all the .
//concept 2 if you do not use where clouse it will delete all the Record.
DELETE FROM employee; //all the Record will remove from the table.
*************************************************************************
PRIMARY AND FOREIGN KEY
*************************************************************************
//primary key
1. primary key always have an unique data
2. a primary key can not have null value
3. A table can contain only one PRIMARY key constraints
//student table
//city table
CREATE TABLE CITY(
cid int NOT NULL AUTO_INCREAMENT
cname varchar(50) NOT NULL
PRIMARY KEY (cid)
);
//student table
CREATE TABLE student
(
id int PRIMARY KEY NOT NULL AUTO_INCREAMENT
name varchar(50) NOT NULL
age int NOT NULL
city varchar(50) NOT NULL
);
//city table
CREATE TABLE CITY
(
cid int PRIMARY KEY NOT NULL AUTO_INCREAMENT
cname varchar(50) NOT NULL
);
//concept Suppose if i alreadt have a table and i want to set PRIMARY key then how will i do that. for this i will ha
ve to alter table like that:
//syntax
ALTER TABLE student
ADD PRIMARY KEY (id);
//example 1.1
CREATE TABLE student
(
id int NOT NULL AUTO_INCREAMENT
name varchar(50) NOT NULL
age int NOT NULL
city varchar(50) NOT NULL
PRIMARY key (id)
FOREIGN KEY (city) REFERENCES CITY(cid)
);
//example 1.2
CREATE TABLE student
(
id int PRIMARY key NOT NULL AUTO_INCREAMENT
name varchar(50) NOT NULL
age int NOT NULL
city varchar(50) NOT NULL
FOREIGN KEY (city) REFERENCES CITY(cid)
);
//concept if we have an existing table if we want to set a FOREIGN KEY the how we will i do that.
//concept 1-inner join, if you have two table and you wants common data from both table then you have to use inner
join.
//syntax
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
//example 1.1
SELECT * from children INNER JOIN city ON children.city=city.cid;
//example 1.2
SELECT name,age,cname as City_name from children INNER JOIN city on children.city=city.cid;
//Intresting concept we can also use alise for table (means sort name for table)
SELECT * FROM children ch INNER JOIN city ci
ON ch.city=ci.cid;
SELECT ch.name,ch.age,ci.cname
FROM children ch INNER JOIN city ci
ON ch.city=ci.cid
where ci.cname='jamshedpur';
SELECT ch.name,ch.age,ci.cname
FROM children ch INNER JOIN city ci
ON ch.city=ci.cid
where ci.cname='Jamshedpur'
ORDER BY ch.name ASC;
//concept 1.2 use ORDER BY clause with Inner join we can also use IN operator
SELECT ch.name,ch.age,ci.cname
FROM children ch INNER JOIN city ci
ON ch.city=ci.cid
where ci.cname IN('Jamshedpur',"Rourkela")
ORDER BY ch.name ASC;
*************************************************************************
LEFT AND RIGHT JOIN
*************************************************************************
//Left and Right join
//concept 1 left join all Record from left table and common data from the right table.
//syntax
SELECT *
FROM table1
LEFT JOIN table2
ON table1.column_name(foreignkey)=table2.column_name(primarykey)
//example 1.2 with full table name we can also use where clause
SELECT *
FROM children
LEFT JOIN city
ON children.city=city.cid;
WHERE children.age>20;
//example 1.2 with full table name we can also use ORDER Clause clause
SELECT *
FROM children
LEFT JOIN city
ON children.city=city.cid;
WHERE children.age>20;
ORDER BY name;
//concept 1 right join all recode from right table and common data from the left table.
//syntax
SELECT *
FROM table1
RIGHT JOIN table2
ON table1.column_name(foreignkey)=table2.column_name(primarykey)
//example 1.2 with full table name we can also use where clause
SELECT *
FROM children
RIGHT JOIN city
ON children.city=city.cid;
WHERE children.age>20;
//example 1.2 with full table name we can also use ORDER Clause clause
SELECT *
FROM children
RIGHT JOIN city
ON children.city=city.cid;
WHERE children.age>20;
ORDER BY name;
*************************************************************************
CROSS JOIN
*************************************************************************
//concept of cross join it will join each row of the first table with
//every single row of the second table.
basically cross join make all possible combination between two table.
//syntax
SELECT column1,column2,column3...
FROM table1,
CROSS JOIN table2;
//syntax
SELECT column1,column2,column3...
FROM table1, table2;//it is also working same way just use common in place of cross join
//example 1.1
SELECT *
FROM children
CROSS JOIN city;
//example 1.2
SELECT ch.name,ci.cname
FROM children ch
CROSS JOIN city ci;
*************************************************************************
Join Multiple Table by Using INNER JOIN
*************************************************************************
//join multiple table we can also join multiple table by using inner join keyword again again...
//synax
SELECT column1,column2,column3.....
FROM table1
INNER JOIN table2 //this table2 is joined with first table
ON table1.column=table2.column
//Update table
Update college_course
SET course="B.Tech"
Where cr_id=1;
SELECT C.id,C.name,T.city
FROM College_name C INNER JOIN College_city T
ON C.c_course=T.c_id;
//example 1.1
//Example 1.3 select some column from all table and also use where clouse
*************************************************************************
GROUP BY and HAVING Clause
*************************************************************************
//Group by and Having clause
CONCEPT -> (HAVING) it is similar to where clause but it is used to perform conditon on the Result which will co
me out from the group by clause.Having clause come after group by clause.
//Syntax 1.1 with group by clause
SELECT Column1,Column2,Column3......,Aggregate(column1,column2,..)
FROM Table_name
Where Condition
Group By Column_name1,Column2....
SELECT column1,column2,column3....Aggregate(column1,column2,...)
FROM table1
INNER JOIN Table2
ON table1.column=table2.column
Where Condition
GROUP BY Column1,column2,...
SELECT column1,Column2,Column3...Aggregate(column1,column2,..)
FROM table1
INNER JOIN table2
ON Table1.column=Table2.column
WHERE Condition
Group BY Column1,column2....
Order By column1,column2...ASC / DESC
//Example 1.2 number of student comes from each city Order BY Count
SELECT T.c_id,T.city,Count(C.c_city) as Totale_student
FROM College_name C
INNER JOIN College_city T
ON C.c_city=T.c_id
GROUP BY c_city
ORDER BY Count(C.c_city);
//Concept having 1.1 clause it is used on the result come from group by clasue
SELECT column1,column2,column3...Aggaregate(column1,column2...)
FROM table_name
Group BY column1,column2,...
HAVING Condition1,condition2,....
//Concept having 1.2 clause it is used on the result come from group by clasue
SELECT column1,column2,column3...Aggaregate(column1,column2...)
FROM table_name
Where condition1,condition2....
Group BY column1,column2,...
HAVING Condition1,condition2,....
//Concept having 1.3 clause it is used on the result come from group by clasue
SELECT column1,column2,column3...Aggaregate(column1,column2...)
FROM table_name
Where condition1,condition2....
Group BY column1,column2,...
HAVING Condition1,condition2,....
ORDER BY Column1,column2.....
//Example 1.1
SELECT *,count(c_city)
FROM College_name
GROUP BY c_city
HAVING c_city>1;
//Example 1.2
SELECT C.name,T.city,count(C.c_city) as STUDENT
FROM College_name C
INNER JOIN College_city T
ON C.c_city=T.c_id
GROUP BY c_city
HAVING count(C.c_city)>1;
//Example 1.3
SELECT C.name,T.city,count(C.c_city) as STUDENT
FROM College_name C
INNER JOIN College_city T
ON C.c_city=T.c_id
GROUP BY c_city
HAVING count(C.c_city)>1;
ORDER BY name;
*************************************************************************
SUBQUERY and Exists and not exist
*************************************************************************
//Subquery ,Query inside an other query it will work with all others common like select insert delete update also.
//Syntex
SELECT column1,column2,column3....
FROM table1
WHERE column=
(SELECT column1,column2.. FROM table2 Where Condition1,Condition2....)
//Example 1.1 it return name where this course is running
SELECT name FROM College_name WHERE c_course=2;//here we do not know that what course 2 is represented
but by using subquery concept we can solve this problem .
/*it will not work because subquery will return multiple value for this we will use IN operator insted of equal (=) wit
h where clause in the first query.*/
//incorrect
SELECT name FROM College_name WHERE c_course = (SELECT cr_id FROM College_course WHERE course
="M.B.B.S" OR course="B.Tech");
//correct
SELECT name FROM College_name WHERE c_course IN (SELECT cr_id FROM College_course WHERE cours
e="M.B.B.S" OR course="B.Tech");
//Example 1.4 IN operator in subquery
SELECT name FROM College_name WHERE c_course IN (SELECT cr_id FROM College_course WHERE cours
e IN("M.B.B.S","B.Tech"));
//syntax 1
if subquery table2 return any single row then table1 will display the result.
SELECT column1,column2,column3,..
FROM table1
WHERE
EXISTS (SELECT column1,column2.. FROM Table2 WHERE Condition )
//syntax 2
if subquery table2 not return any row then table1 will display the result.
SELECT column1,column2,column3,..
FROM table1
WHERE
NOT EXISTS (SELECT column1,column2.. FROM Table2 WHERE Condition )
//EXample 1.1
SELECT name FROM College_name WHERE EXISTS (SELECT cr_id FROM College_course WHERE course="
MBA");
it will show all name of the first table because subquery condition is true.
//EXample 1.2
SELECT name FROM College_name WHERE NOTEXISTS (SELECT cr_id FROM College_course WHERE cour
se="M.Tech");
it will show all name of the first table because subquery condtion is true
*************************************************************************
IF and CASE---used to display result
*************************************************************************
//If,Case is used to display result based on the condition
NOTE:-
1.IF
if we need to show whether a student is fail or pass then we will use if.
if will take three parameter.if(Condition,TrueResult,FalseResult)
2.CASE
we need to show division for each student like first,second,third, then we will use case
we can also use CASE clause with UPDATE clause to update multiple record at once.
//syntax 2
SELECT column1,column2,....
IF(condition,true result,false result) as alias_name
FROM table_name;
where condition1,condition2,..
.
.
.
//example
SELECT id,name,percentage
IF(percentage>33,"Pass","Fail") as result
FROM employee;
SELECT column1,column2,column3,.....
CASE
when condition1 then result1
when condition2 then result2
when condition3 then result3
ELSE result4 as alias_name
END as alias_name
FROM table_name
SELECT id,name,percentage,
CASE
WHEN percentage>80 AND percentage<=100 then "merit"
WHEN percentage>60 AND percentage<=80 then "First division"
WHEN percentage>45 AND percentage<=60 then "Second division"
WHEN percentage>33 AND percentage<=45 then "third division"
WHEN percentage<33 then "Fail"
ELSE "NOT Correct % "
END as Grade
FROM employee;
SELECT id,name,percentage,
CASE
WHEN percentage>=80 AND percentage<=100 then "merit"
WHEN percentage>=60 AND percentage<80 then "First division"
WHEN percentage>=45 AND percentage<60 then "Second division"
WHEN percentage>=33 AND percentage<45 then "third division"
WHEN percentage<33 then "Fail"
ELSE "NOT Correct % "
END
FROM employee;
*************************************************************************
Arthmetic function
*************************************************************************
//Arthmetic function is used to perform mathmetical operation with column of the table.
we have many arithmetic function
1.CEIL
2.FLOOR
3.ROUND
4.ABS
5.POW
6.sqrt
7.rand
8.pi
9.sign
//example 1
select 4+5 as total;
select 4-5 as total;
select 4*5 as total;
select 4/5 as total;
select 4%5 as total;
select 4 ADD 5 as total;
select 4 SUB 5 as total;
select 4 MUL 5 as total;
select 4 DIV 5 as total;
select 4 MOD 5 as total;
//concept of CEIL
//example 2.1
//example 2.3
select id,name,percentage
FROM employee ORDER BY rand();
//concept of ceil
8.select pi() as Result;//result=3.1415
//concept of ceil
9.select sign(-38) as Result;//result=-1
select sign(38) as Result;//result=1
select sign(0) as Result;//result=0
*************************************************************************
STRING function
*************************************************************************
//string function
we have many string function we will see one by one
1. UPPER()/UCASE ()
2. LOWER()/LCASE ()
3. LENGTH()
4. CHAR_LENGTH()
5. CONCAT()
6. CONCAT_WS()
7. LTRIM()
8. RTRIM()
9. TRIM()
10.POSITION()
11.LOCATE()it will take three parameter and one parameter is opetional
12.INSTR()
//concet 1 upper
SELECT UPPEER(name) as Name from employee;
//concept 2 lower
SELECT LOWER(name) as Name FROM Employee;
//concept 3 length
SELECT name,length(name) FROM employee//here length will be in bytes
//concept 4 char_length
SELECT name,char_length(name) FROM employee;
//concept 5 concat(name,city)
SELECT concat(name,city) from employee;
SELECT concat(name," ",city) from employee;
SELECT concat(name,"_",city) from employee;
//concept 6 concat_ws(seprate,value1,valeu2)
SELECT concat_ws('_',name,city,gendder) from employee
output-> name_city_gender will be
//concept 10 instr('string','find');
SELECT instr("good morning to the friends","morning");
//String Function
some more string function
1. SUBSTRING()/SUBSTR()
2. MID()
3. SUBSTRING_INDEX()
4. LEFT()
5. RIGHT()
6. LPAD()
7. RPAD()
8. SPACE()
9. REVERSE()
10.REPEAT()
11.REPLACE()
12.STRCMP()
13.FIELD()
14.FIND_IN_SET()
15.FORMAT()
16.HEX(str)
//concept 1 substring(string,start,length) it will return the string from the given point/index/start_point
here start and length parameter is opetional
//concept 3 substring_index(string,delimeter,number)
it will give the string until the specifies delemeter
substring_index("www.google.com",".",1);
substring_index("www.google.com",".",2);
substring_index("www.google.com","o",1);
substring_index("www.google.com","g",2);
//concept 3 LEFT("string",index) it will return string of the left side based on given index
SELECT LEFT("Hello world Good Morning",4);
SELECT LEFT("Hello world Good Morning",10);
//concept 4 RIGHT("string",idnex) it will return string of the right side based on given index
SELECT RIGHT("Hello world Good Morning",4);
SELECT RIGHT("Hello world Good Morning",10);
//concept 5 LPAD("String",length,charecter) it will increase the length of the string by adding padding to it.
SELECT LPAD("Good",10,'*');
//concept 11 FIELD(search,str1,str2,str3,str4,str5,....)
SELECT FIELD('hello','good','morning',"to","the","friend","hello");
//concept 12 FIND_IN_SET(search,"string");
SELECT FIND_IN_SET("good","hello friend good morning");//it will not work
SELECT FIND_IN_SET("good","hello,friend,good,morning");//it will work
//concept 13 formate(floating_value,DigitAfterDecimal)
SELECT FORMATE(23.344909,1);
SELECT FORMATE(23.3449080,6);
SELECT FORMATE(23.3449090,3);
SELECT FORMATE(23.3448980,4);
*************************************************************************
DATE function
*************************************************************************
//list of date function
1.CURDATE()
2.CURRENT_DATE()
3.SYSDATE()
4.NOW()
5.LAST_DATE()
6.DAY()
7.DAYNAME()
8.DAYOFMONTH()
9.DAYOFWEEK()
10.DAYOFYEAR()
11.WEEK()
12.WEEKDAY()
13.WEEKOFYEAR()
14.YEAR()
15.YEARWEEK()
16.EXTRACT()
list of parameter with extract function
1.microsecon
2.second
3.minute
4.hour
5.day
6.WEEK
7.month
8.quater
9.year
10.Second_microsecond
11.minute_microsecond
12.minut_second
13.hour_microsecond
14.hour_second
15.hour_minute
16.day_microsecond
17.day_second
18.day_minute
19.day_hour
20.year_month
//concept extract(use parameter,"datetime") it will do all the operation by changing parameter.like day,month year m
inute second.
*************************************************************************
DATE function
*************************************************************************
/list of date function
some more function of the date
1.DATE_ADD()
2.ADDDATE()
3.MAKEDATE()
4.DATE_SUB()
5.SUBDATE()
6.DATEDIFF()
7.TO_DAYS()
8.FROM_DAYS()
9.PERIOD_ADD()
10.PERIOD_DIFF()
11.DATE_FORMATE()
12.STR_TO_DATE()
//List of ADDUNIT
1.microsecon
2.second
3.minute
4.hour
5.day
6.WEEK
7.month
8.quater
9.year
10.Second_microsecond
11.minute_microsecond
12.minut_second
13.hour_microsecond
14.hour_second
15.hour_minute
16.day_microsecond
17.day_second
18.day_minute
19.day_hour
20.year_month
DATE
day->d(1-31),e(01-31),D(st,nd,rd,),j(001-265)
month->M(Januaary),b(jan to dec),m(00-12),c(0-12)
Year->%Y,%y
WEEK->%a(mon),%W(Monday),w(0-6)
TIME
hour->%h,%H,%g,%G
MINUTE->%i;
SECOND->%s(00-59)
MICROSECOND->%f(000000 to 999999);
MERIDIAN->%p
/*
concept 1 ADDDATE('date' interval number) it add day in the given date.
ADDDATE('date' interval number days)
ADDDATE('date' interval number month)
ADDDATE('date' interval number year)*/
//Concept date_formate("date",'formate') it will return on the basis of the formate you specify there
SELECT date_format('1998-02-12','%Y');
SELECT date_format('1998-02-12','%Y/%M/%s');
SELECT date_format('1998-02-12','%Y-%b-%s');
SELECT date_format('1998-02-12','%d-%b-%Y');
SELECT date_format('1998-02-12 23:55:23:2334','%d-%b-%Y %h:%i');
SELECT date_format('1998-02-12 23:55:23:2334','%d-%b-%Y %h:%i:%s');
SELECT date_format('1998-02-12 23:55:23:2334','%d-%b-%Y %h:%i:%s:%f');
SELECT date_format('1998-02-12 23:55:23:23','%d-%b-%Y %h:%i:%s:%f %p');
*************************************************************************
TIME function
*************************************************************************
//time fucntion
1.CURTIME()
2.CURRENT_TIMESTAMP()
3.LOCALTIME()
4.LOCALTIMESTAMP()
5.TIMESTAMP()
6.TIME()
7.TIMEDIFF()
8.HOUR()
9.MINUTE()
10.SECOND()
11.MICROSECOND()
12.ADDTIME()
13.SUBTIME()
14.MAKETIME()
15.TIME_FORMATE()
16.SEC_TO_TIME()
17.TIME_TO_SEC()
//this concept is similar to date fucntion you just memorize fucntion name then sart using it.
*************************************************************************
ALTER COMMAND
*************************************************************************
my code:
select column_name,constraint_name,referenced_column_name,referenced_table_name from information_schema.k
ey_column_usage where table_name='stu';
some times you come across where you have seen ,when you use AUTOINCREMENT in your table then it will inc
rement by it self.
when you delete any record then id value show bad impact.
when you insert new record it will continue from that index.
so need to solve this problem by using alter commond
*************************************************************************
DROP and TRUNCATE COMMAND
*************************************************************************
//drop and truncate function is used to remove table and table data
//syntax
DROP table student;
//syntax
TRUNCATE table student;
*************************************************************************
VIEW COMMAND
*************************************************************************
//view commond
NOTE: why we need to use view ,the resion is that
when we need to use a complex query again n again like to join multiple table or, join three table it's a time taking pr
ocess to write again again
thats why we will create view and past that query into view and when you need just call it by view name.
//syntax
CREATE VIEW view_name
AS
SELECT column1,column2,column3....
FROM table_name
WHERE condition1,condition2,condition3,...
Group By column_name,
ORDER BY column_name;
//syntax
RENAME table view_name
TO new_view_name
//example
RENAME table My_view To Your_view;
//if we need to drop view how you will do that
we can do that with the help of following syntax.
//syntax
DROP view view_name;
//Example
DROP view My_view;
disadvantage
1.performance decrease.
2.dependency on table
*************************************************************************
INDEX COMMAND
*************************************************************************
//index commond we use to increase the seraching in the table
but we offence use it on the big table .
//syntax
CREATE INDEX index_name
ON table_name(column1,column2,column3,....);
//guidline of index
when you create index you should remember some tips.
1. you should not select a column for index which is alredy a primay column or unique.
2. you we select that column which is mostly use form searching
3.index we can apply on that column which is used for join in multiple table.
4.we shoid to that column which has many null values.
5. we should not apply index on small table
//example 1.1
SELECT * FROM student;
where dob>"1997-12-30";
//example 1.2
//example
show index from student;