DB
DB
ASC, AT, CHAR (CHR), CHAR_LENGTH (LEN), LOWER, TRIM, SPACE, SUBSTRING
(SUBSTR/MID), UPPER, VALUE (VAL)
DATE, DAY, MONTH, YEAR
ADD, ALL, ALTER, ANY, AS, ASC, BETWEEN, BY, CREATE, DELETE, DESC, DISTINCT,
DROP, EXISTS, FROM, GROUP, HAVING, IN, INDEX, INNER JOIN, INSERT, INTEGER,
INTERSECT, INTO, LEFT [OUTER] JOIN, LIKE, MINUS, NULL, RIGHT [OUTER] JOIN,
FULL [OUTER] JOIN, ON, ORDER, SELECT, SET, TABLE, TO, UNION, UNIQUE,
UPDATE, VALUES, VIEW, WHERE
INSERT INTO STUDENT (CNO, NAME, SID, CLASS) VALUES (28, 'Eva', '03505', '1B') # can be any order
TABLE FIELD VALUE
SELECY NAME, CNO FROM STUDENT WHERE CLASS = '1B' ORDER BY CNO DESC
SELECY NAME, CNO FROM STUDENT WHERE CLASS = '1B' ORDER BY CNO # default order is ASC
UPDATE STUDENT SET NAME = 'Bob', CLASS = '1B' WHERE SID = '03517' # UPDATE ... SET
ALTER TABLE STUDENT MODIFY SID INT # change data type of SID to INT
ALTER TABLE STUDENT ADD NAME VARCHAR(40) DEFAULT 'to be assigned' # adding filed
ALTER TABLE STUDENT MODIFY SID CHAR(5) PRIMARY KEY # input the data tyoe is needed
ALTER TABLE STUDENT DROP FOREIGN KEY cn # cn is constraint name which will be given
SELECT SID, NAME FROM STUDENT WHERE CLASS IN ('1A', '1B', '2C')
SELECT SID, NAME FROM STUDENT WHERE DOB BETWEEN '2013-08-03' AND '2013-09-22'
SELECT SID, NAME FROM STUDENT WHERE SCORE1 IS NULL / IS NOT NULL
SELECT TRIM(' A Cat! ') # result: 'A cat!' (removes all spaces from the start and the end of <string)
Aggregation functions
SELECT COUNT(*) FROM STUDENT # count total number of records, include NULL
SELECT CLASS, SEX, COUNT(*) FROM STUDENT GOURP BY CLASS, SEX ORDER BY CLASS
SELECT NAME, SCORE FROM STUDENT WHERE SCORE = (SELECT MAX(SCORE) FROM STUDENT) # subquery
SELECT * FROM STUDENT NATURAL JOIN CLUB # natural join, which = inner join, but join automatically by using the only pair of
same field name
SELECT s1.NAME, s2.NAME AS Monitor_NAME FROM STUDENT s1 INNER JOIN STUDENT s2 ON s1.Monitor_SID = s2.SID
SELECT NAME FROM STUDENT WHERE SID NOT IN (SELECT SID FROM SC)
SELECT NAME FROM STUDENT WHERE CLASS = ANY (SELECT CALSS FROM STUDENT2) # must follow a subquery
SELECT NAME FROM STUDENT WHERE CLASS > ALL (SELECT CALSS FROM STUDENT2) # must follow a subquery
= ALL != IN
<> ALL = NOT IN
select * from Table2 h where (select count(c.ID) from Table1 c where HID = h.ID) > capacity
select h.NAME FROM CAT c, HOTEL h where HID = h.ID group by HID HAVING COUNT(c.ID) > AVG(capacity)
SELECT SID FROM STUDENT1 UNION SELECT STUDENT_ID FROM STUDENT2 ORDER BY SID
# fieldname from the 2nd table are not carried to the combined result
# duplicated rows(completely identical in all rows) are removed
CREATE INDEX SNAME ON STUDENT (NAME) # <index name>, <table name>, <dield to be indexed>
# normally, the name of the index is used only when dropping the index
ALTER TABLE STUDENT DROP INDEX SNAME # SNAME is the index name
A schema for a table: STUDENT(SID, NAMEm CLASS) # primary key should be underlined
violate e. i.:
no primary key
duplicate primary key
primary key is null
referential integrity:
the references of foreign keys in a table are valid
violate r. i.:
insert/update record with foreign key cannot be found in the referenced table
delete/update the primary key in the referenced table
domain integrity:
value of an attribute must fall within a reasonable domain
rollback
| must
O optional
cardinality
anomaly / anomalies
update anomaly occurs when a database with data redundancy fails to maintain data consistency after an update is made
insertion anomaly refers to the inability to insert data of one entity type without having the data of another entity type
deletion anomaly refers to the loss of data of one entity type when deleting the data of another entity type
Functional dependency exists between 2 sets of attributes when the values of 1 set can be uniquely determined by the values of
another set
Full functional dependency: all attributes that are not primary key are dependent on the primary key
Partial functional dependency: some attributes that are not primary key are dependent on part of the primary key
# only exists when the primary key of a table is a composite key
Transitive functional dependency: some non-primary key attributes are dependent on other non-primary ket attributes
Normalisation
Denormalisation