SQL Fundamentals

PainlessAllusion avatar
PainlessAllusion
·
·
Download

Start Quiz

Study Flashcards

22 Questions

What is a primary key defined through?

Constraint

What are foreign keys also defined as?

Constraint

What happens to entities in a database?

They become tables

What is required of primary key attributes?

They must be not null and unique

How are many-to-many relationships represented in a database?

As two many-to-one relationships

What SQL command is used to delete a table?

DROP TABLE

What is used to define a foreign key?

The columns which make up the FK, the referenced table, and the columns which are referenced by the FK

What happens to attributes in a database?

They become columns with the same name

What is the purpose of the DROP TABLE command?

To remove a table from the database

Which command is used to add a new column to a table?

ALTER TABLE r ADD A D

What is the purpose of the UPDATE command?

To change row(s) in a table

What is the use of the WHERE clause in UPDATE and DELETE commands?

To specify which rows to change or remove

What is the command to add a constraint to a table?

ALTER TABLE r ADD CONSTRAINT

What is the command to delete all tuples from a table?

DELETE FROM r

What was SQL originally called?

Sequel

Which of the following SQL data types is a fixed-length character string?

char(n)

What is the purpose of the create table command in SQL?

To define an SQL relation

What is the primary purpose of integrity constraints in SQL?

To restrict the set of allowed values for an attribute

What is the name of the standard that most modern DBMS use in SQL?

SQL-92

What is the data type of the attribute 'budget' in the 'department' table?

numeric(12,2)

What is the purpose of the 'primary key' constraint in SQL?

To specify a unique identifier for a table

In which year was SQL-86 standard introduced?

1986

Study Notes

Overview to SQL

  • Originally called 'Sequel', Structured English Query Language
  • Designed and implemented at IBM, with ANSI Standards: SQL-86 or SQL1, SQL-92 or SQL2, SQL-99 or SQL3
  • Most modern DBMS use a variety of SQL, mostly based on SQL2, increasingly SQL3
  • Can be used from other languages and often extended to provide common programming constructs (if-then tests, loops, variables, etc.)

SQL Data Types

  • char(n): fixed-length character string
  • varchar(n): variable-length character string with specific maximum length n
  • int: integer
  • smallint: small integer
  • numeric(p, d): fixed-point number with user-specified precision
  • float(n): floating-point number, with precision of at least n digits
  • real, double precision

SQL Basic Schema Definition

  • Defines an SQL relation using the create table command
  • General form: create table r (A1 D1, A2 D2, ..., An Dn, ...);
  • r is the name of the relation, Ai is the name of an attribute, Di specifies the type of attribute Ai

Integrity Constraints

  • SQL prevents updates that violate an integrity constraint
  • Supports various integrity constraints, including:
    • Primary key: primary key attributes are required to be not null and unique
    • Foreign key: defined through constraints, specifying columns, referenced table, and referenced columns

Implementing E/R Designs

  • Entities and attributes: each entity becomes a table, with attributes as columns
  • Primary keys are defined through constraints
  • Relationships:
    • 1:1: usually not used or treated as a special case of M:1
    • M:1: represented as a foreign key from the M-side to the 1
    • M:M: split into two M:1 relationships

Example

  • Creating tables for department, course, and instructor entities
  • Defining primary keys and foreign keys

More SQL

  • DROP TABLE: deletes a table
  • ALTER TABLE: changes a table
    • Adds or removes a column
    • Adds or removes a constraint
  • INSERT, UPDATE, and DELETE: add, change, or remove rows from a table
    • Use WHERE clauses to specify which rows to change or remove
    • Be careful with these, as incorrect WHERE clauses can destroy data

Deleting Tables

  • DROP TABLE removes a table
  • DELETE FROM removes all tuples from a table

Changing Tables

  • ALTER TABLE:
    • Adds a new column: alter table r add A D;
    • Removes an existing column: alter table r drop A;
    • Adds a new constraint: alter table r add constraint;
    • Removes an existing constraint: alter table r drop constraint;

INSERT and UPDATE

  • INSERT INTO: adds a row to a table
  • UPDATE: changes row(s) in a table
  • Both use WHERE clauses to specify which rows to change or add

This quiz covers the basics of SQL, including data types, schema definition, integrity constraints, and E-R designs. It also explores the history of SQL and its development.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free
Use Quizgecko on...
Browser
Browser