0% found this document useful (0 votes)
6 views14 pages

4-Data Manipulation and Querying

The document outlines the fundamental operations for data manipulation and querying in databases, including creating, inserting, updating, and deleting data. It explains how to define tables, primary keys, foreign keys, and composite keys, along with examples of SQL commands for each operation. Additionally, it covers the specifics of inserting data, updating existing records, and deleting rows from a table.

Uploaded by

mthuramge99
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)
6 views14 pages

4-Data Manipulation and Querying

The document outlines the fundamental operations for data manipulation and querying in databases, including creating, inserting, updating, and deleting data. It explains how to define tables, primary keys, foreign keys, and composite keys, along with examples of SQL commands for each operation. Additionally, it covers the specifics of inserting data, updating existing records, and deleting rows from a table.

Uploaded by

mthuramge99
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/ 14

DATA MANIPULATION

AND QUERYING
DATA MANIPULATION AND
QUERYING

 CREATE
 INSERT
 UPDATE
 DELETE
TO CREATE A TABLE

 CREATE TABLE name_of_table (name_of_attribute1


type_of_data, name_of_attribute2 type_of_data, ……)

 CREATE TABLE product (Product_no integer, Name text,


Price numeric);

 To delete the table: DROP TABLE product


Product_no Name Price
TO CREATE A TABLE

 The unique identifier in a table is a primary key


 This serves to join tables
 CREATE TABLE product (
 product_no integer PRIMARY KEY,
 name text,
 price numeric DEFAULT 9.99 CHECK (price>0);
TO CREATE A TABLE
 When a field is used as a common field in a table that
references the primary key from a field in another
table, that field is referred to as a foreign key
 CREATE TABLE order (
 order_no integer PRIMARY KEY,
 quantity integer,
 FOREIGN KEY (product_no) REFERENCES product
(product_no));
TO CREATE A TABLE

 A shorter way
 CREATE TABLE order (
 order_no integer PRIMARY KEY,
 quantity integer,
 product_no integer REFERENCES product (product_no));

 If the column names are the same in both the table which contains the
primary key and the one that references that key as a foreign key, you can
use shorthand to specify this fact

 product_no integer REFERENCES product


COMPOSITE KEY

 A primary key constructed as a combination of fields


 CREATE TABLE order_item (
 product_no integer REFERENCES product
 order_no integer REFERENCES order
 quantity integer
 PRIMARY KEY (product_no, order_no));
INSERTING DATA

 Inserted one row at a time


 INSERT INTO product VALUES (1, ‘cheese’, 9.99);
 In order in which columns appear in the table,
separated by commas
 INSERT INTO product (product_no, name, price) VALUES
(1, ‘cheese’, 9.99
 Columns can be re-arranged
 INSERT INTO product (name, price, product_no) VALUES
)’cheese’, 9.99, 1);
INSERTING DATA

 INSERT INTO product VALUES (1,’Cheese’,9.99);

 INSERT INTO product (name,product_no,price) VALUES


(‘cheese’,1,9.99);

 INSERT INTO product (name,product_no,price) VALUES


(‘cheese’,1,9.99),(‘BREAD’,2,1.99);
INSERTING DATA

 If you don’t have values for columns you can omit. The default
value will be filled in or null if no user-defined defaults are
specified
 INSERT INTO product (product_no, name) VALUES (1, ‘cheese’);
 You can insert multiple rows into a table

 INSERT Into product (product_no, name, price) VALUES


 (1, ‘cheese’, 9.99),
 (2, ‘Bread’, 1.99),
 (3, ‘Milk’, 2.99);
INSERTING DATA

 Insert default values for a whole row


 INSERT INTO product DEFAULT VALUES

 You can insert user-defined values into certain fields


and their default values for others
 INSERT INTO product (product_no, name, price) VALUES
(1, ‘cheese’, DEFAULT);
UPDATING DATA
 Modification of data already in a database
 You can update individual rows, all rows in a table, or a subset of all rows
 UPDATE tablename;

 Specify conditions
 UPDATE product SET price = 10 WHERE price = 5;

 For all rows to be updated


 UPDATE products SET price = price * 1.1;

 Update more than one column


 UPDATE products SET price = 10, quantity = 0, description = ‘N/A’ WHERE
quantity <= -1;
DELETING DATA

 Deleting data is only possible in whole rows


 DELETE FROM products where price = 10;

 Deleting all rows in a table


 DELETE FROM products;
CONCLUSION

 CREATE
 INSERT
 UPDATE
 DELETE

You might also like