CH 2 - SQL Flashcards
SQL
structured query language. its the ANSI language use for defintion and manipulation of relational database
ddl
data definition language. statements that specify and modify database schemas.
e.g
CREATE TABLE
DROP TABLE. removes a table from databse
ALTER TABLE. modifies a table after created
CREATE INDEX
dml
data manipulation language. statements that manipulate databse content.
eg
INSERT
UPDATE
DELETE
facts of sql
sql data is case sensitive while commands are not .
create table example
CREATE TABLE Student (
StudentID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
DateOfBirth DATE,
Gender CHAR(1),
Email VARCHAR(100)
);
e.g of alter a table or drop
ALTER TABLE Student
ADD PhoneNumber VARCHAR(15);
DROP TABLE Student;
to avoid errors: DROP TABLE IF EXISTS Student;
explain the 2 drop options
CASCADE : specifies that any foreign key constraint violations that are caused by dropping the table will cause the correspnding rows of the related table to be deleted.
RESTRICT : blocks the deletion of the table of any foreign key constraint violations would be created.
Syntax;
DROP TABLE <table name>[RESTRICT | CASCADE];
learn syntax
DML COMMANDS
INSERT: adds new rows to tables
UPDATE: modifies one or more attributes
DELETE: deletes one or more rows from a table
e.g of instering a row
INSERT into<table name>
VALUES(‘value 1’ , ‘value2’ , NULL);
INSERT into FoodCart
VALUES(‘02/08/24’ , ‘ pizza’,’70’)
e.g of updating an attribute with sql
UPDATE <table name> SET <attr> = <value>
WHERE <selection>;</selection></value></attr>
e.g
UPDATE FoodCart SET sold = 349
WHERE date = ‘02/25/24’ AND food = ‘pizza’;
DELETE a row in a table e.g
DELETE FROM <Table name>
WHERE <condition>;</condition>
DELETE FROM FoodCart
WHERE food = ‘hotdog’;
SQL statement
basic SELECT statement includes 3 clauses
SELECT <attribute> FROM <tables> WHERE <condition></condition></tables></attribute>
select specifies the attributes that are part of the resulting relation
FROM = specifies the tables that serve as the input to the statement
WHERE = specifies the selection condition , including the join condition.
// is it possible to use this without WHERE condition?
nb// * in the select statement means that every attribute of the input table is to be selected.
SELECT * FROM …WHERE….;
to get unique rows use DISTINCT keyword after SELECT e.g
SELECT DISTINCT * FROM …
WHERE …;
SQL operations
join
left join
right join
like
sql clauses
order by
Group by
Having
Join operation
its specified as the FROM clause which list the two inut relations and the WHERE clause which lists the join condition