As developers of any technology which uses MySQL as backend we always need to know some basic queries to work on. The list may vary as per the development need but a basic set still remains the same.
The very first query that comes into use is to set the base for any database i.e creating a database itself its as simple as
create database <yourdatabase>
create database employee
Want to check what other databases are there
show databases
The next command may seem too basic to know but is root of many errors that creep in due to wrongly selected database. Unless you are using a GUI, to work on any database the command that comes in handy is
use <yourdatabase>
use employee
Next object that is always needed is the table. We wont go here into nitty gritty of the data types and keys as they are a topic in themselves.
create table employee_master(id int, name varchar(20), date_of_birth datetime)
To view what all tables are there in the database
show tables
If we need to check the structure(fields) of any table the command that comes to our help is
desc <tablename>
desc employee_master
The table is ready so we need to populate it with data. There are many command variations possible but the base of all of them is same
insert into <tablename> values(<value1>, <value2>, <value3>)
insert into employee_master values(1, 'abc','1967-5-20')
The data is inserted and we need to view the content, the command that comes to rescue is select this command has to many variations that a complete blog can be written on it. But the most basic one to start is
select * from <tablename>
select * from employee
The data needs to be updated as value was wrongly entered. The command to your rescue is update
update >tablename< set <fieldname> = <new value>
update employee_master set date_of_birth='1968-5-20'
A catch here !! the above command will update all the records with the new value, so be careful and use where clause.
So the corrected command will look like :
update employee_master set date_of_birth='1968-5-20' where id=1
Sometimes we might need to remove a erroneous record the command that comes to your rescue is delete
delete from <tablename>
delete from employee_master
Again be cautious here, as the command in its basic format will delete all the records. Use it carefully with where clause
delete from employee_master where id=1
Will cover more query variations on Select, Update, Delete commands in coming posts.
0 Comment(s)