msql query

8

Click here to load reader

Upload: harman-kaur

Post on 06-May-2015

157 views

Category:

Education


0 download

DESCRIPTION

THESE ARE SOME VERY BASIC MYSQL QUERIES... hope it helps...

TRANSCRIPT

Page 1: Msql query

BASIC DATABASE(MYSQL) QUERIES

1. create database dbname;

This statement is used to create new database.

2. show databases;

You can check available databases using show.3. use dbname;

When you have multiple databases in your SQL Schema, then before starting your operation, you would need to select a database where all the operations would be performed. The SQL USE statement is used to select any existing database in SQL schema.

4. show tables; You can check available tables in specific database using show.

Page 2: Msql query

5. create table tname(c1name c1type, c2name c2type, c3name c3type……cnname cntype) The above statement is used to create new table, it requires to specify the column names and type. Type indicates the data to be entered in specific column. create table student(sname varchar(25), sroll int, sage int);

6. describe tname To view the structure of table, we use describe statement, Suppose if we had deleted all the records of the table or the table is empty, and we want to insert new record, we must know the structure of the table, ie column name and its datatype.The output of describe would look like this.. for ex: describe employee_record

Field Type NullEname Varchar yesSalary Int Yes

7. INSERT

Insert query is use to fill the table or add content to the specific table. It can be used in following ways:

insert into tname values(c1value,c2 value, c3value……cnvalue);

To insert data in table

insert into tname (c1name, c3name, c7name,..cnname)values(c1value,c7value,..cnvalue);

To insert data in specific columns only not in all the columns, we need to specify the column names.

insert into t1name( c1name,c2name..cnname) select c1name,c2name..cnname from t2name;

This syntax is used to populate table with data in other existing table.

Page 3: Msql query

In the above snapshot we are inserting data into parent table using the data in child table.

8. DELETE (Delete query doesnot use *)

Delete query is used to remove record or delete some record from specific table. If delete query is used without Where clause, it will delete all the data in the table.

Delete from tname where some condition.Delete from student where roll_no =4;

Delete from tname; Empty the table

Delete from tname where age =(select max(age) from tname); Delete the record of student whose age is maximum.

Page 4: Msql query

In the above snapshop we have deleted all the records where eid and eno are same or equal…. In the same way we can apply any condition.

9. SELECT

Select query is used to view the contents of specific table, it can be used as:

Select * from tname;To view all the contents of table. Here * stands for all.

Select c1name,c2name…cnname from tname;To view records of specific columns/fields. For ex select sname from student.

Page 5: Msql query

Select * from tname where some condition.To view all the contents of table that satisfies the given condition. For ex: select * from employee where salary > 10000;

Select distinct(c1name) from tname;To view unique values or unrepeated values of the table’s particular column. For ex: select distinct(sname) from student.

Select count(cname) from tname. Counts the no. of records in specific column. for ex: select count(sname) from student.

Select count(distinct(cname)) from tname.Counts the distinct no. of records in specific field. For ex: select count(distinct(sname)) from student.

10. DROP

Drop query is used to remove table from selected database or to remove whole database.

Drop database dbname Drop table tname.

11. ALTER (alter query needs the word table explicitly)

Alter query is generally used to change the structure of the table, either by adding new column or deleting existing column.

Alter table tname ADD newcname newctype;Above statement can be used to add new column to the existing table. Say alter table student add parent_name varchar(45);

Alter table tname DROP cname.Above statement can be used to delete existing column from an existing table. Say alter table student drop parent_name;

Alter table tname rename to new_tname;

Above statement can be used to change the name of existing table. For ex: alter table student rename to student_record;

Page 6: Msql query

12. UPDATE

Update query is used to change the data resinding in the table. Or to change the content of table, if update query is used without where clause, it will change all the records of specific column.

Update tname set cname= whatever where some condition.

Update the record of table which satisfies the given condition.

Update student SET age=20 where roll_no =1;

Above statement will change the age to 20 of the student whose rollno is1.Here student is the table name, age is the columnname and roll_no is another column name.

OR

Update student set sname=’arti sharma’ where roll_no=2.

Above statement will change the name to arti sharma of the student whose rollno is2.Here student is the table name, sname is the columnname and roll_no is another column name.

OR

Update student set age=20;

Above statement will change the age of all the students because we have not specified any condition using where clause.