XII - CS - CA - 9 - Structured Query Language - Hsslive
XII - CS - CA - 9 - Structured Query Language - Hsslive
IN) 1
Data type specifies the type of value that can be entered in a column in a table. It ensures the
correctness of data. Data types in SQL are classified into three, Numeric data type, String data type
,Date and time data type.
I). Numeric data type
The commonly used numeric data types in MySQL are INT or INTEGER and
DEC or DECIMAL.
a). INT or INTEGERS : They are whole numbers ie, without fractional part. They can be positive,
negative or zero.
b). DEC or DECIMAL: They are numbers with fraction. Numbers.
The syntax is DEC (Size, D) where Size is the total number of digits and D is the number of
digits after the decimal point. For example DEC(5,2), where the total number of digits is 5 and the
number of digits after the decimal point will be 2.
a) CHAR or CHARACTER : Character includes letters, digits, special symbols etc. It is a fixed length
data type.
Syntax: CHAR (Size) where Size represent the maximum number of characters. The default
size of CHAR data type is 1.
b) VARCHAR data type : The VARCHAR data type represent variable length strings. It is similar to
CHAR, but the space allocated for the data depends only on the actual size of the string.
a)Date:-The date data type is used for storing date. The date in MySQL is
represented in YYYY-MM-DD format(Standard format).
Column constraints : These are applied only to individual columns. They are written immediately
after the column.
1) NOT NULL:-This constraint ensures that a column can never have NULL(empty) values.
value is 1.The auto increment column must be defined as primary key of the table. Only one
auto_increment column is allowed in a table.
3) UNIQUE:- This constraint ensures that no two rows have the same value in a specified column.
This constraint can be applied to those columns that have been declared NOT NULL.
4) PRIMARY KEY:- It declares a column as the primary key of a table. This column must not have null
values and every value should be unique. So a PRIMARY KEY constraint is a combination of UNIQUE
and NOT NULL constraints.
Table Constraints
A table constraint can applied to an individual column or group of columns .It usually appears
at the end of table definition. Examples of table constraints are UNIQUE and CHECK
CHECK:-This constraint limits the values that can be inserted into a column of a table.
Eg: CREATE TABLE STUDENT( adno INT PRIMARY KEY AUTO_INCREMENT, clno int, regno int, NAME
VARCHAR(20) NOT NULL, TOTAL_MARK INT, PERCENTAGE DEC(5,2), UNIQUE ( clno, regno );
Here Table Name represents the name of the table to be created, Column Name the
Eg:-CREATE TABLE STUDENT( ROLLNO INT PRIMARY KEY, NAME VARCHAR(20),TOTAL_MARK INT,
PERCENTAGE DEC(5,2) );
II). ALTER TABLE Command with MODIFY keyword is used to modify an existing column
Syntax : ALTER TABLE <TableName> MODIFY (<ColumnName> <NewDataType> [(<NewSize>)] ) ;
III). ALTER TABLE Command with DROP keyword used to remove a column from a table.
Syntax: ALTER TABLE <Table Name> DROP <Column Name> ;
IV). ALTER TABLE Command with RENAME TO keyword used to rename an existing table
The SELECT Command is used to select rows (tuples or records) from a table. The
Syntax : SELECT <ColumnName1>,[<ColumnName2>,.... ] FROM <Table Name> ;
Eg:- SELECT ROLLNO,NAME,TOTALMARK from STUDENT ;
i). The DISTINCT Keyword : It is used to avoid duplicate rows from the result of a select
command
ii). ALL :The keyword ALL is used to display duplicate rows in a select command.
iii) WHERE Clause : The WHERE clause is used to select rows or columns from a table which satisfy a
specific condition. The condition can be expressed using Relational operators or Logical operators.
Syntax:
SELECT <Columnname>,[<ColumnName>,.....] FROM <TableName> WHERE <Condition> ;
Eg. SELECT name, salary from employee where salary >15000;
viii) ORDER BY Clause: The ORDER BY clause is used to sort the result of a select statement in
ascending (ASC) or descending(DESC) order. The default order is ascending.
Eg : SELECT * FROM STUDENT ORDER BY regno ASC;
vii) COUNT ( * ) Function: COUNT() function used to find the number of rows that matches a specified
condition. It can used with DISTINCT command to avoid duplicate rows.
viii) GROUP BY Clause : The GROUP BY clause is used to group the rows of a table based on a common
value.
Aggregate Functions
The aggregate functions acts on a group of data and returns a single data. They are also called summary
functions. Commonly used aggregate functions
d) UPDATE Command : used to change the values in a column of specified rows. The
rows are set to new values using the SET keyword.
Syntax : UPDATE <Table Name> SET <Column Name> =<value>[,<Column Name>=<value> .... ]
[ Where <Condition> ];
Eg. UPDATE STUDENT SET Adno=1111 WHERE Name=’Biju’ ;
11. Write SQL Command to create an employee table with the following attributes.
Salary numeric(7,2)
CREATE TABLE employee(empid int primary key, name char(30) NOT NULL, salary
numeric(7,2));
12. Write SQL command to data entry to employee table with the following attributes-
Empid integer primary key
Name char(30) not null
Salary numeric(7,2)
13. Write SQL command to insert name and empid of the above employee table.
INSERT INTO employee (empid, name)values(1111,’Subi’);
A Nested query is a query placed within another SQL query. The inner query is known as the
sub query and the query that contains the sub query is known as outer query. SQL first
evaluate the sub query and its result is assigned to the outer query.
Eg. SELECT Regno, Name FROM Student WHERE Score=(SELECT MAX(Score) from
Student);
24. Write the essential clause required for each of the following SQL command.
Prepared by Anil Kumar , GHSS,Kuzhumathikadu
XII Computer Science/Application(HSSLiVE.IN) 8