0% found this document useful (0 votes)
222 views7 pages

Table Creation and Management in SQL

Uploaded by

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

Table Creation and Management in SQL

Uploaded by

aryan pagaria
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1) Layna creates a table STOCK to maintain computer stock

in vidyalaya. After creation of the table, she has entered


data of 8 items in the table.

Based on the data given above answer the following questions:


(i) Identify the most appropriate column, which can be
considered as Primary key.
(ii) If three columns are added and 5 rows are deleted from the
table stock, what will be the new degree and cardinality of the
above table?
(iii) Write the statements to:
(a) Insert the following record into the table
Stockid - 201, dateofpurchase – 18-OCT-2022, name –
neckphone
Make – BoAT, price - 500
(b) Decrease the price of stock by 5% whose were purchased in
year 2020
OR (Option for part iii only)
(iii) Write the statements to:
(a) Delete the record of stock which were purchased before
year 2015.
(b) Add a column STATUS in the table with datatype as char
with 1 characters
Answers:
(i) stockid
(ii) degree = 8, cardinality = 3
(iii) (a) insert into stock values(201,’2022-10-
18’,’neckphone’,’boat’,500);
(b) update stock set price=price*0.95 where
year(dopurchase)=2020;
OR
(a) delete from stock where year(dopurchase) < 2015;
(b) alter table stock add column STATUS char(1);

2) Tarun created the following table in MySQL to maintain stock


for the items he has.

a) The Primary Key should be Productid since it uniquely identifies each


row. (1)
b) Degree – 6 Cardinality – 6 (½ + ½)
c) UPDATE inventory SET stock=stock+10 WHERE company = 'Parley';
OR
DELETE FROM inventory WHERE RATING IS NULL; (2)
3)
Answers:
i) Bcode
(ii) degree =9, cardinality =3
(iii) 1 mark for each
(a)INSERT INTO MOTOR VALUES(207, ‘TVS’, 500, 450, 480,
350);
(b) SELECT BNAME FROM MOTOR WHERE JANUARY>200;
OR
(iii) 1 mark for each
(a)ALTER TABLE MOTOR ADD MAY INT;
(b)SELECT SUM(MARCH) FROM MOTOR;
1. FID because FID has unique values.
2. Cardinality : 9 , Degree : 7
3. (i) INSERT INTO FURNITURE

VALUES(‘D001’,’Computer Table’,’01-Nov-2018’,10000,10);
(ii) UPDATE FURNITURE
SET price=price+1000 WHERE discount > 10;
OR (Option for part 3 only)
3. (a) DELETE FROM FURNITURE

WHERE price <20000;


(b) ALTER TABLE FURNITURE
ADD WOOD VARCHAR(20);

(i) Ans: ROLL_NO


(ii) Ans: New Degree: 8 New Cardinality: 5
(iii) a. INSERT INTO RESULT VALUES (108, ‘Aadit’, 470, 444, 475, ‘I’);
b. UPDATE RESULT SET SEM2=SEM2+ (SEM2*0.03) WHERE SNAME LIKE “N%”;
OR (Option for part iii only)
a. DELETE FROM RESULT WHERE DIV=’IV’;
b. ALTER TABLE RESULT ADD (REMARKS VARCHAR(50));
a)Altertable TeacheraddprimarykeyT_ID;
b)Alter tableTeacher add column P_ID int(1);
c)Alter tableTeacher add constraint(c1_01)foreign key(P_ID)
referencesTeacher(P_ID);
or
Altertable Posting modifycolumn Place varchar(10);
i) He creates Projects table first because PID is primary key and project is foreign key referenced
with PID for employee table.
ii) We need no. of rows and columns to compute degree and cardinality of any table which is not
specified over here. Suppose we consider project table has 3 rows and employee table has five
rows then cartesian products will be as follows:
no. of rows = 3 x 5 = 15
no. of columns = 4 + 6 = 10
Hence, Degree of cartesian product is 10.
iii) create table employee
(EID char(4) primary key,
name varchar(20),
DOB date not null,
DOJ date not null,
Salary integer,
Project char(5) references projects(PID));
iv) alter table employee add column gender char(1);

