Pages

MySQL begineer tutorial part-3

Hello Friends,

This  is the third series of MySQL beginner tutorial. I hope this third tutorial will enhance your knowledge of SQL with MySQL databse.



In previous two tutorial (tutorial-1 and tutorial-2) we learn how to connect to MySQL server and creation of data base and table.

we are using college database for our tutorial. In this part we learn some more SQL statements.

SQL statements and clause which we learn in this tutorial are-

INSERT statement
SELECT
SELECT with LIMIT clause
Conditional SELECT using WHERE clause
WHERE clause with AND, OR, NOT conditional operator


I hope this will add some new knowledge about SQL statement


INSERT statement : 

 

INSERT statement is used for populating the table, or we can say that INSERT statement is used for creating a record or tuple in table.

Since we have college database and student table, so we insert a student detail in our student table.

syntax for INSERT statement


INSERT INTO tablename (columnname 1, column2name2,.....) VALUES (columnvalue1,columnvakue2,...)

 Now in student table--

mysql>INSERT INTO tablename (roll_no, name, sex, age) VALUES ('101', 'RAM', 'M', 24);
mysql>INSERT INTO tablename (roll_no, name, sex, age) VALUES ('102', 'Neha', 'F', 22);

so now we have successfully insert two rows in student table.

NOTE : here one thing is for notice , that in values for roll_no, name., and sex we use single quotes ('') to put the value and for age we did not use qoutes since when we creating we define roll_no, name, as varchar and sex as char datatype and for age we define int datatype so when we insert a value for char or varchar datatype we use '' and for number in this case int we does not use ''.

you can check the datatypes of column for student table using DESC commnd that I have already told you.




SELECT statement :



SELECT statement is used for viewing the data of a particular table, so now we see the data of student table

syntax for SELECT statement


SELECT column[s] FROM table-name.
mysql > SELECT roll_no, name, sex, age FROM student;





selecting the particular column
SELECT roll_no FROM student;




selecting all column using *
mysql > SELECT * FROM student;


SELECT with LIMIT clause---------------

suppose we have table that contains 100s of record and we want to show only top 2 results, in this case we uses LIMIT clause to limit no of records

SELECT roll_no, name FROM student LIMIT 2; // this will return only first 10 record of the table.



WHERE clause in SELECT statement

WHERE clause is used for filtering the result of select statement.

suppose we want to select the recode which match to a particular condition

SELECT sex, age from student WHERE name='Akriti'.




SELECT roll_no, name from student where sex ='F'






WHERE clause with AND, OR, NOT conditional operator









I hope this third tutorial will help you to gain some knowledge about SQL and using it with MySQL.

If you have any confusion or suggestion you can type your query and suggestion in comment section and I will like to solve your query.

So we meet next time with next series of this tutorial.