SlideShare a Scribd company logo
2
Most read
6
Most read
7
Most read
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

DOCX
SQL Tutorial for BCA-2
PPTX
Unit - II.pptx
PPTX
Lecture - MY-SQL/ SQL Commands - DDL.pptx
PPTX
SQL: Data Definition Language(DDL) command
PPT
chapter 8 SQL.ppt
PPTX
introdution to SQL and SQL functions
PPTX
SQL commands powerpoint presentation. Ppt
PDF
DBMS.pdf
SQL Tutorial for BCA-2
Unit - II.pptx
Lecture - MY-SQL/ SQL Commands - DDL.pptx
SQL: Data Definition Language(DDL) command
chapter 8 SQL.ppt
introdution to SQL and SQL functions
SQL commands powerpoint presentation. Ppt
DBMS.pdf

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

DOCX
unit-5 sql notes.docx
PPTX
SQL commands in database managemant systems
PDF
SQL Queries - DDL Commands
PPTX
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
PPTX
lovely
PPTX
Introduction to SQl Commands.pptxhhjhvvb
PPTX
SQL.pptx for the begineers and good know
PPTX
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
PDF
Dms 22319 micro project
PPTX
sql.pptx
PPTX
SQL_all_commnads_aggregate_functions.pptx
PPTX
SQl data base management and design
PPTX
SQL(DDL & DML)
PPTX
PDF
SQL for data scientist And data analysist Advanced
PPTX
SQL.pptx structure query language in database management system
PPTX
SQL-1.pptx for database system and system query language
PPTX
Database Languages power point presentation
PPTX
DBMS UNIT-2.pptx ggggggggggggggggggggggg
PPTX
Relational Database Language.pptx
unit-5 sql notes.docx
SQL commands in database managemant systems
SQL Queries - DDL Commands
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
lovely
Introduction to SQl Commands.pptxhhjhvvb
SQL.pptx for the begineers and good know
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
Dms 22319 micro project
sql.pptx
SQL_all_commnads_aggregate_functions.pptx
SQl data base management and design
SQL(DDL & DML)
SQL for data scientist And data analysist Advanced
SQL.pptx structure query language in database management system
SQL-1.pptx for database system and system query language
Database Languages power point presentation
DBMS UNIT-2.pptx ggggggggggggggggggggggg
Relational Database Language.pptx
Ad

More from SakkaravarthiS1 (6)

PPTX
UNIT-3.pptx Exception Handling and Multithreading
PPTX
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
PPT
UNIT-1 PPT.pptCS 3492 DBMS UNIT 1 to 5 overview Unit 1 slides including purpo...
PDF
DATABASE MANAGEMENT SYSTEMS university course materials useful for students ...
PDF
UNIT 5-JavaFX Event Handling, Controls and Components.pdf
PDF
UNIT4-IO,Generics,String Handling.pdf Notes
UNIT-3.pptx Exception Handling and Multithreading
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...
DATABASE MANAGEMENT SYSTEMS university course materials useful for students ...
UNIT 5-JavaFX Event Handling, Controls and Components.pdf
UNIT4-IO,Generics,String Handling.pdf Notes
Ad

Recently uploaded (20)

PDF
Structs to JSON How Go Powers REST APIs.pdf
PDF
Digital Logic Computer Design lecture notes
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
DOCX
573137875-Attendance-Management-System-original
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Construction Project Organization Group 2.pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
composite construction of structures.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
PPT on Performance Review to get promotions
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Structs to JSON How Go Powers REST APIs.pdf
Digital Logic Computer Design lecture notes
Arduino robotics embedded978-1-4302-3184-4.pdf
573137875-Attendance-Management-System-original
Operating System & Kernel Study Guide-1 - converted.pdf
Construction Project Organization Group 2.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
composite construction of structures.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
OOP with Java - Java Introduction (Basics)
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPT on Performance Review to get promotions
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Strings in CPP - Strings in C++ are sequences of characters used to store and...
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Foundation to blockchain - A guide to Blockchain Tech
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx

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: