Ex.No.
6 TABLE CREATION WITH VIEWS
Statement of problem
create a table ‘ library’ with proper fields and create another table ‘ library1’ and insert
rows from library using views
Aim
To create a table ‘ library’ with proper fields and create another table ‘ library1’ and insert
rows from library using views
PROCEDURE:
Create Database
CREATE DATABASE lib_db;
show the database
SHOW DATABASES;
Using the database
USE lib_db;
Create the 'library' table
CREATE TABLE library
(
book_id INT PRIMARY KEY,
book_title VARCHAR(255),
author VARCHAR(255),
genre VARCHAR(255),
publication_year INT,
available BOOLEAN
);
Desc library;
Insert some records into the 'library' table
INSERT INTO library (book_id, book_title, author, genre, publication_year, available)
VALUES
(1, 'The Lord of the Rings', 'J.R.R. Tolkien', 'Fantasy', 1954, TRUE),
(2, 'Pride and Prejudice', 'Jane Austen', 'Romance', 1813, FALSE),
(3, 'To Kill a Mockingbird', 'Harper Lee', 'Historical Fiction', 1960, TRUE),
(4, '1984', 'George Orwell', 'Dystopian', 1949, FALSE),
(5, 'The Hitchhiker's Guide to the Galaxy', 'Douglas Adams', 'Science Fiction', 1979,
TRUE);
Selecting the table
select * from library;
Create the 'library1' table with the same structure as 'library'
CREATE TABLE library1
(
book_id INT PRIMARY KEY,
book_title VARCHAR(255),
author VARCHAR(255),
genre VARCHAR(255),
publication_year INT,
available BOOLEAN
);
Create a view to select all data from the 'library' table
CREATE VIEW library_view AS SELECT * FROM library;
Insert data from the 'library_view' into the 'library1' table
INSERT INTO library1 SELECT * FROM library_view;
Verify the data in 'library1'
SELECT * FROM library1;
Result
The mysql query to create a table ‘ library’ with proper fields and create another table ‘
library1’ and insert rows from library using views has been done successfully.
OUTPUT:
Library Table structure:
Displaying Library Records:
Library1 Table Structure:
Displaying Library1 Records: