Home » , , , » Belajar coding mysql membuat database dan tabel

Belajar coding mysql membuat database dan tabel




mysql> create database sispen;
Query OK, 1 row affected (0.01 sec)

mysql> use sispen
Database changed
mysql> create table kecamatan (kodekec char(2) not null, namakec char(40) not null);
Query OK, 0 rows affected (0.12 sec)

mysql> create table desa (kodekec char(2) not null, kodedes char(2) not null, namades char(40) not null);
Query OK, 0 rows affected (0.06 sec)

mysql> insert into kecamatan values(01,'Pidie');
Query OK, 1 row affected (0.05 sec)

mysql> insert into kecamatan values(02,'Simpang Tiga');
Query OK, 1 row affected (0.02 sec)

mysql> insert into desa values(02,01,'curucok');
Query OK, 1 row affected (0.02 sec)

mysql> insert into desa values(01,01,'Kunire');
Query OK, 1 row affected (0.01 sec)

mysql> insert into desa values(01,02,'Perumanas Rawa');
Query OK, 1 row affected (0.02 sec)

mysql> select * from desa;
+---------+---------+----------------+
| kodekec | kodedes | namades        |
+---------+---------+----------------+
| 2       | 1       | curucok        |
| 1       | 1       | Kunire         |
| 1       | 2       | Perumanas Rawa |
+---------+---------+----------------+
3 rows in set (0.02 sec)

mysql> select * from kecamatan;
+---------+--------------+
| kodekec | namakec      |
+---------+--------------+
| 1       | Pidie        |
| 2       | Simpang Tiga |
+---------+--------------+
2 rows in set (0.00 sec)

mysql> select * from kecamatan where kodedes=01;
ERROR 1054 (42S22): Unknown column 'kodedes' in 'where clause'
mysql> select * from kecamatan where kodekec=01;
+---------+---------+
| kodekec | namakec |
+---------+---------+
| 1       | Pidie   |
+---------+---------+
1 row in set (0.02 sec)

mysql> select * from desa where kodekec=01;
+---------+---------+----------------+
| kodekec | kodedes | namades        |
+---------+---------+----------------+
| 1       | 1       | Kunire         |
| 1       | 2       | Perumanas Rawa |
+---------+---------+----------------+
2 rows in set (0.02 sec)

mysql> select kecamatan.kodekec,kecamatan.namakec, a.* from kecamatan, desa a where kecamatan.kodekec=a.kodekec;
+---------+--------------+---------+---------+----------------+
| kodekec | namakec      | kodekec | kodedes | namades        |
+---------+--------------+---------+---------+----------------+
| 2       | Simpang Tiga | 2       | 1       | curucok        |
| 1       | Pidie        | 1       | 1       | Kunire         |
| 1       | Pidie        | 1       | 2       | Perumanas Rawa |
+---------+--------------+---------+---------+----------------+
3 rows in set (0.00 sec)

mysql> select * from desa where kodedes=01;
+---------+---------+---------+
| kodekec | kodedes | namades |
+---------+---------+---------+
| 2       | 1       | curucok |
| 1       | 1       | Kunire  |
+---------+---------+---------+
2 rows in set (0.00 sec)

mysql> select * from desa where kodedes=01 and kodekec=01;
+---------+---------+---------+
| kodekec | kodedes | namades |
+---------+---------+---------+
| 1       | 1       | Kunire  |
+---------+---------+---------+
1 row in set (0.00 sec)

mysql> select x.* b.* from desa x, kecamatan b;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the r
ight syntax to use near 'b.* from desa x, kecamatan b' at line 1
mysql> select x.*, b.* from desa x, kecamatan b;
+---------+---------+----------------+---------+--------------+
| kodekec | kodedes | namades        | kodekec | namakec      |
+---------+---------+----------------+---------+--------------+
| 2       | 1       | curucok        | 1       | Pidie        |
| 2       | 1       | curucok        | 2       | Simpang Tiga |
| 1       | 1       | Kunire         | 1       | Pidie        |
| 1       | 1       | Kunire         | 2       | Simpang Tiga |
| 1       | 2       | Perumanas Rawa | 1       | Pidie        |
| 1       | 2       | Perumanas Rawa | 2       | Simpang Tiga |
+---------+---------+----------------+---------+--------------+
6 rows in set (0.00 sec)

mysql> desc desa;
+---------+----------+------+-----+---------+-------+
| Field   | Type     | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+-------+
| kodekec | char(2)  | NO   |     | NULL    |       |
| kodedes | char(2)  | NO   |     | NULL    |       |
| namades | char(40) | NO   |     | NULL    |       |
+---------+----------+------+-----+---------+-------+
3 rows in set (0.03 sec)

mysql> select * from desa where namades;
Empty set, 3 warnings (0.00 sec)

mysql> select namades from desa;
+----------------+
| namades        |
+----------------+
| curucok        |
| Kunire         |
| Perumanas Rawa |
+----------------+
3 rows in set (0.00 sec)

mysql> select namades from kodedes=02;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the r
ight syntax to use near '02' at line 1
mysql> select namades from desa where kodedes=02;
+----------------+
| namades        |
+----------------+
| Perumanas Rawa |
+----------------+
1 row in set (0.00 sec)

mysql> select namades from desa where kodedes=01;
+---------+
| namades |
+---------+
| curucok |
| Kunire  |
+---------+
2 rows in set (0.00 sec)

mysql> select namades from desa where kodedes=01 and kodekec=01;
+---------+
| namades |
+---------+
| Kunire  |
+---------+
1 row in set (0.00 sec)

mysql> select namades from desa where kodedes=01 and kodekec=02;
+---------+
| namades |
+---------+
| curucok |
+---------+

1 row in set (0.00 sec)

0 komentar:

Post a Comment

Ads Inside Post