SQL: A Commercial Database Language Flashcards
The name SQL is derived from
Structured Query Language
Sequel was originally called ______.
SEQUEL ( for Structure English QUEry Language).
What does it mean that SQL is a comprehensive database language?
It has statements for data defintion, query and update (in this sense it is both a DDL and a DML). In addiotion, it has facitilities for defining views, for specifying security anduthorization, for defining integrity constaints, and for specifying transaction controls. It also has rules for embedding SQL statements into a general-purpose programming language.
The SQL language provides a high-level declarative language interface meaning
The SQL language provides a high-level declarative language interface meaning the user specifies what the result is to be and leaves the decisions on how to execute the query to the DBMS.
The terms table, row, and column are used in SQL in documents for ____, ____ and ____ respectively.
table = relation
row = tuple
column = attribute
is SQL case sensitive?
No. It treats upper and lower case letters as the same letter. Only inside quotes does SQL make a distinction between upper and lower case letters.
The concept of ____ is used in SQL to group together constructs that belong to the same database application
schema
A schema includes ______ such as tables constraints, view, domains, authorization grants and other constructs.
schema elements
A SQL schema is identified by a schema name and includes an _______ to indicate the user who owns the scema. In addition, it can include _______ for every element in the schema.
authorization identifier ; descriptors
Write a SQL statement that creates a schema called COMPANY owned by a user with authorization identifier JSMITH
CREATE SCHEMA COMPANY AUTHORIZATION JSMITH;
The concept of a ______ is used in SQL to denote a named collection of schemas.
catalog
A catalog always contains a special schema called _____. This schema provides information on all the schemas in the catalog and all the element descriptors in these schemas.
INFORMATION_SCHEMA
True or False: Some elements (e.g. domain definitions) can be shared by schemas in the same catalog.
Integrity constraints can be defined between relations only if they do not exist in schedmas of the same catalog
True.
False. Integrity constraints can be defined between relations only if they exist in schemas of the same catalog.
What data types are included in SQL?
numerics, character-string, bit-string, data and time.
What are the numeric types in SQL and how are they specified?
What are the character string data types in SQL and how are they specified?
What are the bit string data types in SQL and how are they specified?
What are the data and time data types in SQL and how are they specified?
How do you create a domain in SQL on SSN_TYPE to be a character data type consisting of 9 characters?
CREATE DOMAIN SSN_TYPE AS CHAR(9);
A create domain statement can have an optional _______ and an optional
_____________.
.
default specification ; list of constraints
What does the CREATE TABLE command do?
The CREATE TABLE command is used to specify a new relation, its attributes, their data types and constraints.
The key, entity integrity and referential integrity constraints can be specified within a CREATE TABLE statement or later using the ALTER TABLE command.
Create a relation named department with attributes: DNAME, DNUMBER, MGRSSN, MGRSTARTDATE. The primary key is DNUMBER and the MGRSSN and EMPLOYEE are foreign keys. DNAME is a secondary key (unique). Write a command to create this relation.
A constraint can be given a name following the keyword ________. Giving names to constraints, is:
A) optional but is a good practice to follow.
B) necessary and a bad practice to follow.
C) unnecessary but a bad practice to follow.
D) necessary and a good practice to follow.
CONSTRAINT;
A) optional but is a good practice to follow.
True or False: The names of some of the constraints within a particular can match (not unique).
False. The names of all constraints withing a particular schema must be unique. A constraint name can be used to drop a constraint (and replace it later by another constraint).
What is SQL’s default policy for any modificiations that violate the referential integrity constraint?
Any modifications violating the referential integrity constraints is rejected by the system.
In SQL, the schema designer can specify the action of a referenced primary key value, by attaching a referential triggered action clause to a foreign key constraint.
What are three options?
SET NULL, CASCADE, SET DEFAULT
In SQL, the schema designer can specify the action of a referenced primary key value, by attaching a referential triggered action clause to a foreign key constraint.
The options include SET NULL, CASCADE, SET DEFAULT.
They can be qualified on either ______ or ______.
.
ON DELETE ; ON UPDATE
How would you specify a constraint called EMPPK that enforces for the EMPLOYEE relation’s primary key is SSN?
How would you specify a constraint called EMPSUPERFK that enforces for the EMPLOYEE relation’s primary foreign key is SUPERSSN (references relation EMPLOYEE), sets the value to null on delete, and cascades the changes on update?
How would you specify a constraint called EMPDEPTFK that enforces for the EMPLOYEE foreign key references DEPARTMENT, on delete sets the value to the default value of 4, and on update cascades the changes?
How would you specify a constraint called EMPDEPTFK that enforces for the EMPLOYEE foreign key references DEPARTMENT, on delete sets the value to the default value of 4, and on update cascades the changes?
Can the name of a schema be explicitly specified in the definition of a table?
Yes
CREATE TABLE COMPANY.DEPARTMENT….
A schema can be deleted using the _______ command. It has two options: _____ and _____.
DROP SCHEMA; CASCADE; RESTRICT
For DROP SCHEMA, what happens if RESTRICT is specified ?
The operation will fail if the schema if nonempty; otherwise it will be succeed and the schema will be destroyed.
For DROP SCHEMA, what happens if CASCADE is specified ?
If CASCADE is spedicifed the operation will always succeed.
DROP SCHEMA COMPANY CASCADE;
A table can be deleted using the _________ command. Two options: _____ and _____.
DROP TABLE, CASCADE, RESTRICT
For DROP TABLE, what happens if RESTRICT is specified ?
If RESTRICT is specified, the operation will succeed only if the table is not referenced in any constraints or views.
For DROP TABLE, what happens if CASCADE is specified ?
If CASCADE is specified, the operation will always succeed and propogate to all constraints and views that reference the table.
DROP TABLE DEPENDENT CASCADE;
How do you add a column to relation EMPLOYEE with attribute name JOB and domain of variable char of length 12?
ALTER TABLE COMPANY.EMPLOYEE ADD JOB VARCHAR(12);
ALTER TABLE COMPANY.EMPLOYEE ADD JOB VARCHAR(12) NOT NULL;
What does this command do?
This command creates a new attribute for relation EMPLOYEE called JOB which is a character string with variable length up to 12 NOT NULL forces this attribute to have a value. A DEFAULT clause can also be specified. If no such clause is specified the new attribute will have NULLs in all the tuples of the relation after the command is executed.
How to drop a column called ADDRESS from relation EMPLOYEE with so that the deletion is always successful and propogates to all constraints and views that referencee the column?
ALTER TABLE COMPANY.EMPLOYEE DROP ADDRESS CASCADE;
When dropping a column, what is the difference between CASCADE and RESTRICT?
The CASCADE option: all constraints and views that reference the column are dropped from the schema.
The RESTRICT option. the command is successful only if no views or constraints reference the column.
How would you drop a default clause in a column called MGRSSN for relation DEPARTMENT in the COMPANY SCHEMA?
ALTER TABLE COMPANY.DEPARTMENT ALTER MGRSSN DROP DEFAULT;
How would you add a default clause to a column called MGRSSN in relation DEPARTMENT in the COMPANY schema such that the default MGRSSN is now ‘33344555’?
Adding a default clause in a column:
ALTER TABLE COMPANY.DEPARTMENT ALTER MGRSSN SET DEFAULT ‘33344555’;
What must a constraint have in order to be dropped?
To be dropped a constraint must have a name.
Write the command that drops the constrain from relation EMPLOYEE called EMPSUPERFK such that all foreign key definitions that reference the candidate key will be dropped too.
ALTER TABLE COMPANY.EMPLOYEE DROP CONSTRAINT EMPSUPERFK CASCADE;
When dropping a CONSTRAINT, CASCADE and RESTRICT have meaning only if _____________.
the constraint is specified, all the foreign key definitions that reference the candidate key will be dropped too.
Write a command that adds a constraint called EMPSUPERFK that enforces the foreign key SUPERSSN references an EMPLOYEE.
ALTER TABLE COMPANY.EMPLOYEE ADD CONSTRAINT EMPSUPERFK FOREIGN KEY SUPERSSN REFERENCES EMPLOYEE;
True or False: When adding a constraing using ALTER TABLE, the constraint to be created can be named or unnamed.
True.
What is the problem with these table definitions?
The problem is taht either EMPLOYEE or DEPARTMENT table has to be defined first.
In both cases, the system will issue an error message.
What is a possible solution to the problem in this code?
The chicken and egg problem in this code can be solves by postponing the foreign-key constraint in the first table. For instance, if CREATE TABLE EMPLOYEE is executed first, we should not have the foreign key clause FOREIGN KEY (DNO) … in the statement.
After CREATE TABLE DEPARTMENT has been process, we can add the desired constraint to employee using the ALTER TABLE command:
If we want to populate our COMPANY database, we face another chicken and egg problem because insertions of EMPLOYEE’s with non NULL DNO’s will be rejected because it violates the foreign key constraint:
MRGSSN references EMPLOYEE(SSN). What is a solution to this problem?
One solution is to initially replace DNO component in all tuples to be inserted in the EMPLOYEE relation with NULL. Then, when DEPARTMENT is populated with appropriate tuples, we can scan relation EMPLOYEE and replace NULLs with valid department number. This solution is awkward and error-prone. A better solution is to use transactions and deferred checking of integrity constraints.
SQL has one basic statement for retrieving information from a database; the _______.
SELECT statement
The SELECT statement in SQL is (same/ not same) as the SELECT operation of relational algebra.
Not the same. The Important distinction between SQL and the formal relational model : SQL allow a table (relation) to have two or more tuples that are identical in all their attribute values.
A SQL relation (table) is a multi-set (sometimes called a _____) of tuples; it is not a set of tuples.
bag
SQL relations can be constrained to be sets by specifying PRIMARY KEY or UNIQUE attributes in a table definition or by USING _____ option in a query.
DISTINCT
In a SELECT-FROM-WHERE block, SELECT takes an <attribute> as an argument. what does attribute list refer to ?</attribute>
attibute list is a list of attribute names whose values are to be retrieved by the query