Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values, display records
1. SQL (pronounced sequel) is an acronym for Structured Query Language, a
standardized language used to access and manipulate data.
SQL enables a programmer or database administrator to do the following:
Modify a database's structure
Change system security settings
Add user permissions on databases or tables
Query a database for information
Update the contents of a database
Guidelines for Writing SQL statements
1. SQL statements are not case sensitive.
2. SQL statements can be on one or more lines.
3. SQL is a free form language.
4. SQL keywords are typically entered in uppercase; all other words such as
table and column names are entered in lowercase.
5. SQL Statements are terminated with a semi-colon.
6. SQL won't execute the query until it finds a semicolon
Language SQL Commands
Data Definition Language
(DDL)
1. CREATE
2. ALTER
3. DROP
4. TRUNCATE
5. RENAME
Data Manipulation Language
(DML)
1. INSERT
2. UPDATE
3. DELETE
Data Query Language (DQL) SELECT
Data Control Language
(DCL)
4. GRANT
5. REVOKE
Transaction Control Language
(TCL)
6. COMMIT
7. ROLLBACK
8. SAVEPOINT
2. STRUCTURE OF RELATIONAL DATABASES:
RDBMS in which data is stored in the form of tables. A relational
database consists of a collection of tables, each of which is assigned a unique
name.
BASIC STRUCTURE
Each column header is attributes. Each attribute allows a set of permitted
values called domain of that attribute.
Table is called as a relation and rows in a table are called as tuples.
The tuples in a relation can be either sorted or unsorted.
Several attributes can have same domain. E.g.: customer_name,
employee_name.
Attributes can also be distinct. E.g.: balance, branch_name
Attributes can have null values incase if the value is unknown or does
not exist.
Account Table
What is Query?
A query is a question.
A query is formulated for a relation/table to retrieve some useful
information from the table.
Data Definition Language (DDL) Commands:
Command Description
CREATE Creates a new table, a view of a table in the database
ALTER Modifies an existing table; used to add a new column,
modify the existing column definition and to include or
drop integrity constraint.
DROP Deletes an entire table, a view of a table in the database.
It will delete the table structure
3. TRUNCATE Truncates the table values without deleting table
structure (the records alone will be deleted)
RENAME This is used to change the name of the table
DESC This is used to view the structure of the table
Table Creation Rules:
1. Reserved words cannot be used as table name
2. The first character of the name must be a letter between A and Z, the
remaining characters can be letters or the symbols _, #, $, and @.
3. Underscore, numerals, letters are allowed but not blank space.
4. Maximum length for the table name is 30 characters.
5. 2 different tables should not have same name.
6. We should specify a unique column name for primary key.
7. We should specify proper data type along with width. (Domain type)
8. We can include “not null” condition when needed. By default, it is ‘null’.
The Field's Data Type – Domain type
Data Type Comments
CHAR Alphanumeric data with a length between 1 and 255
characters. Spaces are padded to the right of the value to
supplement the total allocated length of the column.
DATE Included as part of the date as century, year, month, day, hour,
minute, and second.
NUMBER Numeric 0, positive or negative fixed or floating-point data.
VARCHAR2 Alphanumeric data that is variable length; this field must be
between 1 and 2,000 characters long.
The CREATE Syntax
CREATE TABLE <table name>
(
fieldname-1datatype constraints if any,
fieldname-2 datatype constraints if any,
.......
fieldname-n datatype constraints if any,
);
Example
4. SQL> CREATE TABLE Customer
(
Customer_id int,
Customer_name varchar2(20),
Address varchar2(30),
City varchar2(15)
);
Primary key - it uniquely identifies a row within a table. A table may have only
one primary key, which consists of one or more columns. If the primary key
contains multiple columns it is referred to as a composite primary key or
concatenated primary key.
Syntax
CREATE TABLE <tableName> (<attribute_name> <type> PRIMARY KEY);
CREATE TABLE Student
(
Regno int PRIMARY KEY,
Name varchar2(30),
Marks int
);
Describing the structure of the table
After a table has been created, it may be necessary to determine the name,
data type and some of the constraints of the attributes that compose the table. The
command used is DESC. The syntax of this command is
Syntax
DESC table-name;
SQL> DESC Customer;
Name Type
-----------------------------------------------------
Customer_id int
Customer_name varchar2(20)
Address varchar2(30)
City varchar2(15)
5. The ALTER TABLE Statement
Many times your database design does not account for everything it should.
The ALTER TABLE statement enables the database administrator or
designer to change the structure of a table after it has been created.
The ALTER TABLE command enables you to do two things:
Add a column to an existing table
Modify a column that already exists
Add constraints to an existing table
Removing constraints from a table
The syntax for adding and modifying columns:
ALTER TABLE table_name
<ADD column_name data_type; | MODIFY column_name data_type ;>
Example:
ALTER TABLE Customer ADD DateOfBirth int;
Customer_Id Customer_Name Address City DateOfBirth
Change Data Type Example
Now we want to change the data type of the column named "DateOfBirth"
in the "Customers" table. We use the following SQL statement:
Syntax
ALTER TABLE Customers MODIFY COLUMN DateOfBirth year;
If we want to remove a column then
ALTER TABLE Persons DROP COLUMN DateOfBirth;
The DROP TABLE Statement
SQL provides a command to completely remove a table from a database. The
DROP TABLE command deletes a table along with all its associated views and
indexes. After this command has been issued, there is no turning back.
SYNTAX:
DROP TABLE table_name;
6. Example:
DROP TABLE Orders;
The TRUNCATE TABLE Statement
An easier and faster way of removing all rows from a table is using the
TRUNCATE command.
Syntax
TRUNCATE TABLE tablename;
Example
TRUNCATE TABLE Customer;
Data Manipulation Language Commands in SQL
DML commands are the most frequently used SQL commands and is used to
query and manipulate the existing database objects. Some of the commands are
Insert
Update
Delete
The data manipulation statements are:
The INSERT statement
The UPDATE statement
The DELETE statement
The INSERT...VALUES Statement
The INSERT...VALUES statement enters data into a table one record at a
time.
The first form does not specify the column names where the data will be inserted,
only their values:
INSERT INTO table_name VALUES (value1, value2...)
The second form specifies both the column names and the values to be inserted:
INSERT INTO table_name (col1, col2...) VALUES (value1, value2...)
7. The DELETE Statement
In addition to adding data to a database, you will also need to delete data
from a database. The syntax for the DELETE statement is
Syntax
DELETE FROM tablename WHERE condition;
Depending on the use of the DELETE statement's WHERE clause, SQL can do
the following:
Delete single rows
Delete multiple rows
Delete all rows
Here are several points to remember when using the DELETE statement:
The DELETE statement cannot delete an individual field's values. The
DELETE statement deletes entire records from a single table.
Using the DELETE statement deletes only records, not the table itself.
Use the DROP TABLE statement to remove an entire table.
Delete All Data - It is possible to delete all rows in a table without deleting the
table. This means that the table structure, attributes, and indexes will be intact:
DELETE FROM table_name; (OR)
DELETE * FROM table_name;
Exercise: