Menambah Kolom pada Tabel
Kita juga bisa mengatur
letak kolom yang akan kita buat
Modifikasi Nama Tabel
Modifikasi Definisi Tabel
Mengganti Nama Kolom
Menghapus Kolom
Menghapus & Menambah Primary Key
Dalam menambah kolom
dalam tabel kita bisa menggunakan perintah alter table, contohnya kita akan
menambahkan kolom pada tabel t_mobil :
mysql> alter table
t_mobil add
harga bigint(11);
Query OK, 2 rows affected
(0.16 sec)
Records: 2 Duplicates: 0
Warnings: 0
mysql> desc t_mobil;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id_mobil |
varchar(8) | NO | PRI | NULL |
|
| type | varchar(20) | YES | |
NULL | |
| harga | bigint(11) | YES
| | NULL |
|
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> alter table
t_mobil add
series varchar(8) after type;
Query OK, 2 rows affected
(0.12 sec)
Records: 2 Duplicates: 0
Warnings: 0
mysql> desc t_mobil;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id_mobil |
varchar(8) | NO | PRI | NULL |
|
| type | varchar(20) | YES | |
NULL | |
| series | varchar(8)
| YES | | NULL
| |
| harga | bigint(11) | YES
| | NULL |
|
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)Modifikasi Nama Tabel
Dalam memodifikasi
definisi tabel contohnya jika kita ingin mengganti definisi dari t_mobil maka
caranya :
mysql> alter table
t_mobil rename
to t_car;
Query OK, 0 rows affected
(0.07 sec)
mysql> desc t_car;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id_mobil |
varchar(8) | NO | PRI | NULL |
|
| type | varchar(20) | YES | |
NULL | |
| series | varchar(8) | YES | |
NULL | |
| harga | bigint(11) | YES
| | NULL |
|
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.04 sec)Modifikasi Definisi Tabel
Dalam memodifikasi
definisi tabel contohnya jika kita ingin mengganti definisi dari t_car maka
caranya :
mysql> alter table
t_car modify
type varchar(15), modify series varchar(10);
Query OK, 2 rows affected
(0.14 sec)
Records: 2 Duplicates: 0
Warnings: 0
mysql> desc t_car;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id_mobil |
varchar(8) | NO | PRI | NULL |
|
| type | varchar(15) | YES | |
NULL | |
| series | varchar(10) | YES | |
NULL | |
| harga | bigint(11) | YES
| | NULL |
|
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)Mengganti Nama Kolom
Dalam memodifikasi nama
kolom contohnya jika kita ingin mengganti nama kolom dari t_car maka caranya :
mysql>
alter table t_car change
harga harga_mobil bigint(11);
Query OK, 2 rows affected
(0.14 sec)
Records: 2 Duplicates: 0
Warnings: 0
mysql> desc t_car;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| id_mobil | varchar(8) | NO
| PRI | NULL | |
| type | varchar(15) | YES | |
NULL | |
| series | varchar(10) | YES | |
NULL | |
| harga_mobil |
bigint(11) | YES | |
NULL | |
+-------------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)Menghapus Kolom
Cara untuk menghapus
kolom dengan perintah ALTER TABLE adalah sbb, misal kita akan menghapus salah
satu kolom di t_car, maka caranya :
mysql>
alter table t_car drop
harga_mobil;
Query OK, 2 rows affected
(0.12 sec)
Records: 2 Duplicates: 0
Warnings: 0
mysql> desc t_car;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id_mobil |
varchar(8) | NO | PRI | NULL |
|
| type | varchar(15) | YES | |
NULL | |
| series | varchar(10) | YES | |
NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)Menghapus & Menambah Primary Key
Cara untuk menghapus & menambah primary key
dengan perintah ALTER TABLE adalah sbb, misal kita akan menghapus & menambah
primary key di suatu table, maka caranya :
Hapus primary key
mysql> alter table t_pegawai drop primary key;
Query OK, 0 rows affected (0.17 sec)
Records: 0
Duplicates: 0 Warnings: 0
mysql> desc t_pegawai;
+------------+-------------+------+-----+---------+-------+
|
Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
|
id_peg | varchar(8) | NO
| | NULL |
|
|
nama_peg | varchar(50) | YES | |
NULL | |
|
alamat_peg | varchar(50) | YES | | NULL
| |
+------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
Tambah
primary key
mysql>
alter table
t_pegawai add
primary key (id_peg);
Query
OK, 0 rows affected (0.53 sec)
Records:
0 Duplicates: 0 Warnings: 0
mysql> desc t_pegawai;
+------------+-------------+------+-----+---------+-------+
| Field
| Type | Null | Key |
Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id_peg
| varchar(8) | NO | PRI | NULL |
|
| nama_peg
| varchar(50) | YES | | NULL
| |
| alamat_peg | varchar(50) | YES | |
NULL | |
+------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
0 komentar :
Posting Komentar