Structured Query Language - Part I
Structured Query Language - Part I
Structured Query Language (SQL) is a standard language used for accessing databases. This is a special purpose
programming language used to create a table, manage data, and manipulate data.
i) Querying data
The SQL statements are categorized into different categories based upon the purpose. Out of them, we will be studying
only two types:
Data Definition Language (DDL) or Data Description Language (DDL) is a standard for commands that defines the
different structures in a database. DDL statements are used to create structure of a table, modify the existing structure
of the table, and remove the existing table. Some of the DDL statements are CREATE TABLE, ALTER TABLE and DROP
TABLE.
Data Manipulation Language (DML) statements are used to access and manipulate data in existing tables. The
manipulation includes inserting data into tables, deleting data from the tables, retrieving data, and modifying the
existing data. The common DML statements are UPDATE, DELETE and INSERT.
Create a table
Changes Database Definition
Removes a table
Add a column
DDL
Remove a column
Change a constraint
Add rows
Data type of an attribute indicates the type of data value that an attribute can have. It also decides the operations that
can be performed on the data of that attribute. For example, arithmetic operations can be performed on numeric data
but not on character data. Commonly used data types in MySQL are numeric types, date and time types, and string types
VARCHAR(n) • Specifies character type data of length where n could be any value from 0 to 65535.
• But unlike CHAR, VARCHAR(n) is a variable-length data type. That is, declaring VARCHAR (30)
means a maximum of 30 characters can be stored but the actual allocated bytes will depend on the
length of entered string. So ‘city’ in VARCHAR (30) will occupy space needed to store 4 characters
only.
INT • INT specifies an integer value.
• Each INT value occupies 4 bytes of storage.
• The range of unsigned values allowed in INT type are 0 to 4,294,967,295.
• For values larger than that, we must use BIGINT, which occupies 8 bytes.
At any point of time, we can view names of all the databases contained in the server by using statement SHOW
DATABASES.
Syntax:
SHOW DATABASES;
Syntax:
E.g.
USE statement:
To open the database to work on it (to make the database current), USE statement is used.
Syntax:
USE <databaename>
E.g.
USE School;
To create new relations(tables) in the database, the CREATE TABLE statement is used.
Syntax:
CREATE TABLE <tablename>(
E.g.
Name VARCHAR(25) );
• The number of columns in a table defines the degree of that relation, which is denoted by N.
Constraints
Constraints are rules imposed on various attributes of a relation, which help usto ensure validity of the data.
Constraint Purpose
PRIMARY Sets a column or a group of columns as the Primary Key of a table. Therefore, NULLs and duplicate
KEY values in this column are not accepted.
NOT NULL Makes sure that NULLs are not accepted in the specified column.
UNIQUE Makes sure that duplicate values in the specified column are not accepted.
FOREIGN Data will be accepted in this column if same data value exists in a column in another related table. This
KEY other related table name and column name are specified while creating the foreign key constraint.
To view names of all the tables contained in the current database we use SHOW TABLES statement.
Syntax:
SHOW TABLES;
DESCRIBE/DESC statement
The DESCRIBE statement can be used to see the structure of a table as indicated in the Create Statement. It displays the
Column names, their data types, whether Column must contain data, whether the Column is a Primary key etc.
Syntax:
E.g.
DESC Learner;
To add/remove an attribute or to modify the datatype of an existing attribute or to add/remove constraint to attributes,
we can change or alter the structure (schema) of the table by using the ALTER TABLE statement.
Syntax:
E.g.
Syntax:
E.g.
To add primary key to a table (When it does not have one already):
Syntax:
E.g.
Syntax:
E.g.
Syntax:
E.g.
We can use a DROP TABLE statement to remove a table permanently from the database.
Syntax:
E.g.
We can use a DROP DATABASE statement to remove a database permanently from the system.
Syntax:
E.g.