SQL (Structured Query Language) adalah sebuah bahasa yang dipergunakan untuk mengakses data dalam basis data relasional. Bahasa ini secara de facto merupakan bahasa standar yang digunakan dalam manajemen basis data relasional
Secara umum SQL terdiri dari dua bahasa
1. DDL (Data Definition Language)
DDL digunakan untuk mendefinisikan, mengubah, serta menghapus basis data dan
objek-objek yang diperlukan dalam basis data, misalnya tabel, view, user, dan
sebagainya. Secara umum, DDL yang digunakan adalah
- CREATE untuk membuat objek baru,
- USE untuk menggunakan objek,
- ALTER untuk mengubah objek yang sudah ada, dan
- DROP untuk menghapus objek.
2. DML (Data Manipulation Language)
DML digunakan untuk memanipulasi data yang ada dalam suatu tabel. Perintah yang
umum dilakukan adalah:
SELECT
untuk menampilkan dataINSERT
untuk menambahkan data baruUPDATE
untuk mengubah data yang sudah adaDELETE
untuk menghapus data
Contoh Latihan
DDL (Data Definition Language)
1. Masuk ke MySQL
C:\Documents and setting\Mahasiswa BSI> mysql
2. Buat database “NIM_Perpus” dan lihat database tersebut
Mysql>> CREATE DATABASE 12054377_perpus;
3. Buat table “anggota” dengan struktur:
1. Masuk ke MySQL
C:\Documents and setting\Mahasiswa BSI> mysql
2. Buat database “NIM_Perpus” dan lihat database tersebut
Mysql>> CREATE DATABASE 12054377_perpus;
3. Buat table “anggota” dengan struktur:
Field Name | Type | Size | Ket |
Id_anggota | Varchar | 4 | Primary key |
Nama | Varchar | 15 | |
ALamat | Varchar | 20 | |
Notelp | Varchar | 12 | |
Tgl_lahit | Varchar |
Mysql>> CREATE TABLE anggota(id_anggota varchar(4),
Nama varchar(15),
Alamat varchar(20),
Notelp varchar(12),
Tgl_lahir date, primary key(id_anggota));
a. Hapus primary key
Mysql>> ALTER TABLE anggota DROP Primary Key;
b. Tambahkan kembali primary key pada "id_anggota"
Mysql>> ALTER TABLE anggota ADD Primary Key(id_anggota);
4. Lihat field table "anggota"
Mysql>> CREATE DATABASE 12054377_perpus;
5. Ganti nama table "anggota" menjadi "anggota_baru". Lalu lihat perubahan tersebut
Mysql> RENAME TABLE anggota TO anggota_baru;
6. Ganti field "nama" dengan "nama_anggota" lalu lihat perubahannya
Mysql>> ALTER TABLE anggota_baru CHANGE nama nama_anggota VARCHAR(15);
7. Tambahkan field "tgl_anggota" dengan type varchar(15)
Mysql>> ALTER TABLE anggota_baru ADD tgl_anggota varchar(15);
8. Ubahlah type "tgl-anggota" menjadi Date
Mysql>> ALTER TABLE anggota_baru CHANGE tgl_anggota tgl_anggota Date;
9. Hapus field "tgl_anggota"
Mysql>> ALTER TABLE anggota_baru drop tgl_anggota;
10. Lihat field-field anggota_baru
Mysql>> DESCRIBE anggota_baru;
DML (Data Manipulation Language)
11. Isi record pada table tersebut dengan data anda
Contoh : 001, Razbie, Jakarta, 0218000063, 04/05/2010
Mysql>> INSERT INTO VALUES(‘001’,’Razbie’, ’Jakarta’,’0218000063’,’2010-05-04’);
12. Lihat semua isi record
Mysql>> SELECT *FROM anggota_baru;
13. Tambahkan record untuk field "id_anggota" dan "nama_anggota"
Contoh : A002, Intan
Mysql>> INSERT INTO anggota_baru(id_anggota,nama_anggota)
VALUES(‘A002’,’Intan’);
14. Lihat record dari table "anggota" dimana "id_anggota" 001
Mysql>> SELECT *FROM anggota_baru WHERE id_anggota=’001’;
15. Lakukan perubahan untuk "id_anggota" 001 menjadi "A001"
Mysql>> UPDATE anggota_baru SET id_anggota=’A001’ WHERE id_anggota=’001’;
16. Hapus record dimana “id_anggota” A002
Mysql>> DELETE anggota_baru WHERE id_anggota=’A002’;
Basic SQL statements: DDL and DML
In the first part of this tutorial, you’ve seen some of the SQL statements that you need to start building a database. This page gives you a review of those and adds several more that you haven’t seen yet.
• SQL statements are divided into two major categories: data definition language (DDL) and data manipulation language (DML). Both of these categories contain far more statements than we can present here, and each of the statements is far more complex than we show in this introduction. If you want to master this material, we strongly recommend that you find a SQL reference for your own database software as a supplement to these pages.
Data definition language
DDL statements are used to build and modify the structure of your tables and other objects in the database. When you execute a DDL statement, it takes effect immediately.
• The create table statement does exactly that:
CREATE TABLE <table name> (
<attribute name 1> <data type 1>,
...
<attribute name n> <data type n>);
• The alter table statement may be used as you have seen to specify primary and foreign key constraints, as well as to make other modifications to the table structure. Key constraints may also be specified in the CREATE TABLE statement.
ALTER TABLE <table name>
ADD CONSTRAINT <constraint name> PRIMARY KEY (<attribute list>);
• The foreign key constraint is a bit more complicated, since we have to specify both the FK attributes in this (child) table, and the PK attributes that they link to in the parent table.
ALTER TABLE <table name>
ADD CONSTRAINT <constraint name> FOREIGN KEY (<attribute list>)
REFERENCES <parent table name> (<attribute list>);
• If you totally mess things up and want to start over, you can always get rid of any object you’ve created with a drop statement. The syntax is different for tables and constraints.
DROP TABLE <table name>;
ALTER TABLE <table name>
DROP CONSTRAINT <constraint name>;
• All of the information about objects in your schema is contained, not surprisingly, in a set of tables that is called the data dictionary. There are hundreds of these tables most database systems, but all of them will allow you to see information about your own tables, in many cases with a graphical interface. How you do this is entirely system-dependent.
Data manipulation language
DML statements are used to work with the data in tables. When you are connected to most multi-user databases (whether in a client program or by a connection from a Web page script), you are in effect working with a private copy of your tables that can’t be seen by anyone else until you are finished (or tell the system that you are finished). You have already seen the SELECT statement; it is considered to be part of DML even though it just retreives data rather than modifying it.
• The insert statement is used, obviously, to add new rows to a table.
INSERT INTO <table name>
VALUES (<value 1>, ... <value n>);
• The update statement is used to change values that are already in a table.
UPDATE <table name>
SET <attribute> = <expression>
WHERE <condition>;
• The delete statement does just that, for rows in a table.
DELETE FROM <table name>
WHERE <condition>;
• If you are using a large multi-user system, you may need to make your DML changes visible to the rest of the users of the database. Although this might be done automatically when you log out, you could also just type:
COMMIT;
• If you’ve messed up your changes in this type of system, and want to restore your private copy of the database to the way it was before you started (this only works if you haven’t already typed COMMIT), just type:
ROLLBACK;
Privileges
If you want anyone else to be able to view or manipulate the data in your tables, and if your system permits this, you will have to explicitly grant the appropriate privilege or privileges (select, insert, update, or delete) to them. This has to be done for each table. The most common case where you would use grants is for tables that you want to make available to scripts running on a Web server, for example:
GRANT select, insert ON customers TO webuser;
Tidak ada komentar:
Posting Komentar