0% found this document useful (0 votes)
22 views3 pages

Working With Data: Creating Tables Using CREATE Command

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)
22 views3 pages

Working With Data: Creating Tables Using CREATE Command

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/ 3

Working with Data

In this module, we'll delve into how to work with data in SQL, using our 'Bookstore Inventory'
database as a practical example. We'll cover creating tables, inserting, updating, deleting data,
and altering table structures.

Creating Tables using CREATE Command

Purpose: The CREATE TABLE command is used to create a new table in the database.

Example:

CREATE TABLE Publishers (


PublisherID INT PRIMARY KEY,
Name VARCHAR(100),
Address VARCHAR(200)
);

This creates a new table named 'Publishers' with three columns: PublisherID, Name, and
Address.

Inserting Data into Tables using INSERT

Purpose: The INSERT INTO command is used to add new rows (records) to a table.

Example:

INSERT INTO Publishers (PublisherID, Name, Address) VALUES


(1, 'Penguin Books', 'New York, USA'),
(2, 'HarperCollins', 'London, UK');

Updating Existing Data using UPDATE

Purpose: The UPDATE command is used to modify existing records in a table.

Example:

UPDATE Books SET Price = Price + 2 WHERE Genre = 'Fiction';

Deleting Data using DELETE

The DELETE command is used to remove records from a table.

Example:

DELETE FROM Books WHERE Title = '1984';


Modifying Table Structure using ALTER TABLE

Purpose: The ALTER TABLE command is used to modify the structure of an existing table,
such as adding, deleting, or modifying columns.

Adding a column:

ALTER TABLE Publishers ADD COLUMN Website VARCHAR(100);

Modify a column:

ALTER TABLE Publishers ALTER COLUMN Address VARCHAR(250);

Delete a Column:

ALTER TABLE Publishers DROP COLUMN Website;

Practice Questions

Note: you don’t need to submit these practice questions, it’s just for practicing purposes, if you
face any doubts, feel free to ask in the discord server.

Question 1: Create a New Table


Create a table named Customers with the following columns:

● CustomerID (integer, primary key)


● FirstName (varchar, 50 characters)
● LastName (varchar, 50 characters)
● Email (varchar, 100 characters)

Question 2: Insert Data


Insert the following records into the Customers table:

​ CustomerID: 101, FirstName: 'Amit', LastName: 'Kumar', Email:


'[email protected]'
​ CustomerID: 102, FirstName: 'Priya', LastName: 'Sharma', Email:
'[email protected]'

Question 3: Update Existing Data


In the Books table, increase the price of all books in the 'Sci-Fi' genre by 3.

Question 4: Delete Data


Delete the book with the title 'Pride and Prejudice' from the Books table.

Question 5: Modify Table Structure


Add a new column to the Customers table named PhoneNumber (varchar, 15 characters).

Question 6: Advanced Insertion


Insert a new book into the Books table. Choose a title, author, genre, price, quantity, and
publication year of your choice.

Question 7: Conditional Update


Update the Customers table to set the email to '[email protected]' for all customers
whose LastName is 'Sharma'.

Question 8: Complex Deletion


Delete all records from the Books table where the Quantity is less than 5.

Question 9: Alter Table Challenge


Alter the Books table to change the Genre column's character limit to 100 characters.

Question 10: Multiple Conditions Update


In the Books table, reduce the price by 2 for all books published before the year 2000
and having a quantity of more than 10.

You might also like