Rabu, 11 Juli 2012

Sesi X


MySQL adalah sistem manajemen relasi database (RDBMS) yang telah lebih dari 6 juta instalasi. MySQL adalah singkatan "My Structured Query Language". Program ini berjalan sebagai server menyediakan multi-user mengakses ke sejumlah database.
Source code tersedia di bawah ketentuan GNU General Public License, serta dalam berbagai perjanjian eksklusif. MySQL dimiliki dan disponsori oleh sebuah perrusahaan, MySQL AB di Swedia, yang kini merupakan anak perusahaan dari Sun Microsystems, yang memegang hak cipta untuk sebagian besar source code-nya.
MySQL umumnya digunakan oleh perangkat lunak bebas yang memerlukan fitur penuh sistem manajemen database, seperti Wordpress, phpBB dan perangkat lunak lain yang dibangun pada perangkat lunak LAMP. Ia juga digunakan dalam skala sangat tinggi World Wide Web, termasuk produk-produk Google dan Facebook.

Kelebihan MySQL

Sebagai software database dengan konsep database modern, MySQL memiliki banyak kelebihan.
  • Protability
MySQL dapat digunakan dengan stabil tanpa kendala, berarti pada berbagai sistem operasi diantaranya seperti Windows, Linux, Mac OS X Server, Solaris, Amiga HP-UX dan masih banyak lagi.
  • Open source MySQL didistribusikan secara open source di bawah lisensi GPL, sehingga dapat memperoleh menggunakannya secara cuma-cuma tanpa dipungut biaya sepeserpun.
  • Multiuser
MySQL dapat digunakan untuk menangani beberapa user dalam waktu yang bersamaan tanpa mengalami masalah atau konflik. Hal ini akan memungkinkan sebuah database server MySQL dapat diakses client secara bersamaan dalam waktu yang bersamaan pula.
  • Performance Tuning
MySQL memiliki kecepatan yang cukup menakjubkan dalam menangani query sederhana, serta mampu memproses lebih banyak SQL persatuan waktu.
  • Column Types
MySQL didukung tipe kolom(tipe data) yang sangat kompleks.
  • Command dan Functions
MySQL memiliki operator dan fungsi secara penuh yang mendukung perintah SELECT dan WHERE dalam query.
  • Scalability dan Limits
Dalam hal batas kemampuan, MySQL terbukti mampu menangani database dalam skala yang besar dengan jumlah record lebih dari 50 juta dan 60 ribu tabel serta 5 miliar baris. Selain itu batas indeks yang dapat ditampung mencapai 32 indeks pada setiap tabelnya.
Interface
Sama halnya dengan software database lainnya, MySQL memiliki interface (antarmuka) terhadap berbagai aplikasi dan bahasa pemrograman dengan menggunakan fungsi API (Aplication Programming Interface).
  • Struktur tabel
Struktur tabel MySQL cukup baik, serta cukup fleksibel. Misalnya ketika menangani Alter Table, dibandingkan database lainnya semacam ProgresSQL ataupun Oracle.


Start Client & Log In

mysql -u root -p then enter your password when prompted. You will then see the following output: 
Welcome to the MySQL monitor.  Commands end with ; or \g. Your MySQL connection id is 1 to server version: 5.0.51b Type 'help;' or '\h' for help. Type '\c' to clear the buffer. 
mysql -u root mysql update user set Password=password('NewPw') where User='root'; flush privileges; 


Creating a Simple Database and Displaying its Structure

mysql 5.0.51b> create database database01; Database "database01" created. 

mysql 5.0.51b> use database01 Database changed 

Beri Akses ke Database

grant CREATE, INSERT, SELECT, DELETE, UPDATE on namadatabase.* to username@localhost IDENTIFIED BY 'passworduser'; 

Create a table

mysql 5.0.51b> create table table01 (field01 integer, field02 char(10)); Query OK, 0 rows affected (0.00 sec) 
!Enclose entire list of field names between one pair of parentheses. !Commas are used between each field. iA space may be used after the comma between fields. !A comma is not used after last field. !This, and all SQL statements, are concluded by a semicolon ";". 

List the tables

mysql 5.0.51b> show tables; 
+----------------------+ | Tables in database01 | +----------------------+ | table01              | | table02              | +----------------------+ 


List the fields in a table

mysql 5.0.51b> show columns from table01; 
+---------+----------+------+-----+---------+-------+ | Field   | Type     | Null | Key | Default | Extra | +---------+----------+------+-----+---------+-------+ | field01 | int(11)  | YES  |     |         |       | | field02 | char(10) | YES  |     |         |       | +---------+----------+------+-----+---------+-------+ 


