SlideShare a Scribd company logo
1 of 7
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
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
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
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)
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;
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...)
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:

More Related Content

Similar to Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values, display records

Similar to Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values, display records (20)

MySQL Essential Training
MySQL Essential TrainingMySQL Essential Training
MySQL Essential Training
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
DBMS.pdf
DBMS.pdfDBMS.pdf
DBMS.pdf
 
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptxhjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
 
SQL
SQLSQL
SQL
 
Assignment#02
Assignment#02Assignment#02
Assignment#02
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
 
Module 3
Module 3Module 3
Module 3
 
Sql intro & ddl 1
Sql intro & ddl 1Sql intro & ddl 1
Sql intro & ddl 1
 
Sql intro & ddl 1
Sql intro & ddl 1Sql intro & ddl 1
Sql intro & ddl 1
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good know
 
SQL.ppt
SQL.pptSQL.ppt
SQL.ppt
 
DBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptxDBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptx
 
SQL DDL
SQL DDLSQL DDL
SQL DDL
 
Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETE
 
lovely
lovelylovely
lovely
 
Oracle sql material
Oracle sql materialOracle sql material
Oracle sql material
 
Les10 Creating And Managing Tables
Les10 Creating And Managing TablesLes10 Creating And Managing Tables
Les10 Creating And Managing Tables
 
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
 

More from SakkaravarthiS1

UNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and MultithreadingUNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and MultithreadingSakkaravarthiS1
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesUNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesSakkaravarthiS1
 
UNIT-1 PPT.pptCS 3492 DBMS UNIT 1 to 5 overview Unit 1 slides including purpo...
UNIT-1 PPT.pptCS 3492 DBMS UNIT 1 to 5 overview Unit 1 slides including purpo...UNIT-1 PPT.pptCS 3492 DBMS UNIT 1 to 5 overview Unit 1 slides including purpo...
UNIT-1 PPT.pptCS 3492 DBMS UNIT 1 to 5 overview Unit 1 slides including purpo...SakkaravarthiS1
 
DATABASE MANAGEMENT SYSTEMS university course materials useful for students ...
DATABASE MANAGEMENT SYSTEMS  university course materials useful for students ...DATABASE MANAGEMENT SYSTEMS  university course materials useful for students ...
DATABASE MANAGEMENT SYSTEMS university course materials useful for students ...SakkaravarthiS1
 
UNIT 5-JavaFX Event Handling, Controls and Components.pdf
UNIT 5-JavaFX Event Handling, Controls and Components.pdfUNIT 5-JavaFX Event Handling, Controls and Components.pdf
UNIT 5-JavaFX Event Handling, Controls and Components.pdfSakkaravarthiS1
 
UNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf NotesUNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf NotesSakkaravarthiS1
 

More from SakkaravarthiS1 (6)

UNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and MultithreadingUNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and Multithreading
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesUNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
 
UNIT-1 PPT.pptCS 3492 DBMS UNIT 1 to 5 overview Unit 1 slides including purpo...
UNIT-1 PPT.pptCS 3492 DBMS UNIT 1 to 5 overview Unit 1 slides including purpo...UNIT-1 PPT.pptCS 3492 DBMS UNIT 1 to 5 overview Unit 1 slides including purpo...
UNIT-1 PPT.pptCS 3492 DBMS UNIT 1 to 5 overview Unit 1 slides including purpo...
 
DATABASE MANAGEMENT SYSTEMS university course materials useful for students ...
DATABASE MANAGEMENT SYSTEMS  university course materials useful for students ...DATABASE MANAGEMENT SYSTEMS  university course materials useful for students ...
DATABASE MANAGEMENT SYSTEMS university course materials useful for students ...
 
UNIT 5-JavaFX Event Handling, Controls and Components.pdf
UNIT 5-JavaFX Event Handling, Controls and Components.pdfUNIT 5-JavaFX Event Handling, Controls and Components.pdf
UNIT 5-JavaFX Event Handling, Controls and Components.pdf
 
UNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf NotesUNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf Notes
 

Recently uploaded

OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 

Recently uploaded (20)

OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 

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: