Monday, 5 March 2018

SQL Basic Commands

Create database
Create database statement is used to create a database in SQL
Syntax:
Create database databasename;
Example:
Create Database LP;   
   

Select database
It is used to select the database.
Syntax:
use databasename;
Example:
use LP;

Create table:
Create table statement is used to create a table in SQL
Syntax:
 Create table table name( fieldname1 datatype,fieldname2 datatype...);
Example:
Create table DPK (Employee_ID  INT  Not  NULL,Employee_Name Varchar (20)NOT NULL, Salary decimal(7,2) NOT NULL ,PRIMARY KEY (Employee_ID));    
Note:
salary decimal(7,2)
The first argument is precision,which is the number of total digits.The second argument is scale which is the maximum number of digits to the right of the decimal point. 
Example:
34567.23,23478.45

Insert statement:
Insert statement is used to insert data into table in SQL
Syntax: 
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
Example:
INSERT into DPK(Employee_ID, Employee_Name,Salary) values (101, 'Ram Kumar',89555.78 );
 INSERT into DPK(Employee_ID, Employee_Name,Salary) values (102, 'suryosnata',55000.79);
  INSERT into DPK(Employee_ID, Employee_Name,Salary) values (103, 'Dipak',90000.58 );

Select statement:
It is used to retrieve data from table.
Syntax:
SELECT statement is used to select data from one or more tables:
SELECT column_name(s) FROM table_name;
We can use the * character to select ALL columns from a table
Select*from  table name;
Example:
  Select * from DPK;

Update statement
Update statement is used to update existing records in a table
Syntax: 
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value 
Example:
update  DPK set Employee_Name='debasish' where Employee_Name='Ram Kumar';


Delete statement:
delete statement is used to delete records from a table
Syntax: 
delete form table name
Or
DELETE  FROM table_name
WHERE some_column = some_value
Example:
Delete from DPK where Employee_ID=101;


Commands:
// Create Database
 Create Database LP;   
   
 //Select Database
  Use LP;          
               
//Create table DPK
Create table DPK (Employee_ID INT Not NULL,Employee_Name Varchar (20)NOT NULL, Salary decimal(7,2) NOT NULL ,PRIMARY KEY (Employee_ID));    
//Retrieve  Data from DPK
  Select * from DPK;

//Insert Data into DPK
INSERT into DPK(Employee_ID, Employee_Name,Salary) values (101, 'Ram Kumar',89555.78 );
INSERT into DPK(Employee_ID, Employee_Name,Salary) values (102, 'suryosnata',55000.79);
INSERT into DPK(Employee_ID, Employee_Name,Salary) values (103, 'Dipak',90000.58 );

//Retrieve  Data from DPK
Select * from DPK;


//Update data
 update  DPK set Employee_Name='debasish' where Employee_Name='Ram Kumar';
Select * from DPK;


//Delete data
Delete from DPK where Employee_ID=101;
Select * from DPK;


No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...