SQL Data Manipulation (Week 12 Test) Flashcards
What is SQL?
SQL or Structured Query Language is a language that has emerged from the development of the relational model
SQL has become the standard relational database language
What are the primary objectives of SQL?
- Create the database and relation structures
- Perform basic data management tasks
- Perform simple and complex queries
- Minimize user effort and ensure ease of learning
- Be portable across different DBMS
What are the two major components of SQL?
- Data Definition Language (DDL)
- Data Manipulation Language (DML)
What does Data Definition Language (DDL) allow?
Allows definition of database structure, specification of data types and constraints, and controlling access to the data
Examples include CREATE table, ALTER table, DROP table
What is the purpose of Data Manipulation Language (DML)?
Allows retrieving (querying) and updating of data
Examples include SELECT, INSERT, UPDATE, DELETE
How is SQL described in terms of procedural structure?
It is non-procedural; you specify what information you require, rather than how to get it
What is the syntax for creating a table in SQL?
CREATE TABLE DatabaseName.NewTableName (ColumnName1 DataType OptionalConstraint, …)
Optional table constraints can also be included
What are the main data types available in SQL?
- Exact numeric: INT, SMALLINT, BIGINT, DECIMAL
- Approximate numeric: DOUBLE, FLOAT, REAL
- Date/Time: DATETIME, DATE, TIME, TIMESTAMP
- Character/String: CHAR, VARCHAR
What is an integrity constraint in SQL?
Rules that ensure the accuracy and consistency of data within a database
What does the NOT NULL constraint enforce?
Specifies that a column must contain a value
This is used for columns that cannot have null values
What is the purpose of the PRIMARY KEY constraint?
Ensures that each value in the column is unique and not null
What does the FOREIGN KEY constraint do?
Links a column to the primary key of another table, ensuring referential integrity
What is the syntax for altering a table to add a new column?
ALTER TABLE TableName ADD ColumnName DataType
What SQL command is used to remove a table?
DROP TABLE TableName
What is the general syntax of a SELECT statement?
SELECT [DISTINCT | ALL] {* | [columnExpression [AS newName]] [,…]}
FROM TableName [alias] [, …]
[WHERE condition]
[GROUP BY columnList]
[HAVING condition]
[ORDER BY columnList]
What is the purpose of the WHERE clause in a SELECT statement?
Filters rows subject to some condition
What does the ORDER BY clause do in a SQL query?
Specifies the order of the output
What is the purpose of the DISTINCT keyword in SQL?
Eliminates duplicate rows from the result set
What is a calculated field in SQL?
A field that is derived from other fields in the data set, often using arithmetic operations
Fill in the blank: The command to modify an existing column in a table is ______.
ALTER TABLE
True or False: SQL commands are case sensitive.
False
Most components of an SQL statement are case insensitive except for literal character data
What SQL command is used to eliminate duplicates in a query result?
DISTINCT
DISTINCT is applied in the SELECT statement to return only unique values.
How do you produce a list of monthly salaries for all staff?
SELECT StaffNo, Forename, Surname, Salary/12
FROM STAFF;
This query divides the annual salary by 12 to calculate the monthly salary.
What clause is used to rename a column in SQL?
AS clause
The AS clause allows you to give a column a temporary name in the result set.