Question

In: Computer Science

1. Write CREAT TABLE statements for the following tables (primary keys are underlined, foreign keys are...

1. Write CREAT TABLE statements for the following tables (primary keys are underlined, foreign keys are in italic and bold). Make sure you have all needed constraints and appropriate datatypes for attributes:

Student (stID, stName, dateOfBirth, advID, majorName, GPA)

Advisor (advID, advName, specialty)

2.  Insert several records in each table.

Solutions

Expert Solution

Explanation:

The CREATE TABLE statement is used to create a new table.

Basic syntax of CREATE TABLE statement is as follows:

CREATE TABLE table_name
(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype
);

The unique name or identifier for the table follows the CREATE TABLE statement. Then in brackets comes the list defining each column in the table and what sort of data type it is.

Syntax for creating primary key is as follows:

CREATE TABLE table_name
(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY(one or more columns)
);

A primary key is a field in a table which uniquely identifies each row/record in a database table. Primary keys must contain unique values. A primary key column cannot have NULL values.

If a table has a primary key defined on any field(s), then you can not have two records having the same value of that field(s).

Syntax for creating foreign key is as follows:

CREATE TABLE table_name
(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY(one or more columns),
FOREIGN KEY (column_name) REFERENCES tbl_name(column_name)
);

A foreign key is a key used to link two tables together. This is sometimes also called as a referencing key.

Now let us create Advisor table as :

CREATE TABLE Advisor(advID INT,advName VARCHAR(20) NOT NULL,specialty VARCHAR(20) NOT NULL,PRIMARY KEY(advID));

From the above create statement we can see that Advisor table is created with 3 columns:

Column name

Datatype

Constraint

advID

INT

PRIMARY KEY

advName

VARCHAR(20)

NOT NULL

specialty

VARCHAR(20)

NOT NULL

Now let us create Student table as :

CREATE TABLE Student(stID INT,stName VARCHAR(20) NOT NULL,dateOfBirth DATE NOT NULL,advID INT,majorName VARCHAR(20) NOT NULL,GPA INT NOT NULL,PRIMARY KEY(stID),FOREIGN KEY(advID) REFERENCES Advisor(advID));

From the above create statement we can see that Student table is created with 6 columns:

Column name

Datatype

Constraint

stID

INT

PRIMARY KEY

stName

VARCHAR(20)

NOT NULL

dateOfBirth

DATE

NOT NULL

advID

INT

FOREIGN KEY REFERENCES Student(advID)

majorName

VARCHAR(20)

NOT NULL

GPA

INT

NOT NULL

Now in order to insert values in the table.

The INSERT INTO statement is used to insert new records in a table.

It is possible to write the INSERT INTO statement in two ways.

The first way specifies both the column names and the values to be inserted:

INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);

If you are adding values for all the columns of the table, you do not need to specify the column names. The order of the values should be in the same order as the columns in the table.

The INSERT INTO syntax for second way would be as follows:

INSERT INTO table_name VALUES (value1, value2, value3, ...);

Following are the sample data :

INSERT INTO Advisor(advID,advName,specialty) VALUES(1,'Jones','ENG');  --- first way

INSERT INTO Advisor VALUES(2,'Thomas','MATH');  --- second way

INSERT INTO Student(stID,stName,dateOfBirth,advID,majorName,GPA) VALUES(1,'White','20-05-1962',1,'ENG',5);  --- first way

INSERT INTO Student VALUES(1,'Bell','10-08-1952',2,'MATH',7);  --- second way

we have inserted advisor details into Advisor Table and then student details in Student table which has the advisor ID from the Advisor table.


Related Solutions

