BASIC DATABASE(MYSQL) QUERIES1. 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.
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 Null Ename Varchar yes Salary Int Yes7. 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.
In the above snapshot we are inserting data into parent table using the data inchild 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.
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. SELECTSelect 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.
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. DROPDrop query is used to remove table from selected database or to remove wholedatabase. 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 byadding 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;12. UPDATEUpdate query is used to change the data resinding in the table. Or to change thecontent of table, if update query is used without where clause, it will change all therecords 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.Herestudent is the table name, age is the columnname and roll_no is another columnname. ORUpdate student set sname=’arti sharma’ where roll_no=2.Above statement will change the name to arti sharma of the student whose rollnois2.Here student is the table name, sname is the columnname and roll_no is anothercolumn name. ORUpdate student set age=20; Above statement will change the age of all the students because we have notspecified any condition using where clause.