Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
16 views11 pages

SQL Crud

op

Uploaded by

balajikoushik07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views11 pages

SQL Crud

op

Uploaded by

balajikoushik07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

SQL CRUD

Create, Read, Update, Delete

1 / 11
CRUD Operations in SQL

I Create (INSERT)
I Read (SELECT - covered later)
I Update (UPDATE)
I Delete (DELETE, DROP)

2 / 11
INSERT Command

General form is
INSERT INTO <table_name> (<column_name> [, ...]) VALUES
(<new_value> [, ...]);
Example:
insert into author (author_id, first_name, second_name)
values (1, "Jenny","McCarthy");
Can leave off column names list to insert positionally:
insert into author values (1, "Jenny","McCarthy");

3 / 11
UPDATE Command

General form:
UPDATE <table_name> SET <column_name>=<new_value> [, ...]
WHERE expression
Example: Surely we meant Lisp inventor, AI co-founder, and Turing
Award winner John McCarthy instead of anti-vaxxer Jenny McCarthy.
update author set first_name=’John’ where
last_name=’McCarthy’;
Notice that we can use single or double quotes in most RDBMS systems.

4 / 11
DELETE Command

General form:
DELETE FROM <table_name> WHERE <boolean_expression>;
Example:
delete from author where last_name="Batty";
Can also drop whole tables:
DROP TABLE <table_name>

5 / 11
Referential Integrity

To maintain integrity on update or delete specify:


I For ON DELETE:
I SET NULL
I SET DEFAULT
I For ON UPDATE
I CASCADE
Note: for MySQL ON DELETE RESTRICT is the default.

6 / 11
Referential Integrity - SET NULL

Example:
CREATE TABLE pub (
pub_id INT PRIMARY KEY,
title VARCHAR(16) NOT NULL,
book_id INT,
foreign key (book_id) REFERENCES book(book_id) ON
DELETE SET NULL
);
Means that if the row from the book table containing book_id is deleted,
then book_id is set to NULL for each affected row in the pub table.
Notice that if you choose SET NULL as your ON DELETE action your
column definition must allow nulls.

7 / 11
Referential Integrity Constraints in MySQL

MySQL will only enforce referential integrity contraints that are specfied
separately from column definitions as above. The following syntax:
CREATE TABLE pub (
pub_id INT PRIMARY KEY,
title VARCHAR(16) NOT NULL,
book_id INT REFERENCES book(book_id) ON DELETE SET NULL
);
is valid SQL syntax but is ignored by MySQL’s default InnoDB engine.

8 / 11
Referential Integrity - SET DEFAULT

Example:
CREATE TABLE pub (
pub_id INT PRIMARY KEY,
title VARCHAR(16) NOT NULL,
book_id INT DEFAULT 0 REFERENCES book(book_id) ON
DELETE SET DEFAULT
);
Means that if the row from the book table containing book_id is
deleted, then book_id is set to 0 for each affected row in the pub table.
Note: MySQL’s default InnoDB engine does not implement
ON DELETE SET DEFAULT.

9 / 11
Referential Integrity - CASCADE

Example:
CREATE TABLE pub (
pub_id INT PRIMARY KEY,
title VARCHAR(16) NOT NULL,
book_id INT NOT NULL,
FOREIGN KEY (book_id) REFERENCES book(book_id) ON
UPDATE CASCADE
);
Means that if a book_id value changes for a row in the book table, the
change is applied to the affected rows of the pub table also.

10 / 11
Multiple Referential Integrity Constraints

You would normally set contraints for updates and deletes.


Example:
CREATE TABLE pub (
pub_id INT PRIMARY KEY,
title VARCHAR(16) NOT NULL,
book_id INT,
FOREIGN KEY (book_id) REFERENCES book(book_id)
ON UPDATE CASCADE
ON DELETE SET NULL
);

11 / 11

You might also like