1. Write CREAT TABLE statements for the following tables (primary keys are underlined, foreign keys are...
1. Write CREAT TABLE statements for the following tables (primary keys are underlined, foreign keys are in italic and bold). Make sure you have all needed constraints and appropriate datatypes for attributes: Student (stID, stName, dateOfBirth, advID, majorName, GPA) Advisor (advID, advName, specialty) 2. Insert several records in each table.
1. Write CREATE TABLE statements for the following tables (foreign keys are in italic and bold)....
1. Write CREATE TABLE statements for the following tables (foreign keys are in italic and bold). Make sure you have all needed constraints and appropriate datatypes for attributes: Student (stID, stName, dateOfBirth, advID, majorName, GPA) Advisor (advID, advName, specialty) 2.  Insert several records in each table.
Consider the following relational schema about a University (the primary keys are underlined and foreign keys...
Consider the following relational schema about a University (the primary keys are underlined and foreign keys are italic) STUDENT(StudentID, name, major, year, age) CLASS(ClassName, meetsAt, room, LecturerID) ENROLLED(StudentID, ClassName, mark) LECTURER(LecturerID, name, DepartmentID) DEPARTMENT(DepartmentID, name) Write the SQL statements for the following query: B1. Find the age of the oldest student. B2. Find the ID’s of lecturers whose name begins with “K” \ B3. Find the age of the youngest student who is enrolled in Mechatronics. B4. Find the age...
Consider the following relational schema (the primary keys are underlined and foreign keys are italic) ITEM(ItemName,...
Consider the following relational schema (the primary keys are underlined and foreign keys are italic) ITEM(ItemName, ItemType, ItemColour) DEPARTMENT(Deptname, DeptFloor, DeptPhone, Manager) EMPLOYEE(EmpNo, EmpFname, EmpSalary, DeptName, SupervisedBy) SUPPLIER(SupNo, SupName) SALE(SaleNo, SaleQty, ItemName, DeptName) DELIVERY(DeliNo, DeliQty, ItemName, DeptName, SupNo) Write the SQL statements for the following queries: C1. Find the names of items sold on first and second floors. [1 mark] C2. For each department, list the department name and average salary of the employees where the average salary of the...
Create the following tables. The underlined bold column names are the primary keys. Make sure to...
Create the following tables. The underlined bold column names are the primary keys. Make sure to specify the primary and foreign key constraints in your create table statements. customer: (cus_code:integer, cus_lname:varchar(20), cus_fname:varchar(20), cus_initial:char, cus_areacode:integer,cus_phone:integer). invoice: (inv_number:integer, cus_code:integer, inv_date:date,                   foreign key cus_code references customer(cus_code)) vendor:(vend_code:integer,vend_name:varchar(30),vend_contact:varchar(30),vend_areacode:integer,vend_phone:integer) product:(prod_code:integer, prod_desc:varchar(50), prod_price:integer, prod_quant:integer,vend_code:integer, foreign key (vend_code) referenecs Vendor(vend_code)) line: (inv_number:integer, prod_code:integer ,line_units:integer, foreign key (inv_number) references Invoice(inv_number), foreign key (prod_code) references Product (prod_code) )
Consider the following three tables, primary and foreign keys. Table Name        SalesPeople Attribute Name                  &nbsp
Consider the following three tables, primary and foreign keys. Table Name        SalesPeople Attribute Name                                Type                                      Key Type EmployeeNumber             Number                               Primary Key Name                                   Character JobTitle                                  Character            Address                                 Character PhoneNumber                     Character YearsInPosition                             Number Table Name        ProductDescription Attribute Name                                Type                                      Key Type                 ProductNumber                Number                               Primary Key                 ProductName                  Character                            ProductPrice                   Number Table Name        SalesOrder Attribute Name                                Type                                      Key Type                 SalesOrderNumber        Number                               Primary Key                 ProductNumber               Number                               Foreign Key                 EmployeeNumber           Number                               Foreign Key                 SalesOrderDate                Date Assume that you...
Consider the following three tables, primary and foreign keys. Table Name        SalesPeople Attribute Name                  &nbsp
Consider the following three tables, primary and foreign keys. Table Name        SalesPeople Attribute Name                                Type                                      Key Type EmployeeNumber             Number                               Primary Key Name                                   Character JobTitle                                  Character            Address                                 Character PhoneNumber                     Character YearsInPosition                             Number Table Name        ProductDescription Attribute Name                                Type                                      Key Type                 ProductNumber                Number                               Primary Key                 ProductName                  Character                            ProductPrice                   Number Table Name        SalesOrder Attribute Name                                Type                                      Key Type                 SalesOrderNumber        Number                               Primary Key                 ProductNumber               Number                               Foreign Key                 EmployeeNumber           Number                               Foreign Key                 SalesOrderDate                Date Assume that you...
Please create the following tables for a tool rental database with appropriate primary keys & foreign...
Please create the following tables for a tool rental database with appropriate primary keys & foreign keys. Assumptions: 1. Each tool belongs to a category. 2. Each category may have a parent category but the parent category should not have a parent category (so at most two levels). E.g., a Tool A belongs to the electric mower, and electric mowers belong to mowers. Mower has no parent category. 3. Each tool can be rented at different time units. The typical...
1. Consider the following relations. The primary keys are underlined. All attributes are of type string...
1. Consider the following relations. The primary keys are underlined. All attributes are of type string if not indicated otherwise.  Student(s_ID, s_name, s_degree: integer, advisorID, d_ID)  Lecture(l_ID, l_name,l_degree: integer, p_ID, d_ID)  Register(s_ID,l_ID,score: integer, Semester)  Professor(p_ID,p_name, d_ID)  Department(d_ID, d_name, address) a. Find the names of professors who have taught in every semester. b. List the names of lectures that the CISE department offers but that are taught by a professor whose department is not CISE. c....
Problem 1. Please create the following tables for a tool rental database with appropriate primary keys...
Problem 1. Please create the following tables for a tool rental database with appropriate primary keys & foreign keys. [30 points] Assumptions: Each tool belongs to a category. Each category may have a parent category but the parent category should not have parent category (so at most two levels). E.g., a Tool A belongs to electric mower, and electric mower belongs to mower. Mower has no parent category. Each tool can be rented at different time units. The typical time...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT