Skip to content
Exercises · Q4
Q.

Suppose your school management has decided to conduct cricket matches between students of Class XI and Class XII. Students of each class are asked to join any one of the four teams - Team Titan, Team Rockers, Team Magnet and Team Hurricane. During summer vacations, various matches will be conducted between these teams. Help your sports teacher to do the following:

a) Create a database "Sports".

b) Create a table "TEAM" with following considerations:

i) It should have a column TeamID for storing an integer value between 1 to 9, which refers to unique identification of a team.

ii) Each TeamID should have its associated name (TeamName), which should be a string of length not less than 10 characters.

c) Using table level constraint, make TeamID as the primary key.

d) Show the structure of the table TEAM using a SQL statement.

e) As per the preferences of the students four teams were formed as given below. Insert these four rows in TEAM table:

Row 1: (1, Team Titan)

Row 2: (2, Team Rockers)

Row 3: (3, Team Magnet)

Row 3: (4, Team Hurricane)

f) Show the contents of the table TEAM using a DML statement.

g) Now create another table MATCH_DETAILS and insert data as shown below. Choose appropriate data types and constraints for each attribute.

Table: MATCH_DETAILS

MatchIDMatchDateFirstTeamIDSecondTeamIDFirstTeamScoreSecondTeamScore
M12018-07-17129086
M22018-07-18344548
M32018-07-19137856
M42018-07-19245667
M52018-07-18143287
M62018-07-17236751
Punjab PsebTextbookSubjective· 4mImportance★★★★★
46% · 44/95 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

This task involves creating a database and two tables, TEAM and MATCH_DETAILS, in SQL. It covers defining columns with appropriate data types, applying primary key, check, and foreign key constraints, and then inserting and displaying data.

(a) Create a database "Sports"

Before we can create any tables, we need a place to store them. A database acts as a container for all our related tables, views, procedures, and other database objects. The CREATE DATABASE statement is used for this purpose.

CREATE DATABASE Sports;
USE Sports;

Explanation:

  • CREATE DATABASE Sports;: This statement creates a new database named Sports.
  • USE Sports;: After creating the database, we need to select it as the current database so that all subsequent commands operate within it.

(b) Create a table "TEAM" with following considerations:

(c) Using table level constraint, make TeamID as the primary key.

Creating a table involves defining its name and the columns it will contain, along with their data types and any constraints.

  • TeamID: This column needs to store an integer value between 1 and 9 and uniquely identify each team.
    • Data Type: INT is suitable for integer values.
    • Constraint: The requirement for values between 1 and 9 can be enforced using a CHECK constraint.
    • Primary Key: The requirement for unique identification makes TeamID a perfect candidate for a PRIMARY KEY. A primary key ensures that all values in the column are unique and not null, providing a unique identifier for each row. Applying it at the table level means defining it after all columns have been declared.
  • TeamName: This column stores the name of the team and must be a string of at least 10 characters.
    • Data Type: VARCHAR(50) (or a similar length) is appropriate for storing variable-length strings. We choose a maximum length that is reasonable for team names.
    • Constraint: A CHECK constraint can be used to ensure the length of the TeamName is not less than 10 characters.
CREATE TABLE TEAM (
    TeamID INT,
    TeamName VARCHAR(50),
    PRIMARY KEY (TeamID),
    CHECK (TeamID >= 1 AND TeamID <= 9),
    CHECK (LENGTH(TeamName) >= 10)
);

Explanation:

  • CREATE TABLE TEAM (...): This initiates the creation of a table named TEAM.
  • TeamID INT: Defines a column TeamID to store integer values.
  • TeamName VARCHAR(50): Defines a column TeamName to store strings, with a maximum length of 50 characters.
  • PRIMARY KEY (TeamID): This is a table-level constraint that designates TeamID as the primary key for the TEAM table. It ensures that each TeamID is unique and not null.
  • CHECK (TeamID >= 1 AND TeamID <= 9): This constraint ensures that any value inserted into TeamID must be between 1 and 9 (inclusive).
  • CHECK (LENGTH(TeamName) >= 10): This constraint ensures that the TeamName must have a length of at least 10 characters.

