Professional Documents
Culture Documents
SQL-1
SQL-1
SQL-1
Total marks : 16 marks ( 16*1=16)
Number of questions expected for board exam : 16
1
2021-22/ COMPUTER SCIENCE XII/ SQL NOTES JISHA T P
SQL
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
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
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);
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);
( 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;
Syntax:
mysql>INSERT INTO <table_name> VALUES (val1, val2,.. );
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
2 Deepa 7777 XI G
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:-
The above statement insert the values for specific columns—Regno and
Name respectively and remaining columns will be automatically inserted
by Null values .
9
2021-22/ COMPUTER SCIENCE XII/ SQL NOTES JISHA T P