Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

UNIT-3 CHAPTER-2

SQL-1
Total marks : 16 marks ( 16*1=16)
Number of questions expected for board exam : 16

Steps for creating a table in MYSQL.

Given a table COMPANY below, write queries to create the database


CID NAME CITY PRODUCT
111 Sony Delhi TV
222 Nokia Mumbai Mobile
333 Onida Delhi TV
444 Sony Mumbai Mobile
555 Blackberry Madras Mobile

Step 1: - Create a database using CREATE DATABASE command


CREATE DATABASE ANSAR;

Step 2: - Open the database using USE command.


USE ANSAR;

Step 3: - Create a table using CREATE TABLE command


CREATE TABLE COMPANY ( CID INT, NAME CHAR(10), CITY CHAR(10),
PRODUCT CHAR(10));

Step 4: - Insert rows into the a table using INSERT command


INSERT INTO COMPANY VALUES (111,’Sony’,’Delhi’,’TV’);
INSERT INTO COMPANY VALUES (222,’Nokia’,’Mumbai’,’Mobile’);
INSERT INTO COMPANY VALUES (333,’Onida’,’Delhi’,’TV’);
INSERT INTO COMPANY VALUES (444,’Sony’,’ Mumbai’,’Mobile’);
INSERT INTO COMPANY VALUES (555,’Blackberry’,’Madras’,’TV’);

Step 5:- Display the table using SELECT command


SELECT * FROM COMPANY;

1
2021-22/ COMPUTER SCIENCE XII/ SQL NOTES JISHA T P
SQL

 Structured Query Language


 It is a standard relational database language which helps us to create
and operate relational database which are set of related information
stored in tables.
 SQL provides different types of statements or commands for different
purposes. These statements are classified into 4 in the following
categories:
 Data Definition Language (DDL)
 Data Manipulation Language (DML)
 Data Control Language (DCL)
 Transaction Control Language (TCL )
Differences between DDL and DML
1. DDL:
 Data Definition Language
 It provides set of definitions to specify the storage structure and
access method used by the database system. It consists
statements for the creation and deletion of tables
 For eg:- CREATE TABLE, DROP TABLE ,ALTER TABLE.
2. DML:
 Data Manipulation Language
 It enables users to access retrieve, insert, delete or modify data
stored in a database .
 For eg:- SELECT,INSERT,UPDATE,DELETE
MYSQL
It is an open source and freely available Relational Database Management
System (RDBMS) that uses Structured Query Language (SQL).
 It provides excellent features for creating, storing, maintaining and
accessing data stored in the form of databases and their respective
tables.
 All the statement in SQL terminate with a semi-colon(;).

2
2021-22/ COMPUTER SCIENCE XII/ SQL NOTES JISHA T P
 SQL is not a case sensitive, therefore, we can type commands in
either upper case or lower case.

SQL DATATYPES
SQL supports the following datatypes for the specification of various
data-items or fields of a relation/table.
1. INTEGER (Numeric) - INTEGER or integer or int
2. NUMERIC- NUMERIC(x, y) or DECIMAL(x ,y)
Numbers are stored in the given format, where x is the total number
of digits and y is the number of places to the right of the decimal
point.
For eg: Numeric(8,2) or DECIMAL(8,2)
3. CHARACTER(fixed length) - CHAR(x) or CHAR(size)
4. CHARACTER(variable length) - VARCHAR(x) or VARCHAR2(x)

5. DATE - DATE
This data type is used to store a date in ‘yyyy/mm/dd’ format. It
stores year, month and date values. DATE values can be compared
with each other only. The date values to be entered are to be enclosed
with quotation marks.
6. TIME - TIME
This data type is used to store time in hh:mm:ss format. It stores
hour, minute and second values.
For example, a time of day can be taken as 12:30:45p.m. where 12
means hours, 30 means minutes and 45 refers to seconds

Differences between char and varchar


1. CHAR datatype provides fixed length memory storage. It specifies
a fixed length character string.
VARCHAR datatype provides variable length memory storage. It
specifies a variable length string (changeable).

2. The CHAR datatype can store a maximum of 0 to 255 characters.


The VARCHAR datatype can store a maximum number up to 65,535.
3
2021-22/ COMPUTER SCIENCE XII/ SQL NOTES JISHA T P
3. CHAR datatype is used when the data entries in a column are
expected to be of the same size.
VARCHAR datatype is used when the data entries in a column are
expected to vary considerably in size.

4. Search operation is faster with CHAR datatype column.


Search operation works slower in VARCHAR datatype column as
compared to CHAR

SQLCOMMANDS
A database is used to house data in the form of tables. Therefore, before
creating a table, it is mandatory to create a database first.

1) SHOW DATABASES –
It is used to find out which databases currently exist on the server.
Syntax: mysql> SHOW DATABASES;

2) CREATE DATABASE-
The CREATE DATABASE command is used to create a database in
RDBMS.
Syntax : mysql> CREATE DATABASE <database_name>;
For Eg:- mysql> create database school;
It creates database with the name school

3) OPENING DATABASES
Once a database has been created, we need to open it to work on it.
For this, USE command is required.
Syntax: mysql> USE <database_name>;
For Eg:- mysql> use school;

4) REMOVING DATABASES

4
2021-22/ COMPUTER SCIENCE XII/ SQL NOTES JISHA T P
To physically remove/delete a database along with all its tables,
DROP command is used.
Syntax: mysql>DROP DATABASE <database_name>;
For E.g: mysql> drop database school;

