SQL Commands - 1
SQL Commands - 1
Learning Objectives
• At the end of this chapter the students will be able to understand:
• What is SQL?
• Need for SQL
• How to create tables in SQL?
DDL Command
Create database……
Create table ……,
Alter table……
Drop table…….
DML Command
Insert into ………..
Update student ……………………….
Delete from ………………………….
Select col1, col2…. From table_name;
Introduction
SQL (Structured Query Language) is a standard language for accessing and manipulating databases.
SQL commands are used to create, transform and retrieve information from Relational Database
Management Systems and also used to create interface between user and database. By using SQL
commands, one can search any data in the database and perform other functions like, create tables,
add records, modify data, remove rows, drop table etc. SQL commands are used to implement the
following;
• SQL can retrieve data from a database
• SQL can insert records in a database
• SQL can update records in a database
• SQL can delete records from a database
• SQL can create new databases
• SQL can create new tables in a database
• SQL can create views in a database
>>>show databases;
>>>create database <database name>;
>>>use <database name>;
>>>create table <tableName>(<colName1 dataType [size] [constraints], colName2 dataType…..>
---desc <table name>;
>>>desc employee;
---Insert into <table name> values(val1, val2, val2….. valn);
---Select col1, col2, col3 from <table name>;
>>>Select * from employee;
Syntax:
CREATE TABLE<table name>(
<column name1> <data type>[size][constraints],
<column name2> <data type>[size][constraints],
:
:
<column name n> <data type>[size][constraints]
);
Example:
Create the following table:
Table: Student
Column Name Data Type Size Constraints
Adno Numeric 3 Primary key
Name Varchar 20 NOT NULL
Class Numeric 2
Section Char 1 default ‘A’
Fees Numeric 10, 2 check Fees<7000
Command:
➢ CREATE TABLE student (
Adno Numeric(3) Primary Key,
Name varchar(20) not null,
Class Numeric(2),
Section char(1) default ‘A’ ,
Fees Numeric(10, 2) check Fees<7000
);
SELECT Command
This command is used to retrieve/view table information from SQL database. By using SELECT
command, we can get one or more fields information, while using *, one can get all fields
information.
Syntax: SELECT (*/column name) FROM <table name> [WHERE <condition>];
We can specify any condition using where clause. Where clause is optional.
Example:
1. Display student table information.
➢ SELECT * FROM student;
This will display all information of the particular table (student) in the database.
2. To display name and class of student table.
➢ SELECT name, class FROM student;
3. To display name of 10th class student.
➢ SELECT name FROM student WHERE class = 10;