PRAKTIKUM 3 PEMROGRAMAN BASIS DATA
Menghapus baris Deleting rows- DELETE FROM Use the DELELE FROM command to delete row(s) from a table, with the following syntax: -- Delete all rows from the table. Use with extreme care! Records are NOT recoverable!!! DELETE FROM tableName -- Delete only row(s) that meets the criteria DELETE FROM tableName WHERE criteria
contoh mysql> DELETE FROM products WHERE name LIKE 'Pencil%'; mysql> SELECT * FROM products;
Beware that "DELETE FROM tableName" without a WHERE clause deletes ALL records from the table. Even with a WHERE clause, you might have deleted some records unintentionally. It is always advisable to issue a SELECT command with the same WHERE clause to check the result set before issuing the DELETE (and UPDATE).
Loading/exporting data from/to a text file There are several ways to add data into the database: (a) manually issue the INSERT commands; (b) run the INSERT commands from a script; (c) load raw data from a file using LOAD DATA or via mysqlimport utility.
LOAD DATA LOCAL INFILE... INTO TABLE... Besides using INSERT commands to insert rows, you could keep your raw data in a text file, and load them into the table via the LOAD DATA command. For example, create the following text file called "produk_in.csv", where the values are separated by ','. The file extension of ".csv" stands for Comma-Separated Values text file. \N,PENC,Pensil 3B,500,3000 \N,PENC,Pensil 4B,300,3500 \N,PENC,Pensil 5B,800,3750 \N,PENC,Pensil 6B,400,4000
mysql> LOAD DATA LOCAL INFILE 'd:/produk_in.csv' INTO TABLE produk COLUMNS TERMINATED BY ',' LINES TERMINATED BY '\r\n';
Notes: You need to provide the path (absolute or relative) and the filename. Use Unix-style forward-slash '/' as the directory separator, instead of Windows-style back-slash '\'. The default line delimiter (or end-of-line) is '\n' (Unix-style). If the text file is prepared in Windows, you need to include LINES TERMINATED BY '\r\n'. For Mac, use LINES TERMINATED BY '\r'. The default column delimiter is "tab" (in a so-called TSV file - Tab-Separated Values). If you use another delimiter, e.g. ',', include COLUMNS TERMINATED BY ','. You must use \N (back-slash + uppercase 'N') for NULL.
Select …into outfile … Lihat hasil file yang dibuat pada d:/…
More than one tables
One to many relationship Masing-masing produk memiliki satu suplier, dan setiap suplier menyuplai minimal nol atau lebih produk. Buat tabel suplier dengan atribut id_supl(int), nama_supl (varchar(3)), dan telp (char(8)) Id_supl (int)Nama_supl(varchar)Phone (char(8)) 501Perusahaan ABC Perusahaan XYZ Perusahaan KLM
Tabel Id_produkKode_produkNama_produkJumlahHargaId_supl 1001PENPulpen merah PENPulpen Hitam PENPulpen biru PENCPensil 6B PENCPensil 5B PENCPensil 4B PENCPensil 3B
Buat tabel suplier
Masukkan data suplier
Buat kolom id_suplier pada tabel produk
Tambahkan id_suplier sebagai foreign key pada tabel produk
Ganti id supl pada masing-masing produk
Select with Join SELECT command can be used to query and join data from two related tables. For example, to list the product's name (in products table) and supplier's name (in suppliers table), we could join the two table via the two common supplierID columns
Join via where clause (legacy and not recommended)
Gunakan alias ‘AS’ untuk menampilkan nama kolom
Many to Many Setiap produk dapat disuplai oleh banyak suplier dan setiap suplier dapat menyuplai banyak produk. Buat sebuah tabel baru, sebagai tabel penghubung (junction/join tabel) yang diberi nama tabel suplier_produk.
Tabel suplier_produk Id_produk (foreign key)Id_supl (foreign key)
Masukkan nilai untuk suplier_produk
Hapus kolom id_supl pada tabel produk, karena ini hanya berlaku pada one to many relationship. Sebelum menghapus kolom id_supl, kita hilangkan dulu foreign key yang ada pada column tersebut. Karena key merupakan constraint yang diberikan oleh sistem, maka kita masuk ke sistem databasenya.
One to one relationship
Backup The syntax for the mysqldump program is as follows: -- Dump selected databases with --databases option > mysqldump -u username -p --databases database1Name [database2Name...] > backupFile.sql -- Dump all databases in the server with --all-databases option, except mysql.user table (for security) > mysqldump -u root -p --all-databases --ignore-table=mysql.user > backupServer.sql -- Dump all the tables of a particular database > mysqldump -u username -p databaseName > backupFile.sql -- Dump selected tables of a particular database > mysqldump - u username -p databaseName table1Name [table2Name...] > backupFile.sql
Backup Keluar dulu ke bin Lihat hasilny pada d:/projek