Insert a record

mysql 5.0.51b> insert into table01 (field01, field02) values (1, 'first');  Query OK, 1 row affected (0.00 sec) 
!Enclose entire list of field names between one pair of parentheses. !Enclose the values to be inserted between another pair of parentheses. !Commas are used between each field and between each value. iA space may be used after the comma between fields. 

List all the records in a table

mysql 5.0.51b> select * from table01; 
+---------+---------+ | field01 | field02 | +---------+---------+ |       1 | first   | +---------+---------+ 


Adding Fields

mysql 5.0.51b> alter table table01 add column field03 char(20); Query OK, 1 row affected (0.04 sec) Records: 1  Duplicates: 0  Warnings: 0 
mysql 5.0.51b> alter table table01 add column field04 date, add column field05 time;  Query OK, 1 row affected (0.04 sec) Records: 1  Duplicates: 0  Warnings: 0 
!The "add column" must be restated for each column. !Commas are used between each add column statement. iA space may be used after these commas. 
mysql 5.0.51b> select * from table01; 
+---------+---------+---------+---------+---------+ | field01 | field02 | field03 | field04 | field05 | +---------+---------+---------+---------+---------+ |       1 | first   | NULL    | NULL    | NULL    | +---------+---------+---------+---------+---------+ 


Multi-line Command Entry


Single Line Entry

mysql 5.0.51b> create table table33 (field01 integer,field02 char(30)); 

Multiple Line Entry

mysql 5.0.51b> create table table33 -> (field01 -> integer, -> field02 -> char(30)); 
!Don't break up words: Valid Invalid mysql 5.0.51b> create table table33 -> (field01 -> integer, -> field02 -> char(30));  mysql 5.0.51b> create table table33 -> (field01 inte -> ger, -> field02 -> char(30)); 
!When inserting or updating records, do not spread a field's string across multiple lines, otherwise the line breaks are stored in the record: 

Standard Operation

mysql 5.0.51b> insert into table33 (field02) -> values -> ('Who thought of foo?');   

Line Break Stored in Record

mysql 5.0.51b> insert into table33 (field02) -> values -> ('Pooh thought -> of foo.'); 
mysql 5.0.51b> select * from table33; 
+---------+---------------------+ | field01 | field02             | +---------+---------------------+ |    NULL | Who thought of foo? | |    NULL | Pooh thoughtof foo. | +---------+---------------------+ 

Insert Some More Records into the Table

mysql 5.0.51b> insert into table01 (field01,field02,field03,field04,field05) values -> (2, 'second', 'another', '1999-10-23', '10:30:00'); Query OK, 1 row affected (0.00 sec) 
!Quotes must go around text values. 
iStandard date format is "yyyy-mm-dd". iStandard time format is "hh:mm:ss". !Quotes are required around the standard date and time formats, noted above. iDates may also be entered as "yyyymmdd" and times as "hhmmss". If entered in this format, values don't need to be quoted. 
Numeric values do not need to be quoted. This holds true regardless of the data type a column is formatted to contain (e.g. text, date, time, integer).
MySQL has a useful command buffer. The buffer stores the SQL statements you've entered thus far. Using it keeps you from having to retype the same commands over and over. Let's use this next step as an example. Add another record using the command buffer (and optional date and time formats)
  • Hit the up arrow key twice.
  • Hit the ENTER key.
  • Type in the new values between a pair parentheses and stick a closing semicolon on the end.
(3, 'a third', 'more foo for you', 19991024, 103004); 
  • Hit the ENTER key.
Voilà! Is it in there? mysql 5.0.51b> select * from table01;  +---------+-----------+------------------+------------+----------+ | field01 | field02   | field03          | field04    | field05  | +---------+-----------+------------------+------------+----------+ |       1 | first     | NULL             | NULL       | NULL     | |       2 | second    | another          | 1999-10-23 | 10:30:00 | |       3 | a third   | more foo for you | 1999-10-24 | 10:30:01 | +---------+-----------+------------------+------------+----------+ 

It's in there!

Now, we're almost done... Updating Existing Records Modify one field at a time
!Again, be careful with syntax. Quote marks need to go around text but not around numbers. mysql 5.0.51b> update table01 set field03='new info' where field01=1;  Query OK, 1 row affected (0.00 sec) 


Change multiple fields at once

