Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

 Primary key – one column that can uniquely identify a row

 Foreign Key – links that table to another Table’s Primary Key


 SQL is not case-sensitive
 RDMS –Relational Database Management System: includes tools and applications to help
manage larger-scale database installations
 Comments
 -- is for single-line comments
 /* is for multi-line comments */

SELECT:

SELECT VALUES FROM TABLENAME ;

 <TABLENAME.COLUMNNAME>
 SELECT p.person_first_name FROM person p; p=alias
 SELECT DISTINCT p.person_first_name FROM person p; p=alias
 WHERE clause – TRUE or FALSE : SELECT p.person_first_name FROM person p WHERE
p.person_first_name = ‘Jon’ ;
 BETWEEN: - acts on a column and two values: SELECT p.person_first_name FROM person p
WHERE p.contacted_number BETWEEN 1 AND 20 ;
 LIKE: just for strings: SELECT p.person_first_name FROM person p WHERE
p.person_first_name LIKE ‘J%’;
 IN: a column and a list of potential values (values can be numeric or string or date)
SELECT p.person_first_name FROM person p WHERE p.person_first_name IN (‘Jon’ ,
‘Shannon’, ‘Fritz’ ) ;
 IS: -helps address when a value in a column might be NULL; SELECT p.person_first_name
FROM person p WHERE p.last_name IS NULL;
 IS NOT: SELECT p.person_first_name FROM person p WHERE p.last_name IS NOT NULL;

ORDER BY:

-comes after the WHERE clause, but the WHERE clause isn’t required;

- Ascending (ACS) – default; you can use DESC to descending

Exemple: SELECT p.person_first_name, p.person_lat_name FROM person p ORDER BY


p.person_last_name;

SET FUNCTIONS

- Some of those functions help to turn column data from Tables into computed values
- Parameter of the function – the column name; PHOTO!!!
 COUNT – includes NULL values when used with *
 MAX – maximum value of the column specified – does not include NULL values
 MIN - does not include NULL values
 AVG - does not include NULL values ! ONLY ON NUMERIC columns
 SUM - does not include NULL values ! ONLY ON NUMERIC columns

** How many rows I have in the table? SELECT COUNT(*) as NR FROM person;
SELECT COUNT(DISTINCT a.adress_street) FROM address a;

GROUP BY –HAVING

JOINS
- Creating a new result set from two or more tables
1. CROSS
 Cartesian Product / all the rows and columns from both tables
 CROSS keyword is optional
2. INNER JOIN
 ON
3. OUTER JOIN – The difference between INNER and OUTER JOIN is that OUTER JOIN is ok with
null values.

CRUD – Create, Read, Update, Delete

1. INSERT
 INSERT INTO: Table name (only one Table at a time is allowed)
 Command: INSERT INTO person (person_id, person_first_name, person_date_added)
VALUES (1, ‘John’,’2013-01-14 11:43:31’);
2. UPDATE
 Without WHERE clause, will affect all rows in the Table
 UPDATE…SET
3. DELETE
 DELETE FROM person; - all the rows in the table person

Transactions

ACID - Atomic, Consistent, Isolated, Durable

 Syntax
START TRANSACTION;
DELETE FROM
Person;
COMMIT;
Or ROLLBACK

Creating the Database – not ANSI SQL


Syntax: CREATE DETABASE Person;

USE DATABASE Person;

CREATE TABLE –part of the ANSI sql

Syntax: CREATE TABLE email_address (email_address_id INTEGER, email_address_person_id


INTEGERE, email_address VARACHAR(55));

PRIMART KEY: CREATE TABLE email_address (email_address_id INTEGER PRIMARY KEY,…

CONSTRAINT:

CREATE TABLE phone_number( phone_number_id INTEGER NOT NULL,…, CONSTRAINT


PK_phone_number PRIMARY KEY (phone_number_id));

ALTER TABLE

- Enables you to add or change a column definicion or CONSTRAINT on an existing Table


- Allows you to do anything that you could do in CREATE TABLE definicion
- WARNING! For example, you can’t change a column to NOT NULL if it already has NULL data in it.
- Syntax:
ALTER TABLE email_address
ADD CONSTRAINT FK_email_address_person FOREIGN KEY(email_address_person_id)
REFERENCES person (person_id);

DROP TABLE
- Removes the Table from the database
-

You might also like