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
| MatchID | MatchDate | FirstTeamID | SecondTeamID | FirstTeamScore | SecondTeamScore |
|---|---|---|---|---|---|
| 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 |
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 namedSports.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:
INTis suitable for integer values. - Constraint: The requirement for values between 1 and 9 can be enforced using a
CHECKconstraint. - Primary Key: The requirement for unique identification makes
TeamIDa perfect candidate for aPRIMARY 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.
- Data Type:
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
CHECKconstraint can be used to ensure the length of theTeamNameis not less than 10 characters.
- Data Type:
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 namedTEAM.TeamID INT: Defines a columnTeamIDto store integer values.TeamName VARCHAR(50): Defines a columnTeamNameto store strings, with a maximum length of 50 characters.PRIMARY KEY (TeamID): This is a table-level constraint that designatesTeamIDas the primary key for theTEAMtable. It ensures that eachTeamIDis unique and not null.CHECK (TeamID >= 1 AND TeamID <= 9): This constraint ensures that any value inserted intoTeamIDmust be between 1 and 9 (inclusive).CHECK (LENGTH(TeamName) >= 10): This constraint ensures that theTeamNamemust 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):
| Field | Type | Null | Key | Default | Extra |
|---|---|---|---|---|---|
| TeamID | int | NO | PRI | NULL | |
| TeamName | varchar(50) | YES | NULL |
Explanation:
DESCRIBE TEAM;: This statement displays the column names, their data types, whether they can containNULLvalues, if they are part of a key (likePRIfor primary key), and other information about theTEAMtable'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 theTEAMtable. We explicitly list the columnsTeamIDandTeamNameto 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:
| TeamID | TeamName |
|---|---|
| 1 | Team Titan |
| 2 | Team Rockers |
| 3 | Team Magnet |
| 4 | Team Hurricane |
Explanation:
SELECT * FROM TEAM;: This statement selects all columns (*) from theTEAMtable 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 KEYas it uniquely identifies each match.
- Data Type:
MatchDate: Date of the match.- Data Type:
DATE.
- Data Type:
FirstTeamID,SecondTeamID: These refer toTeamIDfrom theTEAMtable.- Data Type:
INT, matching theTeamIDin theTEAMtable. - Constraint:
FOREIGN KEYreferencingTeamIDin theTEAMtable. This ensures referential integrity, meaning you cannot record a match with aTeamIDthat doesn't exist in theTEAMtable.
- Data Type:
FirstTeamScore,SecondTeamScore: Scores of the teams.- Data Type:
INT. Scores are typically non-negative, so aCHECKconstraint could be added (e.g.,CHECK (FirstTeamScore >= 0)), but it's not explicitly requested, so we'll keep it simple.
- Data Type:
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.