!Remember to put commas between each field you're updating. mysql 5.0.51b> update table01 set field04=19991022, field05=062218 where field01=1;  Query OK, 1 row affected (0.00 sec) 

mysql 5.0.51b> select * from table01; 
+---------+-----------+------------------+------------+----------+ | field01 | field02   | field03          | field04    | field05  | +---------+-----------+------------------+------------+----------+ |       1 | first     | new info         | 1999-10-22 | 06:22:18 | |       2 | second    | another          | 1999-10-23 | 10:30:00 | |       3 | third one | more foo for you | 1999-10-24 | 10:30:01 | +---------+-----------+------------------+------------+----------+ 

Update multiple records in one stroke

mysql 5.0.51b> update table01 set field05=152901 where field04>19990101; Query OK, 3 rows affected (0.00 sec) 
mysql 5.0.51b> select * from table01; 
+---------+-----------+------------------+------------+----------+ | field01 | field02   | field03          | field04    | field05  | +---------+-----------+------------------+------------+----------+ |       1 | first     | new info         | 1999-10-22 | 15:29:01 | |       2 | second    | another          | 1999-10-23 | 15:29:01 | |       3 | third one | more foo for you | 1999-10-24 | 15:29:01 | +---------+-----------+------------------+------------+----------+ 


Deleting Records

mysql 5.0.51b> delete from table01 where field01=3; Query OK, 1 row affected (0.01 sec) 
mysql 5.0.51b> select * from table01; 
+---------+---------+----------+------------+----------+ | field01 | field02 | field03  | field04    | field05  | +---------+---------+----------+------------+----------+ |       1 | first   | new info | 1999-10-22 | 15:29:01 | |       2 | second  | another  | 1999-10-23 | 15:29:01 | +---------+---------+----------+------------+----------+ 

Time to Call it Quits

mysql 5.0.51b> quit Bye 

Fungsi PHP-MySQL adalah suatu fungsi yang menjembatani antara PHP sebagai Programming web server, dan MySQL sebagai database, sehingga data-data yang terdapat
pada database MySQL dapat ditampilkan pada browser.

contoh fungsi di PHP MySQL :

  1. Koneksi dan Menutup Koneksi ke Server MySQL
    • mysql_connect
      • Membuka koneksi ke database MySQL
    • mysql_pconnect
      • Membuka koneksi ke database MySQL secara persisten
    • mysql_select_db
      • Memilih database yang akan digunakan setelah koneksi terbuka
    • mysql_close
      • Memutus koneksi database
    • mysql_change_user
      • Mengganti nama pemakai dan password pada koneksi yang sedang aktif
  2. Membuat dan Menghapus Database
    • mysql_create_db
      • Membuat sebuah database
    • mysql_drop_db
      • Menghapus sebuah database
  3. Melakukan Query
    • mysql_db_query
      • mengeksekusi perintah SQL untuk melakukan query pada server yang disebutkan
    • mysql_query
      • Mengeksekusi perintah SQL untuk melakukan query pada database yang telah dipilih
  4. Memanipulasi Hasil Query
    • mysql_fetch_array
      • Mengambil sebuah baris dari hasil query sebagai sebuah array assosiatif
    • mysql_result
      • Memberikan data hasil query
    • mysql_fetch_row
      • Menghasilkan informasi jumlah record yang diproses
    • mysql_affected_rows
      • Menghasilkan baris atau record dalam array enumerated
    • mysql_num_rows
      • Memberikan informasi jumlah record/baris
    • mysql_fetch_field
      • Menghasilkan objek informasi kolom
    • mysql_fetch_lengths
      • Menghasilkan panjang setiap output dari hasil query
    • mysql_fetch_object
      • Menghasilkan sebuah baris/row sebagai objek
    • mysql_field_name
      • Mengambil nama field
    • mysql_field_len
      • Menghasilkan panjang field
    • mysql_field_seek
      • Memposisikan pointer pada suatu field yang disebutkan
    • mysql_field_type
      • Menghasilkan type field
    • mysql_field_flags
      • Menghasilkan flag berasosiasi dengan field yang dispesifikasikan
    • mysql_insert_id
      • Menghasilkan id dari operasi insert sebelumnya
    • mysql_data_seek
      • Menggerakkan pointer hasil internal
    • mysql_free_result
      • Membersihkan/menghapus memory hasil
  5. Memanipulasi Kesalahan
    • mysql_errno
      • Menghasilkan kode/nilai error suatu perintah
    • mysql_error
      • Menghasilkan teks error

Tidak ada komentar:

Posting Komentar