(d) Show the structure of the table TEAM using a SQL statement.

To view the structure (schema) of a table, including its columns, data types, and constraints, we can use the DESCRIBE (or DESC) statement or SHOW COLUMNS FROM statement.

DESCRIBE TEAM;

Expected Output (may vary slightly depending on the specific SQL RDBMS, e.g., MySQL, PostgreSQL):

FieldTypeNullKeyDefaultExtra
TeamIDintNOPRINULL
TeamNamevarchar(50)YESNULL

Explanation:

  • DESCRIBE TEAM;: This statement displays the column names, their data types, whether they can contain NULL values, if they are part of a key (like PRI for primary key), and other information about the TEAM table's structure.

(e) As per the preferences of the students four teams were formed as given below. Insert these four rows in TEAM table:

To add new rows of data into an existing table, we use the INSERT INTO statement. We specify the table name, optionally the columns we are inserting into, and then the values for each column.

INSERT INTO TEAM (TeamID, TeamName) VALUES
(1, 'Team Titan'),
(2, 'Team Rockers'),
(3, 'Team Magnet'),
(4, 'Team Hurricane');

Explanation:

  • INSERT INTO TEAM (TeamID, TeamName) VALUES (...): This statement inserts data into the TEAM table. We explicitly list the columns TeamID and TeamName to ensure values are mapped correctly.
  • (1, 'Team Titan'), etc.: Each set of parentheses represents a row to be inserted, with values corresponding to the listed columns.

(f) Show the contents of the table TEAM using a DML statement.

To retrieve and display all the data stored in a table, we use the SELECT statement, which is a Data Manipulation Language (DML) command.

SELECT * FROM TEAM;

Expected Output:

TeamIDTeamName
1Team Titan
2Team Rockers
3Team Magnet
4Team Hurricane

Explanation:

  • SELECT * FROM TEAM;: This statement selects all columns (*) from the TEAM table and displays all the rows currently stored in it.

(g) Now create another table MATCH_DETAILS and insert data as shown below. Choose appropriate data types and constraints for each attribute.

This involves creating a new table MATCH_DETAILS and then populating it with the provided data. We need to carefully select data types and constraints for each column.

  • MatchID: Unique identifier for each match.
    • Data Type: VARCHAR(5) (e.g., 'M1', 'M2').
    • Constraint: PRIMARY KEY as it uniquely identifies each match.
  • MatchDate: Date of the match.
    • Data Type: DATE.
  • FirstTeamID, SecondTeamID: These refer to TeamID from the TEAM table.
    • Data Type: INT, matching the TeamID in the TEAM table.
    • Constraint: FOREIGN KEY referencing TeamID in the TEAM table. This ensures referential integrity, meaning you cannot record a match with a TeamID that doesn't exist in the TEAM table.
  • FirstTeamScore, SecondTeamScore: Scores of the teams.
    • Data Type: INT. Scores are typically non-negative, so a CHECK constraint could be added (e.g., CHECK (FirstTeamScore >= 0)), but it's not explicitly requested, so we'll keep it simple.
CREATE TABLE MATCH_DETAILS (
    MatchID VARCHAR(5) PRIMARY KEY,
    MatchDate DATE,
    FirstTeamID INT,
    SecondTeamID INT,
    FirstTeamScore INT,
    SecondTeamScore INT,
    FOREIGN KEY (FirstTeamID) REFERENCES TEAM(TeamID),
    FOREIGN KEY (SecondTeamID) REFERENCES TEAM(TeamID)
);

INSERT INTO MATCH_DETAILS (MatchID, MatchDate, FirstTeamID, SecondTeamID, FirstTeamScore, SecondTeamScore) VALUES
('M1', '2018-07-17', 1, 2, 90, 86),
('M2', '2018-07-18', 3, 4, 45, 48),
('M3', '2018-07-19', 1, 3, 78, 56),
('M4', '2018-07-19', 2, 4, 56, 67),
('M5', '2018-07-18', 1, 4, 32, 87),
('M6', '2018-07-17', 2, 3, 67, 51);

SELECT * FROM MATCH_DETAILS;

Explanation: …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.