Beruflich Dokumente
Kultur Dokumente
SQL | PHP
SQL intro
There are many different versions of SQL available for usage. Oracle MySQL SQLite DB2 Mimer The popular ones are Oracle and MySQL with MySQL quickly gaining ground. Ill be showing you MySQL. The syntax between SQL domains varies little and with google skills you can adjust this knowledge for SQLite, which Ive never used.
Databases _ creation
CREATE TABLE tableName ( name VARCHAR(55), sex CHAR(1), age INT(3), birthdate DATE, salary DECIMAL(10,2), primary key(name) ); Types of attributes: char, varchar, int, smallint, decimal, date, float, etc. *varchar is a string with varying # of characters. In our example, 55 is the characters longest possible string allowed. *decimal(10,2) indicated 2 places after the decimal point and 10 total digits (including the decimal numbers)
Databases _ creation 2
CREATE TABLE tableName ( name VARCHAR(55), sex CHAR(1) NOT NULL, age INT(3), birthdate DATE, salary DECIMAL(10,2) DEFAULT 0.00, primary key(name) ); Primary key: primary key is a UNIQUE value. For every entry in your database this must be unique and not null and every DB must have one. NOT NULL: column must have a value DEFAULT: you can set a default value if no other value is inputted for that column.
Databases _ deletion
DROP TABLE tableName;
Databases _ insertion
Inserting data in the database: INSERT INTO tableName(name,sex,age) VALUES(Mr. Freeze,M,42); Also valid: INSERT INTO tableName(sex,name,age) VALUES(F,Mr. Freeze,42); Order doesnt matter.
Databases _ updating
Suppose we want to change Mr. Freezes age to 52.
UPDATE tableName SET age = 52 WHERE name LIKE Mr. Freeze And so forth.
Databases _ aggregates
This is the actual meat of using SQL. These are where you set your conditions, narrow down your table into a usable set. Here are the usable functions, Ill show a quick example with each. The only way to really know this stuff is practice. Group by Count Sum Avg Min/Max Order by
Databases _ group by
This is the actual meat of using SQL. These are where you set your conditions, narrow down your table into a usable set. Here are the usable functions, Ill show a quick example with each. The only way to really know this stuff is practice. Group by lumps all the common attributes into one row. SELECT employee_id, MAX(salary) FROM Works_In GROUP BY dept_id; * MAX selects the maximum value in its () likewise for MIN
Databases _ count
Count counts the number of columns with the specified attribute.
SELECT term, COUNT(course_id) FROM teaches GROUP BY term; We counted the number of courses taught during x term. AVG & SUM function pretty much the same way.
PHP _ insertion
<?php mysql_connect(localhost,username,pw) or die(mysql_error()); mysql_select_db(ljlayou_comp353 or die(mysql_error()); mysql_query(INSERT INTO Works_In(company,position) VALUES(McDonalds,fry cook)); ?> Were querying PHP to tell MySQL to do something, in this case create the table. The same applies for dropping a table. As you can see our code is being reused over and over. It gets pretty repetitive like this.
<?php