Common questions

Powered by AI

When writing SQL statements to modify a database table, it's essential to apply conditions and structure changes accurately. For updating records, a typical statement uses the 'UPDATE' command with a 'SET' clause for modifications and a 'WHERE' clause for constraints, e.g., 'UPDATE stock SET price=price*0.95 WHERE YEAR(dopurchase)=2020'. For altering table structures, 'ALTER TABLE' is used, e.g., 'ALTER TABLE stock ADD STATUS char(1)', which shows how to add a column as in Source 1.

SQL can perform conditional operations using 'WHERE' clauses, allowing for updates or deletions that meet specified criteria. This ability to target specific records ensures precise data management. For example, Source 1 describes decreasing the price by 5% for stocks purchased in 2020 using 'WHERE YEAR(dopurchase)=2020' as part of the SQL statement, demonstrating how conditions can selectively alter or purge data accurately.

The VARCHAR data type is used for storing variable-length strings, offering flexibility as it only consumes space for the stored content plus a small byte overhead, rather than a fixed size. This is more efficient in terms of storage when dealing with strings of varying lengths. It also allows efficient indexing and retrieval of data. In Source 2, VARCHAR is used for names and project references, capitalizing on its adaptability to varying string sizes while maintaining efficient storage.

Understanding the relationship between primary and foreign keys is crucial in relational database design because it defines the connection between different tables, ensuring data consistency and referential integrity. A primary key uniquely identifies each table row, while a foreign key in another table refers to this primary key, enabling linking of related data across tables. In Source 2, the project table has 'PID' as its primary key, and this 'PID' is referenced as a foreign key in the employee table, illustrating the dependency and consistency enforced by these constraints.

Best practices for inserting records include ensuring that all relevant fields are populated, particularly those involved in primary and foreign key relationships, to maintain relational consistency. Records should also adhere to data validation rules established by the database schema. For instance, as shown in Source 1, inserting a new record into the Stock table includes providing a unique 'stockid' to maintain integrity, while ensuring other key fields align with existing relational constraints, thus ensuring consistency and data validity across the database.

Degree refers to the number of columns in a database table, while cardinality refers to the number of rows. They are calculated based on the table's structure: degree is obtained by counting the columns, and cardinality by counting the rows. For example, if three columns are added to a table with an initial degree of 5 and 5 rows are deleted from 8, the new degree becomes 8, and the new cardinality becomes 3, as shown in Source 1.

A primary key is significant because it uniquely identifies each record in a database table, ensuring that each entry is distinct. This uniqueness is critical for maintaining data integrity and preventing duplicate records in the database. It also allows for accurate referencing in relationships between different tables, such as foreign key dependencies. For example, in Source 1, 'stockid' is identified as the primary key for the Stock table as it uniquely identifies each item's entry, thus maintaining the table’s integrity.

Deleting records based on conditions can help maintain the relevance and accuracy of a database by removing obsolete or incorrect data. However, this needs careful management to avoid unintended loss of important relationships and data integrity issues, particularly if there are dependencies on the deleted data. For instance, in Source 1, deleting records from stock for purchases before 2015 prevents clutter from outdated entries but should ensure that referenced data elsewhere doesn't break as a result of these deletions.

Composite keys offer the advantage of using multiple columns to provide a unique identifier for a table, which is beneficial when a single column isn't sufficient to guarantee uniqueness. They enable complex data relationships and allow more nuanced table structures by combining columns' values, increasing the precision of identification. This prevents data anomalies and improves database normalization and flexibility. Although Source 1 doesn’t specifically mention composite keys, their usage can be inferred when a table demands complex uniqueness conditions.

The Cartesian product in database queries combines all rows from two tables, resulting in pairs of rows. This is useful in scenarios where a comprehensive cross-listing of data is needed for analysis, but it can be inefficient and create large, unwieldy result sets, especially with large tables. Properly understanding its application is crucial to avoid unnecessary overheads. In Source 2, the example of computing the Cartesian product with 3 rows from the project table and 5 from the employee table highlights the exponential increase in data to 15 rows, showcasing its power and potential inefficiencies.

You might also like