Basic SQL Statements Flashcards
Week 8 Content
Name some RDBMS( Relational Database Management Services)
MySQL
MS Access
SQL Server
Oracle
Write sample code creating a table called newTable from an existing table called oldTable and explain what’s happening?
select * into newTable from oldTable
this code creates a table called newTable and duplicates information from table into it.
NOTE: instead of oldTable which is a base table, a derived table can be used instead
Given a table called Customers with these columns : customerName, address, DOB. Create the following views.
a. View called Name&Addr that only has customerName and address columns.
b. View called customerDOB that only has DOB
c. View called CustomersView that has all the columns.
a. create view Name&Addr as
select customerName, address from Customers
b. create view customerDOB as
select DOB from Customers
c. create view CustomersView as
select * from Customers
What are the limitations that come with views
Defines a logical table from one or more tables or views.
There are limitations on updating data through a view.
Where views can be updated, those changes can be transferred to the underlying base tables
originally referenced to create the view.
How does the DEFAULT constraint work?
Automatically assigns a value to a column if no data was given
What is a candidate key?
A column or set of columns designated as UNIQUE, where only one candidate key can be a PRIMARY KEY. A table can have multiple candidate keys.
How do you drop a column called address from the Customer table using ALTER TABLE?
Using the DROP COLUMN statement followed by the column name.
ALTER TABLE Customers
DROP COLUMN address
What is the difference between a base table and a derived table in SQL?
A base table is a standalone table(Table made with a CREATE statement), while a derived table is created based on an existing table or query(Exists only in query).
How can you add a new column to an existing table using ALTER TABLE?
By using the ADD keyword along with the column name, data type, and constraints.
ALTER TABLE CUSTOMERS
ADD age INT(3) NOT NULL;
How can you change the datatype of an existing column to VARCHAR(100) using ALTER TABLE?
ALTER TABLE CUSTOMERS
ALTER COLUMN customerName VARCHAR(100)
How can you drop a constraint using ALTER TABLE?
ALTER TABLE CUSTOMERS
DROP CONSTRAINT customer_PK
What can Alter table be used for?
- Adding a new column to an existing table
- Alter existing columns
- Add/Delete/Alter constraints using ADD, DROP, ALTER CONSTRAINT Keywords
When you add a new column to an existing table what value will the rows have for that column?
NULL
What does the Check constraint do? and provide syntax
Allows only certain values for this column/columns
example:
CONSTRAINT checkage CHECK (age >= 18)
True or False:
Some RDBMSs will not allow you to alter a table in a way that the current data in that table will
violate the new definitions
True