My SQL

                                                                                                               << BACK
Query :
Create Database :
create database databaseName

Create Table :

    CREATE TABLE `user_details` (
      `user_id` int(11) NOT NULL AUTO_INCREMENT,
      `user_name` varchar(2000) DEFAULT NULL,
      `user_email` varchar(2000) DEFAULT NULL,
      `user_number` varchar(2000) DEFAULT NULL,
      `user_dob` date DEFAULT NULL,
      `gender` varchar(2000) DEFAULT NULL,
      `address` text,
      `ip` varchar(100) DEFAULT NULL,
      `upload_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      PRIMARY KEY (`user_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
       

Drop Table :

DROP TABLE `user_details1`;

Insert Data :

insert into `user_details`(`user_id`,`user_name`,`user_email`,`user_number`,`user_dob`,`gender`,`address`,`ip`,`upload_date`) values (1,'Rajesh','rajeshpalande77@gmail.com','9221460041','1988-12-07','Male','test',NULL,'2015-08-26 13:16:11');

Show All Data :

select * from user_details

Show Record Base On Key :

select user_name from user_details
select user_name,user_email from user_details

Fetch Record By Using Where Clause :

select * from user_details where user_id = '1'

Delete Record By Using Where Claise :

delete from user_details where user_id= '3'

Update Record :

update user_details set user_name = "Ramesh" where user_id = '2'

Count :

select count(*) from user_details
select count(*) from user_details where user_name = 'Rajesh'

Update Record : 

select max(*) from user_details

Max : 

select min(*) from user_details

Join : 

u ,e is object
SELECT e.emp_name, u.user_name FROM emp_details AS e JOIN user_details AS u ON e.emp_id = u.user_id

Unique Record : 

select DISTINCT user_email from user_details

Ascending Order :

select * from user_details order by user_name asc

Descending Order :

select * from user_details order by user_name desc

Limit :

select * from user_details limit 1

Order + Limit :

select * from user_details order by user_name desc limit 1

Like(Search Last Word) : 

select * from user_details where user_name like '%esh'

Like(Search First Word) :

select * from user_details where user_name like 'raj%'

Like(Search Any Word) :

select * from user_details where user_name like '%ra%'

Group By : 

SELECT emp_name, Max(age) FROM emp_details GROUP BY emp_name

Alter (Add Columns) : 

ALTER TABLE emp_details ADD DateOfBirth date

Alter (Delete Columns)  : 

ALTER TABLE emp_details DROP COLUMN DateOfBirth

Alter (Table Name)  : 

ALTER TABLE TableOldName RENAME TableNewName

Get result from multiple Value in column  : 

e.g. my column name is section_id and value of this section id is 7,17,27,37  and I want to find  result of matching only number 7, not (17,27,37) then use following query
select * from tableName where section_id REGEXP '[[:<:]](7)[[:>:]]'


Store language other than english in mysql (e.g hindi, marathi, japanese, etc... : 

ALTER TABLE display_data MODIFY discription VARCHAR(20) CHARACTER SET UTF8


No comments:

Post a Comment