You are on page 1of 8

THE ASIAN SCHOOL, KINGDOM OF BAHRAIN

GRADE: 9 SUBJECT: COMPUTER SCIENCE

UNIT 3

INTRODUCTION TO SQL AND SQL QUERIES

SQL:

SQL stands for Structured Query Language.


SQL is a standard database language for storing, manipulating and retrieving data
stored in a relational database. All the Relational Database Management Systems
(RDMS) like MySQL, MS Access, Oracle, Sybase, Postgres and SQL Server use
SQL as their standard database language. SQL is a standard language for accessing
and manipulating databases.
RDBMS

RDBMS stands for Relational Database Management System.

TABLES:

The data in RDBMS is stored in database objects called tables.A table is a


collection of related data entries and it consists of columns and rows.A table is the
most common and simplest form of data storage in a relational database. Example:
customers table

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | Kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+

1
ID NAME AGE ADDRESS SALARY

1 RAMESH 32 AHMEDABAD 2000

2 KHILAN 25 DELHI 1500

3 KAUSHIK 23 KOTA 2000

4 LAXMAN 25 MUMBAI 5000

5 MILAN 30 BANGLORE 4000

6 SHIVA 35 CHENNAI 4500

7 MARTIN 26 PUNE 5000

FIELD:

Every table is broken up into smaller entities called fields. A field is a column in a
table that is designed to maintain specific information about every record in the
table.

The fields in the above given Customers table consist of ID, NAME, AGE,
ADDRESS and SALARY.

RECORD/ROW :

A record, also called a row, is each individual entry that exists in a table.A record
is a horizontal entity in a table.

Example:
There are 7 records in the above Customers table. Following is a single row of data
or record in the Customers table

2
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
+----+----------+-----+-----------+----------+
COLUMN:

A column is a vertical entity in a table that contains all information associated with
a specific field in a table.

Example:
The ADDRESS column in the Customers table, which represents location
description is shown as below.
+-----------+
| ADDRESS |
+-----------+
| Ahmedabad |
| Delhi |
| Kota |
| Mumbai |
| Bhopal |
| MP |
| Indore |
+----+------+

NULL VALUE:

A NULL value in a table is a value in a field that appears to be blank, which means
a field with a NULL value is a field with no value. If a column in a row has no value
, then column is said to be null.

A NULL value is different from a zero value or a field that contains spaces. A field
with a NULL value is one that has been left blank during record creation.

DATA TYPES.

A data type in SQL server is defined as the type of data that any column or varaiable
can store.

3
1.CHAR (size):
A FIXED length string (can contain letters, numbers, and special characters).
The size parameter specifies the column length in characters - can be from 0 to 255.
Default is 1. Programmers can use this when the length of the characters are known.
2.VARCHAR (size):
A VARIABLE length string (can contain letters, numbers, and special characters).
The size parameter specifies the maximum column length in characters can be from
0 to 65535. Programmers can use this when the data entries length is varying.
Eg: create table student (name varchar(20),gender char(6),phone number int(10));
3.INT
It is used for storing integer values. Allows whole numbers between -2,147,483,648
and 2,147,483,647
4.DATE
It represents the date including day, month and year.
Format used is YYYY-MM-DD.
The supported range is from '1000-01-01' to '9999-12-31'.

CLASSIFICATION OF SQL COMMANDS:

4
1.CREATE DATABASE:
This command is used to create your own database.
Syntax:
create database databasename;
Examples:
1: create database asianschool;
2: create database samson10b;
2. SHOW DATABASES :
This command shows all the databases already created in the system
Syntax:
show databases;
Example:

asianschool

samson10b

information_schema

sql_schema

3. USE DATABASES
This command is used to access the specific database.
Syntax:
use databasename;
Example:
use asianschool;

5
4. DROP DATABASE:

This command is used to is used to drop an existing SQL database.

Syntax:
Drop database databasename;
Example:
drop database asianschool;
5.CREATING A TABLE
The CREATE TABLE statement is used to create a new table in a database.
Create table table_name (
columnname1 datatype1(size),
columnname2 datatype2(size),
columnname3 datatype3(size),
....
);

The column parameters specify the names of the columns of the table.

The datatype parameter specifies the type of data the column can hold (e.g.
varchar, integer, date, etc.

SQL CREATE TABLE Example

The following example creates a table called "Persons" that contains five
columns: PersonID, LastName, FirstName, Address, and City:

Example
CREATE TABLE Persons (
PersonID int(12),
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255));

The PersonID column is of type int and will hold an integer.

The LastName, FirstName, Address, and City columns are of type varchar and
will hold characters, and the maximum length for these fields is 255 characters.

6
6. SHOW TABLES:

This command is used to show all the tables created in the specific database.

Syntax: show tables;

Example:show tables;

7. VIEWING A TABLE STRUCTURE:


Describe command to show the structure of our table, such as column names,
datatypes etc. DESC command is a short form of the DESCRIBE command.
Syntax:
Describe <tablename>; or desc <tablename>;
Or
desc tablename;
Example:
describe jamesjoseph10c;
8.DROP TABLE:
The drop table statement is used to drop an existing table in a database.

Syntax
drop table tablename;
Example:
Drop table jamesjoseph10c;
9. ALTER TABLE COMMAND:
The ALTER TABLE command is used to modify the definition(structure) of a
table by modifying the definition of its columns. The alter table command is
used to perform the following operations

➢ To add a column to an existing table.


➢ To rename any existing column.
➢ To change the datatype of any column or modify its size.
➢ To remove or physically delete a column.

7
a) ADDING A COLUMN TO AN EXISTING TABLE

Syntax:
ALTER TABLE table name ADD (columnname datatype(size));
Example:
ALTER TABLE student ADD (mobileno integer);
b) ADDING A COLUMN WITH DEFAULT VALUE:
Syntax:
ALTER TABLE table name ADD (columnname1 datatype1 default data);
Example:
ALTER TABLE student ADD (city char(6) DEFAULT “delhi”);
c) MODIFYING AN EXISTING COLUMN:
Syntax:
ALTER TABLE table name MODIFY (columnname1 datatype1 );
Example:
ALTER TABLE student modify (city char(6) DEFAULT “Delhi”);
D) RENAMING A COLUMN:
Syntax:
ALTER TABLE table name CHANGE <old name> <newname>
column_definition;
Example:
ALTER TABLE student CHANGE (city State varchar(10));

10.TRUNCATE COMMAND:
The SQL TRUNCATE TABLE command is used to delete complete data from an
existing table.
You can also use DROP TABLE command to delete complete table but it would
remove complete table structure form the database and you would need to re-
create this table once again if you wish you store some data.
Syntax:
TRUNCATE TABLE tablename;

Example:
Truncate table student;

You might also like