5) CREATING A TABLE
The CREATE TABLE statement is used to create a table in a
database. Tables are organized into rows and columns, and each table
must have a name. It is the most extensively used DDL command.
A table must have at least one column.
Syntax for creating a table:
CREATE TABLE <table_name>(<column_name1> <data_type> ,
<column_name2> <data_type> .......... );
For example, write SQL query to create the following table .
STUDENT
REGNO NAME DOB AADHAR CLASS

101 ARUN 25-10-2000 784213 XII A

102 KABIR 24-08-2001 123457 XII F

Query:
mysql> Create table STUDENT(REGNO integer, NAME varchar(10), DOB
date, AADHAR integer, CLASS varchar(5));

CONSTRAINTS
Constraint is a condition applicable on a field or group of fields.
Two types of constraints are:
 Column constraint: Apply only to individual column.
 Table constraint: Apply to group of columns.

Different constraints:
1) Unique Constraint-It ensures that no 2 rows have the same values in the
specified column.
CREATE TABLE student
5
2021-22/ COMPUTER SCIENCE XII/ SQL NOTES JISHA T P
( id char (4) UNIQUE,
name char (20) NOT NULL,
age int mark int);

2) Primary Key constraint- This constraint declares a column as the primary key.
The primary keys cannot have NULL values , thus this constraint should be
applied to columns declared as NOT NULL.
CREATE TABLE student
( id char (4) PRIMARY KEY NOTNULL,
name char (20) NOT NULL,
age mark int);

3) Default constraint-a default value can be specified for a column using


default clause. If no value is provided, the default value will be entered.
CREATE TABLE student
( id char (4) ,
name char (20) NOT NULL,
age int mark int DEFAULT=0);

4)Check constraint- This constraint limits the value that can be inputted into
a column.
CREATE TABLE student
( id char (4),
name char (20),
age int CHECK (age<20 mark int);

5) NOT NULL constraint- This constraint ensures that inputting valueinto


a column is mandatory.
CREATE TABLE student

( id char (4) NOT NULL,name char (20) , age int mark int);

6
2021-22/ COMPUTER SCIENCE XII/ SQL NOTES JISHA T P
Applying table Constraint-

Table constraint appear at the end of table definition. For example, if you want mark
and id of above table to be unique you may write it as follows:
CREATE TABLE student
( id char (4) PRIMARY KEY NOT NULL,
sname char (20) NOT NULL,
age int ,marks int,
UNIQUE (id, marks)); //id and marks are 2 different columns

6) VIEWING A TABLE
To verify that the table has been created, SHOW TABLES command
is used.
Syntax: mysql>SHOW TABLES;
For eg: mysql> show tables;

7) VIEWING A TABLE STRUCTURE


To view a table structure, DESCRIBE or DESC command is used.
Syntax: mysql> DESCRIBE <tablename>;
For eg: mysql> describe student;

8) INSERTING DATA INTO A TABLE


The INSERT INTO command is used to insert a new record/row/tuple
in a table.
It is possible to write the INSERT INTO statement in the following
different forms:
(a) Inserting data (for all the columns) into a table: In the first
method, it does not specify the column names where the data will be
inserted, only their values.

Syntax:
mysql>INSERT INTO <table_name> VALUES (val1, val2,.. );

For example, write SQL query to create the following table .


7
2021-22/ COMPUTER SCIENCE XII/ SQL NOTES JISHA T P
STUDENT
REGNO NAME AADHAR CLASS

101 ARUN 784213 XII

102 KABIR 123457 XI

Query:
insert into student values (101,’ARUN’, 784213, 'XII');
insert into student values (102,’KABIR’, 123457, 'XI');

(b) Inserting data directly into a table: The second form specifies both
the column names and the values to be inserted.
Syntax:
mysql> INSERT INTO <table_name> (column1,column2,,...)
VALUES (value1,value2,valueN,...);
Here, column1, column2,—the names of the columns in the table
for which you want to insert data.
For eg:-, insert into student (REGNO, NAME,AADHAR,CLASS)
values(2,'Deepa’,7777,’XI G’);
will add a new row as follows

REGNO NAME AADHAR CLASS

101 ARUN 784213 XII

102 KABIR 123457 XI

2 Deepa 7777 XI G

When adding a row, only the characters or date values should be


enclosed within quotes.

(c) Inserting data into specific columns of a table:


Syntax :
mysql> INSERT INTO <table_name>(column1, column2, ... columnN)
VALUES (value1, value2,.... valueN);

8
2021-22/ COMPUTER SCIENCE XII/ SQL NOTES JISHA T P
For example, to insert a record into the student table for the columns Regno
and Name , SQL insert query is:
insert into student (REGNO, NAME) values(5,'ROHIT’);
will add a new row as follows:-

REGNO NAME AADHAR CLASS

101 ARUN 784213 XII

102 KABIR 123457 XI

5 ROHIT NULL NULL

The above statement insert the values for specific columns—Regno and
Name respectively and remaining columns will be automatically inserted
by Null values .

(d) Inserting NULL values into a table:


If a column in a row has no value or missing value, then the column is said
to be null or holding NULL value. We can insert NULL value into any
column in a table. It can be done by typing NULL without quotes.
Null is not equivalent to 0, i.e., NULL ≠ 0. It acts as a placeholder for
unknown or inapplicable values.
For example, insert into student(Regno, Name, Aadar, class,)
values(NULL, 'Swati’,NULL, NULL);
After the execution of the above command, NULL value shall be inserted
for the fields Regno, Aadar and Class respectively.

REGNO NAME AADHAR CLASS

101 ARUN 784213 XII

102 KABIR 123457 XI

NULL Swati NULL NULL

9
2021-22/ COMPUTER SCIENCE XII/ SQL NOTES JISHA T P

You might also like