php za pocetnike - predavanje 9

Post on 23-Feb-2017

274 Views

Category:

Education

11 Downloads

Preview:

Click to see full reader

TRANSCRIPT

PHP za početnike

Academy387 Nermin Šehić

Predavanje 9

Recap: predavanje 8• Baze podataka

• DBMS

• Tabele

• Redovi

• Kolone

• Atributi

• Primary key

• Foreign key

• Query

Dodavaje foreign key-a

ALTER TABLE child_table_name ADD FOREIGN KEY (foreign_key_name) REFERENCES parent_table_name (table_id)

Vjezba 1• Napraviti novu bazu blog

• Napraviti dvije table author i article

• U tabeli author dodati polja id, name, email, pop

• U tabeli article dodati polja id, title, text, author_id

• Polje author_id u tabeli article je foreign key od tabele author

• Uraditi dva INSERT query-a u tabelu author

JOINS

• Inner join

• Left join

• Right join

• Outer join

id name course_fk

1 John Doe 1

2 Foo Bar 3

3 Branko Copic 5

id name

1 HTML5

2 PHP

3 CSS

INNER JOIN

SELECT user.name, course.nameFROM `user` INNER JOIN `course` on user.course = course.id;

LEFT JOIN

SELECT user.name, course.nameFROM `user` LEFT JOIN `course` on user.course = course.id;

RIGHT JOIN

SELECT user.name, course.nameFROM `user` RIGHT JOIN `course` on user.course = course.id;

OUTER JOIN

OUTER JOIN

SELECT user.name, course.name| FROM `user ̀LEFT JOIN `course` on user.course = course.id

UNIONSELECT user.name, course.name FROM `user ̀RIGHT JOIN `course` on user.course = course.id;

Alternativni pristup

• SELECT * FROM table1, table2 WHERE table2.fk = table1.pk

• fk - foreign key

• pk - primary key

Primer 1

• Dodavanje u tabelu sa foreign key-em

• INSERT INTO table_name (column1, column2, foreign_key) VALUES (value1, value2, foreign_key_value)

Vjezba 2

• Koristeci bazu iz vjezbe 1, dodati u tabelu article 2 reda sa razliciom fk referencom

• Koristeci jedan od join metoda uraditi SELECT query

ERD

• Entity relationship diagram

• Koristi se za vizuelnu prezentaciju baze, njenih atributa i veza medju tabelama

Primjer ERD

Vjezba 3

• Koristeci bazu iz vjezbi 1 i 2 i nacrtati ERD

Vjezba 4

• Isprogramirati bazu na osnovu zadanog ERD

Vjezba 4

Korisne MySQL komande

• SELECT * FROM tableName LIMIT 30

• SELECT COUNT(pk) FROM tableName

• SELECT COUNT(id) as count FROM author

Korisne MySQL komande

• SELECT * FROM tableName ORDER BY pk DESC

• SELECT * FROM tableName ORDER BY pk ASC

• SELECT tN.pk FROM tableName tN

To sum up…• Dodavanje FK constraint-a

• Left join

• Right join

• Inner join

• Outer join

• ERD

• MySQL Komande

Pitanja?

• Naredno predavanje: 10.11.2015

• http://www.slideshare.net/NerminSehic/php-za-pocetnike-predavanje-9

top related