Podcast
Questions and Answers
After successfully creating a database, what must you do before creating database tables?
After successfully creating a database, what must you do before creating database tables?
- Optimize server settings.
- Choose which database to use. (correct)
- Delete temporary files.
- Back up the master database.
The 'PRIMARY KEY' constraint ensures the uniqueness of every record in a table and is optional for every table.
The 'PRIMARY KEY' constraint ensures the uniqueness of every record in a table and is optional for every table.
False (B)
What is the purpose of the FOREIGN KEY ... REFERENCES
constraint?
What is the purpose of the FOREIGN KEY ... REFERENCES
constraint?
To enforce referential integrity between tables.
The __________
constraint is used to determine if null values are allowed in a column.
The __________
constraint is used to determine if null values are allowed in a column.
What type of value does the IDENTITY
property provide for a column?
What type of value does the IDENTITY
property provide for a column?
You can add two primary keys in a table at the same time through one declaration.
You can add two primary keys in a table at the same time through one declaration.
In the table FACULTY, what does the attribute ColCode become when linking tables?
In the table FACULTY, what does the attribute ColCode become when linking tables?
The __________
statement is used to provide a default value for a column when a value is not given by the user during the insert operation.
The __________
statement is used to provide a default value for a column when a value is not given by the user during the insert operation.
Match the DDL statements with their descriptions:
Match the DDL statements with their descriptions:
What is the first step to see the equivalent diagram generated by the SQL Server?
What is the first step to see the equivalent diagram generated by the SQL Server?
If you want to delete a database, it must be currently selected.
If you want to delete a database, it must be currently selected.
What constraint is added to FACULTY to reference EmpID in the FACULTY table itself and why?
What constraint is added to FACULTY to reference EmpID in the FACULTY table itself and why?
To delete a table, the syntax is DROP TABLE __________
.
To delete a table, the syntax is DROP TABLE __________
.
When adding a foreign key constraint, what must be ensured about the data type?
When adding a foreign key constraint, what must be ensured about the data type?
The CHECK constraint is used to provide a default value for a column.
The CHECK constraint is used to provide a default value for a column.
Flashcards
CREATE DATABASE
CREATE DATABASE
A DDL statement used to create a new database.
USE DATABASE
USE DATABASE
A DDL statement specifying which database to use.
DROP DATABASE
DROP DATABASE
A DDL statement to completely remove a database.
CREATE TABLE
CREATE TABLE
Signup and view all the flashcards
PRIMARY KEY
PRIMARY KEY
Signup and view all the flashcards
FOREIGN KEY
FOREIGN KEY
Signup and view all the flashcards
NULL | NOT NULL
NULL | NOT NULL
Signup and view all the flashcards
IDENTITY
IDENTITY
Signup and view all the flashcards
DEFAULT
DEFAULT
Signup and view all the flashcards
CHECK
CHECK
Signup and view all the flashcards
DROP TABLE
DROP TABLE
Signup and view all the flashcards
ALTER TABLE (add column)
ALTER TABLE (add column)
Signup and view all the flashcards
ALTER TABLE (drop column)
ALTER TABLE (drop column)
Signup and view all the flashcards
ALTER TABLE (alter column)
ALTER TABLE (alter column)
Signup and view all the flashcards
ALTER TABLE (add primary key)
ALTER TABLE (add primary key)
Signup and view all the flashcards
Study Notes
Creating a Database
- Syntax to create a database uses:
CREATE DATABASE database_name
- Example:
CREATE DATABASE samplelecture
- Following successful database creation, select the database to use before creating tables
- The default selected database is master
- There are two methods for selecting the database:
- Using the USE DATABASE statement. Syntax:
USE database_name
. Example:USE samplelecture
- By clicking on the database list in the interface
- Using the USE DATABASE statement. Syntax:
Removing a Database
- Before deleting a database, ensure that the database to be deleted is not currently selected
- Syntax for removing a database:
DROP DATABASE database_name
- Example:
DROP DATABASE samplelecture
Creating a Database Table
- Select the correct database before executing the structure of your database table
- Syntax:
CREATE TABLE table_name( column_name1 datatype, column_name2 datatype, column_name3 datatype, ... ) [table constraints, if any]
Common Constraints
PRIMARY KEY
ensures the uniqueness of every record in a table and is required in every tableFOREIGN KEY ... REFERENCES
enforces referential integrity, ensuring a value inserted in the foreign key column matches a value stored in the primary key column of the reference tableNULL | NOT NULL
determines if null values are allowed in a column. If a value is required,NOT NULL
should be placed after the data type declarationIDENTITY
is used to provide a unique, incremental value for a column, commonly used with primary key constraints. Example:StudID int primary key identity (1000,1)
DEFAULT
provides a default value for a column when a value isn't given by the user during insertion. Example:HireDate datetime not null default (getdate())
CHECK
can limit or filter the possible values entered into a column. Example:InstructorType varchar(1) check(InstructorType in (ʻR','P'))
- You cannot add two primary keys in a table at the same time in one declaration.
- To set two primary keys in a table, you have to define first the columns and its datatype with NOT NULL values and then add a separate constraint for the two columns that you wanted to be set as the primary keys
Viewing Database Diagram on SQL Server
- On the Object Explorer, expand the Databases folder
- Expand the folder of the database where needed table exists
- Right-click on Database Diagrams and choose New Database Diagram
- In the Add Table dialog box, select the tables to include in the diagram and click Add
Deleting a Table
- Syntax:
DROP TABLE table_name
- Example:
DROP TABLE COLLEGE
- To check if the table has been deleted, regenerate a new database diagram
- Refresh the database first, as there might be a delay in the generation of the new diagram
Linking Tables with Constraints
- Link tables by using constraints. For example, the FACULTY table contains a ColCode attribute, which becomes a foreign key referencing the ColCode in the COLLEGE table
- The SupervisorID in the FACULTY table references the EmpID as its primary key within the same table
- Since tables can no longer have constraints added after the column declaration and data type, alter the structure of the table in order to do it
Adding a Foreign Key Constraint
- Syntax:
ALTER TABLE table_name
ADD CONSTRAINT constraint_name
FOREIGN KEY (column_name)
REFERENCES reference_table_name(reference_column_name)
- Constraint for the colcode in the faculty table referencing the colcode in the college table:
alter table FACULTY
add constraint colcode_fk foreign key(colcode) references college(colcode)
- Constraint for the supervisorID in the table faculty referencing the EmpID in the same table:
alter table FACULTY
add constraint sup_fk foreign key(supervisorID) references Faculty(empID)
- Drag the link to its respective primary key and foreign key when setting up the new database diagram
- The key represents the primary key and the infinite sign represents the foreign key. The
colcode_fk
constraint represents the relationship between the two tables, while thesup_fk
constraint represents the relationship in the same table - When adding a foreign key constraint, ensure that the data type matches its referencing primary key
Deleting a Constraint from a Table
- Syntax:
ALTER TABLE table_name
DROP CONSTRAINT constraint_name
- Example:
ALTER TABLE FACULTY
DROP CONSTRAINT sup_fk
- Refresh and generate a new database table to confirm deletion. Only the
colcode_fk
constraint should remain
Adding a Column to an Existing Table
- Syntax:
ALTER TABLE table_name
ADD column_name datatype
- Example:
ALTER TABLE COLLEGE
ADD ColDesc varchar(50)
Deleting a Column to an Existing Table
- Syntax:
ALTER TABLE table_name
DROP COLUMN column_name
- Example:
ALTER TABLE COLLEGE
DROP COLUMN ColDesc
Changing the Column Definition of an Existing Table
- For example, altering the maximum number of characters accepted in the ColName column of the COLLEGE table from varchar(20) to varchar(30)
- Syntax:
ALTER TABLE table_name
ALTER COLUMN column_name new_datatype
- Example:
ALTER TABLE COLLEGE
ALTER COLUMN ColName varchar(30)
Adding a Primary Key Constraint to an Existing Table
- Assume the COLLEGE table has been created, but the primary key was not indicated
- Syntax:
ALTER TABLE table_name
ADD CONSTRAINT constraint_name
PRIMARY KEY (column_name)
- Example:
ALTER TABLE COLLEGE
ADD CONSTRAINT col_pk
PRIMARY KEY (ColCode)
- Ensure NOT NULL is set to the declaration of the column before adding the constraint
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.