Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1 6 . DO the following: Build the Persons 1 6 table with the same data fields and data records where the P _
DO the following: Build the Persons table with the same data fields and data
records where the PId column in the "Persons table is the PRIMARY KEY in the
"Persons table.
The "Persons table:
PId LastName FirstName Address City
Hansen Harry Smith St Edison
Svendson Tammy Apple Way Colonia
Pettersen Kimmie Patio Place Iselin
To understand SQL FOREIGN KEY Constraint on CREATE TABLE, DO the following
SQL creates a FOREIGN KEY on the PId column when the "Orders table is created:
CREATE TABLE Orders
OId int NOT NULL PRIMARY KEY,
OrderNo int NOT NULL,
PId int,
FOREIGN KEY PId REFERENCES PersonsPId
To understand how to DROP a PRIMARY KEY constraint, DO the following SQL:
ALTER TABLE Persons
DROP CONSTRAINT pkPersonID
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY
constraint on multiple columns, DO the following SQL syntax:
CREATE TABLE Orders
OId int NOT NULL,
OrderNo int NOT NULL,
PId int,
PRIMARY KEY OId
CONSTRAINT fkPerOrders FOREIGN KEY PId
REFERENCES PersonsPId
To understand how to DROP a FOREIGN KEY constraint, DO the following SQL:
ALTER TABLE Orders
DROP CONSTRAINT fkPerOrders
DO the following SQL to create a CHECK constraint on the PId column when the
"Persons" table is created. The CHECK constraint specifies that the column PId must
only include integers greater than
CREATE TABLE Persons
PId int NOT NULL CHECK PId
LastName varchar NOT NULL,
FirstName varchar
Address varchar
City varchar
DO the following to allow naming of a CHECK constraint, and for defining a CHECK
constraint on multiple columns, use the following SQL syntax:
CREATE TABLE Persons
PId int NOT NULL,
LastName varchar NOT NULL,
FirstName varchar
Address varchar
City varchar
CONSTRAINT chkPerson CHECK PId AND City'Sandnes'
To understand the SQL CHECK Constraint on ALTER TABLE, DO the following
to create a CHECK constraint on the PId column when the table is already created,
use the following SQL:
ALTER TABLE Persons
ADD CHECK PId
To understand how to DROP a CHECK constraint, DO the following SQL:
ALTER TABLE Persons
DROP CONSTRAINT chkPerson
To use the SQL DEFAULT Constraint on CREATE TABLE, DO the following SQL
creates a DEFAULT constraint on the "City" column when the "Persons" table is created:
CREATE TABLE Persons
PId int NOT NULL,
LastName varchar NOT NULL,
FirstName varchar
Address varchar
City varchar DEFAULT 'Sandnes'
To understand how to use the DEFAULT constraint can also be used to insert system
values, by using functions like SYSDATE, DO the following:
CREATE TABLE Orders
OId int NOT NULL,
OrderNo int NOT NULL,
PId int,
OrderDate date DEFAULT SYSDATE
To understand how to use the SQL DEFAULT constraint on ALTER TABLE, DO the following to create a DEFAULT constraint on the "City" column when the table is already created, use the following SQL:
ALTER TABLE Persons
MODIFY City DEFAULT 'SANDNES'
To demonstrate the SQL ALTER TABLE, DO the following:
Build a "Persons table with the following columns and insert the data into the table:
PId LastName FirstName Address City
Hansen Harry Smith St Edison
Svendson Tammy Apple Way Colonia
Pettersen Kimmie Patio Place Iselin
DO: Now add a column named "DateOfBirth" in the "Persons table by doing the following SQL statement:
ALTER TABLE Persons
ADD DateOfBirth date
Notice that the new column, "DateOfBirth", is of type date and is going to hold a date. The data type specifies what type of data the column can hold.
DO: Use a Select command to demonstrate that the "Persons table will now look like this:
PId LastName FirstName Address City DateOfBirth
Hansen Harry Smith St Edison
Svendson Tammy Apple Way Colonia
Pettersen Kimmie Patio Place Iselin
The "DateOfBirth" column is now of type year and is going to hold a year in a
twodigit or fourdigit format. DO: Use a DESC statement on the Persons table to see this change.
To delete the column named "DateOfBirth" in the "Persons table, DO the following
SQL statement:
ALTER TABLE Persons
DROP COLUMN DateOfBirth
DO: Use a Select command to demonstrate that the "Persons table will now look like this:
PId LastName FirstName Address City
Hansen Harry Smith St Edison
Svendson Tammy Apple Way Colonia
Pettersen Kimmie Patio Place Iselin
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started