SlideShare ist ein Scribd-Unternehmen logo
1 von 18
Prof. Neeraj Bhargava
Pooja Dixit
Department of Computer Science
School of Engineering & System Science
MDS, University Ajmer, Rajasthan, India
1
2
3
4
 SQL commands are instructions, coded into SQL statements, which are used
to communicate with the database to perform specific tasks, work, functions
and queries with data.
 SQL commands can be used not only for searching the database but also to
perform various other functions like, for example, you can create tables, add
data to tables, or modify data, drop the table, set permissions for users. SQL
commands are grouped into four major categories depending on their
functionality:
 Data Definition Language (DDL) - These SQL commands are used for
creating, modifying, and dropping the structure of database objects. The
commands are CREATE, ALTER, DROP, RENAME, and TRUNCATE.
 Data Manipulation Language (DML) - These SQL commands are used for
storing, retrieving, modifying, and deleting data.
These Data Manipulation Language commands are: SELECT, INSERT, UPDATE,
and DELETE.
 Transaction Control Language (TCL) - These SQL commands are used for
managing changes affecting the data. These commands are COMMIT,
ROLLBACK, and SAVEPOINT.
 Data Control Language (DCL) - These SQL commands are used for providing
security to database objects. These commands are GRANT and REVOKE.
5
The Create Table Command
The create table command defines each column of the table
uniquely. Each column
has minimum of three attributes.
 Name
 Data type
 Size(column width).
Each table column definition is a single clause in the create table
syntax. Each table column definition is separated from the other
by a comma. Finally, the SQL statement is terminated with a
semicolon.
Syntax:
CREATE TABLE <table_name>
(
<column_name1> <datatype1> <constraint1>
<column_name2> <datatype2> <constraint2>
<constraint-list>
) ;
6
ALTER command
 SQL command can be used to add, modify, or drop a column from the existing
table or to rename a table.
ALTER TABLE - ADD Column
 To add a column in a table, use the following syntax:
 ALTER TABLE table_name
ADD column_name datatype;
 Eg: alter table clint_master add (telephone number(10));
ALTER TABLE - DROP COLUMN
 To delete a column in a table, use the following syntax (notice that some
database systems don't allow deleting a column):
ALTER TABLE table_name
DROP COLUMN column_name;
Eg: alter table clint_master
drop column address2;
ALTER TABLE - ALTER/MODIFY COLUMN
 To change the data type of a column in a table, use the following syntax:
 ALTER TABLE table_name
MODIFY column_name datatype;
Eg: Alter table product_master modify(sellprice number(10,2));
7
The SQL DROP TABLE Statement
 The DROP TABLE statement is used to drop an existing table in a
database.
 Syntax
 DROP TABLE table_name;
 drop table clint_master;
SQL TRUNCATE TABLE
 The TRUNCATE TABLE statement is used to delete the data inside a table,
but not the table itself.
 Syntax
 TRUNCATE TABLE table_name;
 TRUNCATE TABLE clint_master;
RENAME command
 The SQL command that removes the entire table.
 Syntax:
 RENAME TO old_table_name to new_table_name;
 Rename salesman_master to sman_mast;
8
 SQL constraints are used to specify rules for data in a table.
 SQL Create Constraints
 Constraints can be specified when the table is created with the CREATE TABLE statement, or
after the table is created with the ALTER TABLE statement.
 Syntax
 CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
 The following constraints are commonly used in SQL:
 NOT NULL - Ensures that a column cannot have a NULL value
 UNIQUE - Ensures that all values in a column are different
 PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a
table
 FOREIGN KEY - Uniquely identifies a row/record in another table
 CHECK - Ensures that all values in a column satisfies a specific condition
 DEFAULT - Sets a default value for a column when no value is specified
 INDEX - Used to create and retrieve data from the database very quickly
9
SQL NOT NULL Constraint
 By default, a column can hold NULL values.
 The NOT NULL constraint enforces a column to NOT accept NULL values.
 This enforces a field to always contain a value, which means that you cannot
insert a new record, or update a record without adding a value to this field.
 The following SQL ensures that the "ID", "LastName", and "FirstName"
columns will NOT accept NULL values:
 Example
 CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
SQL UNIQUE Constraint
 The UNIQUE constraint ensures that all values in a column are different.
 Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for
uniqueness for a column or set of columns.
 A PRIMARY KEY constraint automatically has a UNIQUE constraint.
10
Primary Key
 Each table must normally contain a column or set of columns that uniquely identifies rows
of data that are stored in the table.This column or set of columns is referred to as the
primary key.Most tables have a single column as the primary key.Primary key columns are
restricted against NULLs and duplicate values.
 Points to be noted -
 A table can have only one primary key.
 Multiple columns can be clubbed under a composite primary key.
 Oracle internally creates unique index to prevent duplication in the column values.Indexes
would be discussed later in PL/SQL.
 Syntax:
Column level:
◦ COLUMN [data type] [CONSTRAINT <constraint name> PRIMARY KEY]
Table level:
CONSTRAINT [constraint name] PRIMARY KEY [column (s)]
The following example shows how to use PRIMARY KEY constraint at column level.
CREATE TABLE TEST ( ID NUMBER CONSTRAINT TEST_PK PRIMARY KEY, ... );
The following example shows how to define composite primary key using PRIMARY KEY
constraint at the table level.
◦ CREATE TABLE TEST ( ..., CONSTRAINT TEST_PK PRIMARY KEY (ID) );
11
Foreign Key
 When two tables share the parent child relationship based on specific
column, the joining column in the child table is known as Foreign Key.This
property of corresponding column in the parent table is known as Referential
integrity.Foreign Key column values in the child table can either be null or
must be the existing values of the parent table.Please note that only primary
key columns of the referenced table are eligible to enforce referential
integrity.
 Syntax:
 Column Level:
 COLUMN [data type] [CONSTRAINT] [constraint name] [REFERENCES] [table
name (column name)]
 Table level:
 CONSTRAINT [constraint name] [FOREIGN KEY (foreign key column name)
REFERENCES] [referenced table name (referenced column name)]The following
example shows how to use FOREIGN KEY constraint at column level.
 CREATE TABLE TEST (ccode varchar2(5) CONSTRAINT TEST_FK REFERENCES
PARENT_TEST(ccode), ... );
12
Check constraint
 Sometimes the data values stored in a specific column must fall within some
acceptable range of values.A CHECK constraint requires that the specified
check condition is either true or unknown for each row stored in the
table.Check constraint allows to impose a conditional rule on a column, which
must be validated before data is inserted into the column. The condition must
not contain a sub query or pseudo column CURRVAL NEXTVAL, LEVEL,
ROWNUM, or SYSDATE.
 Oracle allows a single column to have more than one CHECK constraint. In
fact, there is no practical limit to the number of CHECK constraints that can be
defined for a column.
Syntax:
Column level:
 COLUMN [data type] CONSTRAINT [name] [CHECK (condition)]
Table level:
 CONSTRAINT [name] CHECK (condition)The following example shows how to
use CHECK constraint at column level.
 CREATE TABLE TEST ( ..., GRADE char (1) CONSTRAINT TEST_CHK CHECK
(upper (GRADE) in ('A','B','C')), ... );The following example shows how to use
CHECK constraint at table level.
 CREATE TABLE TEST ( ..., CONSTRAINT TEST_CHK CHECK (stdate < = enddate),
);
13
 DML(Data Manipulation Language) : The SQL commands that deals
with the manipulation of data present in database belong to DML
or Data Manipulation Language and this includes most of the SQL
statements.Examples of DML:
 SELECT – is used to retrieve data from the a database.
 INSERT – is used to insert data into a table.
 UPDATE – is used to update existing data within a table.
 DELETE – is used to delete records from a database table.
14
INSERT command
 Insert command is used to insert data into a table. Following
is its general syntax,
 INSERT INTO table_name VALUES(data1, data2, ...)
 Insert value into only specific columns
 We can use the INSERT command to insert values for only
some specific columns of a row. We can specify the column
names along with the values to be inserted like this,
 INSERT INTO student(id, name) values(102, 'Alex');The above
SQL query will only insert id and name values in the newly
inserted record.
15
UPDATE command
 UPDATE command is used to update any record of data in a table. Following
is its general syntax,
 UPDATE table_name SET column_name = new_value WHERE some_condition;
 WHERE is used to add a condition to any SQL query, we will soon study
about it in detail.
 Updating Multiple Columns
 We can also update values of multiple columns using a
single UPDATE statement.
 UPDATE student SET name='Abhi', age=17 where s_id=103;
DELETE command
 DELETE command is used to delete data from a table.
Syntax,
 DELETE FROM table_name;
 Delete all Records from a Table
 Eg: DELETE FROM student;
The above command will delete all the records from the table student.
 Delete a particular Record from a Table
In our student table if we want to delete a single record, we can use
the WHERE clause to provide a condition in our DELETE statement.
Eg : DELETE FROM student WHERE s_id=103;The above command will delete
the record where s_id is 103 from the table student.
16
 Transaction Control Language(TCL) commands are used to manage transactions in
the database. These are used to manage the changes made to the data in a table
by DML statements. It also allows statements to be grouped together into logical
transactions.
COMMIT command
 COMMIT command is used to permanently save any transaction into the database.
 When we use any DML command like INSERT, UPDATE or DELETE, the changes
made by these commands are not permanent, until the current session is closed,
the changes made by these commands can be rolled back.
 To avoid that, we use the COMMIT command to mark the changes as permanent.
 Following is commit command's syntax,
 COMMIT;
ROLLBACK command
 This command restores the database to last commited state. It is also used
with SAVEPOINT command to jump to a savepoint in an ongoing transaction.
 If we have used the UPDATE command to make some changes into the database,
and realise that those changes were not required, then we can use
the ROLLBACK command to rollback those changes, if they were not commited
using the COMMIT command.
 Following is rollback command's syntax,
 ROLLBACK TO savepoint_name;
17
SAVEPOINT command
 SAVEPOINT command is used to temporarily save a
transaction so that you can rollback to that point whenever
required.
 Following is savepoint command's syntax,
 SAVEPOINT savepoint_name; In short, using this command we
can name the different states of our data in any table and
then rollback to that state using the ROLLBACK command
whenever required.
18

Weitere ähnliche Inhalte

Was ist angesagt?

SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
Vikas Gupta
 

Was ist angesagt? (20)

introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
Mysql
MysqlMysql
Mysql
 
Chapter 4 Structured Query Language
Chapter 4 Structured Query LanguageChapter 4 Structured Query Language
Chapter 4 Structured Query Language
 
DATABASE CONSTRAINTS
DATABASE CONSTRAINTSDATABASE CONSTRAINTS
DATABASE CONSTRAINTS
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
SQL
SQLSQL
SQL
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
 
MYSQL
MYSQLMYSQL
MYSQL
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
SQL
SQLSQL
SQL
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 
Sql commands
Sql commandsSql commands
Sql commands
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
 
Sql and Sql commands
Sql and Sql commandsSql and Sql commands
Sql and Sql commands
 

Ähnlich wie Sql commands

2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used
TheVerse1
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
Belle Wx
 
Sql integrity constraints
Sql integrity constraintsSql integrity constraints
Sql integrity constraints
Vivek Singh
 
SQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfSQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdf
DraguClaudiu
 
Web app development_my_sql_09
Web app development_my_sql_09Web app development_my_sql_09
Web app development_my_sql_09
Hassen Poreya
 

Ähnlich wie Sql commands (20)

Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
 
SQL Query
SQL QuerySQL Query
SQL Query
 
SQL
SQLSQL
SQL
 
Sql smart reference_by_prasad
Sql smart reference_by_prasadSql smart reference_by_prasad
Sql smart reference_by_prasad
 
Sql smart reference_by_prasad
Sql smart reference_by_prasadSql smart reference_by_prasad
Sql smart reference_by_prasad
 
Sql basics
Sql basicsSql basics
Sql basics
 
2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
Sql integrity constraints
Sql integrity constraintsSql integrity constraints
Sql integrity constraints
 
COMPUTERS SQL
COMPUTERS SQL COMPUTERS SQL
COMPUTERS SQL
 
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 wksht-2
Sql wksht-2Sql wksht-2
Sql wksht-2
 
Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETE
 
SQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfSQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdf
 
Oracle sql material
Oracle sql materialOracle sql material
Oracle sql material
 
Introduction to SQL..pdf
Introduction to SQL..pdfIntroduction to SQL..pdf
Introduction to SQL..pdf
 
Dms 22319 micro project
Dms 22319 micro projectDms 22319 micro project
Dms 22319 micro project
 
Web app development_my_sql_09
Web app development_my_sql_09Web app development_my_sql_09
Web app development_my_sql_09
 
Assignment#02
Assignment#02Assignment#02
Assignment#02
 
chapter 8 SQL.ppt
chapter 8 SQL.pptchapter 8 SQL.ppt
chapter 8 SQL.ppt
 

Mehr von Pooja Dixit

Mehr von Pooja Dixit (20)

Combinational circuit.pptx
Combinational circuit.pptxCombinational circuit.pptx
Combinational circuit.pptx
 
number system.pptx
number system.pptxnumber system.pptx
number system.pptx
 
Multiplexer.pptx
Multiplexer.pptxMultiplexer.pptx
Multiplexer.pptx
 
Logic Gates.pptx
Logic Gates.pptxLogic Gates.pptx
Logic Gates.pptx
 
K-Map.pptx
K-Map.pptxK-Map.pptx
K-Map.pptx
 
Karnaugh Map Simplification Rules.pptx
Karnaugh Map Simplification Rules.pptxKarnaugh Map Simplification Rules.pptx
Karnaugh Map Simplification Rules.pptx
 
Half Subtractor.pptx
Half Subtractor.pptxHalf Subtractor.pptx
Half Subtractor.pptx
 
Gray Code.pptx
Gray Code.pptxGray Code.pptx
Gray Code.pptx
 
Flip Flop.pptx
Flip Flop.pptxFlip Flop.pptx
Flip Flop.pptx
 
Encoder.pptx
Encoder.pptxEncoder.pptx
Encoder.pptx
 
De-multiplexer.pptx
De-multiplexer.pptxDe-multiplexer.pptx
De-multiplexer.pptx
 
DeMorgan’s Theory.pptx
DeMorgan’s Theory.pptxDeMorgan’s Theory.pptx
DeMorgan’s Theory.pptx
 
Combinational circuit.pptx
Combinational circuit.pptxCombinational circuit.pptx
Combinational circuit.pptx
 
Boolean Algebra.pptx
Boolean Algebra.pptxBoolean Algebra.pptx
Boolean Algebra.pptx
 
Binary Multiplication & Division.pptx
Binary Multiplication & Division.pptxBinary Multiplication & Division.pptx
Binary Multiplication & Division.pptx
 
Binary addition.pptx
Binary addition.pptxBinary addition.pptx
Binary addition.pptx
 
Basics of Computer Organization.pptx
Basics of Computer Organization.pptxBasics of Computer Organization.pptx
Basics of Computer Organization.pptx
 
Decoders
DecodersDecoders
Decoders
 
Three Address code
Three Address code Three Address code
Three Address code
 
Cyrus beck line clipping algorithm
Cyrus beck line clipping algorithmCyrus beck line clipping algorithm
Cyrus beck line clipping algorithm
 

Kürzlich hochgeladen

Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 

Kürzlich hochgeladen (20)

Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEGEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 

Sql commands

  • 1. Prof. Neeraj Bhargava Pooja Dixit Department of Computer Science School of Engineering & System Science MDS, University Ajmer, Rajasthan, India 1
  • 2. 2
  • 3. 3
  • 4. 4
  • 5.  SQL commands are instructions, coded into SQL statements, which are used to communicate with the database to perform specific tasks, work, functions and queries with data.  SQL commands can be used not only for searching the database but also to perform various other functions like, for example, you can create tables, add data to tables, or modify data, drop the table, set permissions for users. SQL commands are grouped into four major categories depending on their functionality:  Data Definition Language (DDL) - These SQL commands are used for creating, modifying, and dropping the structure of database objects. The commands are CREATE, ALTER, DROP, RENAME, and TRUNCATE.  Data Manipulation Language (DML) - These SQL commands are used for storing, retrieving, modifying, and deleting data. These Data Manipulation Language commands are: SELECT, INSERT, UPDATE, and DELETE.  Transaction Control Language (TCL) - These SQL commands are used for managing changes affecting the data. These commands are COMMIT, ROLLBACK, and SAVEPOINT.  Data Control Language (DCL) - These SQL commands are used for providing security to database objects. These commands are GRANT and REVOKE. 5
  • 6. The Create Table Command The create table command defines each column of the table uniquely. Each column has minimum of three attributes.  Name  Data type  Size(column width). Each table column definition is a single clause in the create table syntax. Each table column definition is separated from the other by a comma. Finally, the SQL statement is terminated with a semicolon. Syntax: CREATE TABLE <table_name> ( <column_name1> <datatype1> <constraint1> <column_name2> <datatype2> <constraint2> <constraint-list> ) ; 6
  • 7. ALTER command  SQL command can be used to add, modify, or drop a column from the existing table or to rename a table. ALTER TABLE - ADD Column  To add a column in a table, use the following syntax:  ALTER TABLE table_name ADD column_name datatype;  Eg: alter table clint_master add (telephone number(10)); ALTER TABLE - DROP COLUMN  To delete a column in a table, use the following syntax (notice that some database systems don't allow deleting a column): ALTER TABLE table_name DROP COLUMN column_name; Eg: alter table clint_master drop column address2; ALTER TABLE - ALTER/MODIFY COLUMN  To change the data type of a column in a table, use the following syntax:  ALTER TABLE table_name MODIFY column_name datatype; Eg: Alter table product_master modify(sellprice number(10,2)); 7
  • 8. The SQL DROP TABLE Statement  The DROP TABLE statement is used to drop an existing table in a database.  Syntax  DROP TABLE table_name;  drop table clint_master; SQL TRUNCATE TABLE  The TRUNCATE TABLE statement is used to delete the data inside a table, but not the table itself.  Syntax  TRUNCATE TABLE table_name;  TRUNCATE TABLE clint_master; RENAME command  The SQL command that removes the entire table.  Syntax:  RENAME TO old_table_name to new_table_name;  Rename salesman_master to sman_mast; 8
  • 9.  SQL constraints are used to specify rules for data in a table.  SQL Create Constraints  Constraints can be specified when the table is created with the CREATE TABLE statement, or after the table is created with the ALTER TABLE statement.  Syntax  CREATE TABLE table_name ( column1 datatype constraint, column2 datatype constraint, column3 datatype constraint, .... );  The following constraints are commonly used in SQL:  NOT NULL - Ensures that a column cannot have a NULL value  UNIQUE - Ensures that all values in a column are different  PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table  FOREIGN KEY - Uniquely identifies a row/record in another table  CHECK - Ensures that all values in a column satisfies a specific condition  DEFAULT - Sets a default value for a column when no value is specified  INDEX - Used to create and retrieve data from the database very quickly 9
  • 10. SQL NOT NULL Constraint  By default, a column can hold NULL values.  The NOT NULL constraint enforces a column to NOT accept NULL values.  This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.  The following SQL ensures that the "ID", "LastName", and "FirstName" columns will NOT accept NULL values:  Example  CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255) NOT NULL, Age int ); SQL UNIQUE Constraint  The UNIQUE constraint ensures that all values in a column are different.  Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of columns.  A PRIMARY KEY constraint automatically has a UNIQUE constraint. 10
  • 11. Primary Key  Each table must normally contain a column or set of columns that uniquely identifies rows of data that are stored in the table.This column or set of columns is referred to as the primary key.Most tables have a single column as the primary key.Primary key columns are restricted against NULLs and duplicate values.  Points to be noted -  A table can have only one primary key.  Multiple columns can be clubbed under a composite primary key.  Oracle internally creates unique index to prevent duplication in the column values.Indexes would be discussed later in PL/SQL.  Syntax: Column level: ◦ COLUMN [data type] [CONSTRAINT <constraint name> PRIMARY KEY] Table level: CONSTRAINT [constraint name] PRIMARY KEY [column (s)] The following example shows how to use PRIMARY KEY constraint at column level. CREATE TABLE TEST ( ID NUMBER CONSTRAINT TEST_PK PRIMARY KEY, ... ); The following example shows how to define composite primary key using PRIMARY KEY constraint at the table level. ◦ CREATE TABLE TEST ( ..., CONSTRAINT TEST_PK PRIMARY KEY (ID) ); 11
  • 12. Foreign Key  When two tables share the parent child relationship based on specific column, the joining column in the child table is known as Foreign Key.This property of corresponding column in the parent table is known as Referential integrity.Foreign Key column values in the child table can either be null or must be the existing values of the parent table.Please note that only primary key columns of the referenced table are eligible to enforce referential integrity.  Syntax:  Column Level:  COLUMN [data type] [CONSTRAINT] [constraint name] [REFERENCES] [table name (column name)]  Table level:  CONSTRAINT [constraint name] [FOREIGN KEY (foreign key column name) REFERENCES] [referenced table name (referenced column name)]The following example shows how to use FOREIGN KEY constraint at column level.  CREATE TABLE TEST (ccode varchar2(5) CONSTRAINT TEST_FK REFERENCES PARENT_TEST(ccode), ... ); 12
  • 13. Check constraint  Sometimes the data values stored in a specific column must fall within some acceptable range of values.A CHECK constraint requires that the specified check condition is either true or unknown for each row stored in the table.Check constraint allows to impose a conditional rule on a column, which must be validated before data is inserted into the column. The condition must not contain a sub query or pseudo column CURRVAL NEXTVAL, LEVEL, ROWNUM, or SYSDATE.  Oracle allows a single column to have more than one CHECK constraint. In fact, there is no practical limit to the number of CHECK constraints that can be defined for a column. Syntax: Column level:  COLUMN [data type] CONSTRAINT [name] [CHECK (condition)] Table level:  CONSTRAINT [name] CHECK (condition)The following example shows how to use CHECK constraint at column level.  CREATE TABLE TEST ( ..., GRADE char (1) CONSTRAINT TEST_CHK CHECK (upper (GRADE) in ('A','B','C')), ... );The following example shows how to use CHECK constraint at table level.  CREATE TABLE TEST ( ..., CONSTRAINT TEST_CHK CHECK (stdate < = enddate), ); 13
  • 14.  DML(Data Manipulation Language) : The SQL commands that deals with the manipulation of data present in database belong to DML or Data Manipulation Language and this includes most of the SQL statements.Examples of DML:  SELECT – is used to retrieve data from the a database.  INSERT – is used to insert data into a table.  UPDATE – is used to update existing data within a table.  DELETE – is used to delete records from a database table. 14
  • 15. INSERT command  Insert command is used to insert data into a table. Following is its general syntax,  INSERT INTO table_name VALUES(data1, data2, ...)  Insert value into only specific columns  We can use the INSERT command to insert values for only some specific columns of a row. We can specify the column names along with the values to be inserted like this,  INSERT INTO student(id, name) values(102, 'Alex');The above SQL query will only insert id and name values in the newly inserted record. 15
  • 16. UPDATE command  UPDATE command is used to update any record of data in a table. Following is its general syntax,  UPDATE table_name SET column_name = new_value WHERE some_condition;  WHERE is used to add a condition to any SQL query, we will soon study about it in detail.  Updating Multiple Columns  We can also update values of multiple columns using a single UPDATE statement.  UPDATE student SET name='Abhi', age=17 where s_id=103; DELETE command  DELETE command is used to delete data from a table. Syntax,  DELETE FROM table_name;  Delete all Records from a Table  Eg: DELETE FROM student; The above command will delete all the records from the table student.  Delete a particular Record from a Table In our student table if we want to delete a single record, we can use the WHERE clause to provide a condition in our DELETE statement. Eg : DELETE FROM student WHERE s_id=103;The above command will delete the record where s_id is 103 from the table student. 16
  • 17.  Transaction Control Language(TCL) commands are used to manage transactions in the database. These are used to manage the changes made to the data in a table by DML statements. It also allows statements to be grouped together into logical transactions. COMMIT command  COMMIT command is used to permanently save any transaction into the database.  When we use any DML command like INSERT, UPDATE or DELETE, the changes made by these commands are not permanent, until the current session is closed, the changes made by these commands can be rolled back.  To avoid that, we use the COMMIT command to mark the changes as permanent.  Following is commit command's syntax,  COMMIT; ROLLBACK command  This command restores the database to last commited state. It is also used with SAVEPOINT command to jump to a savepoint in an ongoing transaction.  If we have used the UPDATE command to make some changes into the database, and realise that those changes were not required, then we can use the ROLLBACK command to rollback those changes, if they were not commited using the COMMIT command.  Following is rollback command's syntax,  ROLLBACK TO savepoint_name; 17
  • 18. SAVEPOINT command  SAVEPOINT command is used to temporarily save a transaction so that you can rollback to that point whenever required.  Following is savepoint command's syntax,  SAVEPOINT savepoint_name; In short, using this command we can name the different states of our data in any table and then rollback to that state using the ROLLBACK command whenever required. 18