0% found this document useful (0 votes)
48 views

Membuat Database MySQL - Database Myshop

1. The document describes how to create a database called "myshop" using MySQL and create 3 tables - "users", "items", and "categories". 2. Data is inserted into the tables, including user accounts, product items belonging to categories. 3. Queries are performed to select, join, and update data from the tables.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Membuat Database MySQL - Database Myshop

1. The document describes how to create a database called "myshop" using MySQL and create 3 tables - "users", "items", and "categories". 2. Data is inserted into the tables, including user accounts, product items belonging to categories. 3. Queries are performed to select, join, and update data from the tables.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Dadan (dadannazril@gmail.

com)

1. Membuat Database

C:\Users\Jembar.id>cd C:\xampp\mysql\bin
C:\xampp\mysql\bin>mysql -u root
MariaDB [(none)]> create database myshop
MariaDB [(none)]> use myshop

2. Membuat table didalam Database

create table users(


-> id int(10) auto_increment,
-> name varchar(255),
-> email varchar(255),
-> pasword varchar(255),
-> primary key(id)
-> );

create table items(


-> id int(10) auto_increment,
-> name varchar(255),
-> description varchar(255),
-> price int(10),
-> stock int(10),
-> primary key(id),
-> category_id int(10),
-> foreign key (category_id) references users(id)
-> );

create table categories(


-> id int(10) auto_increment,
-> name varchar(255),
-> primary key (id)
-> );

show tables
-> ;
+------------------+
| Tables_in_myshop |
+------------------+
| categories |
| items |
| users |
+------------------+

desc users
-> ;
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| email | varchar(255) | YES | | NULL | |
| pasword | varchar(255) | YES | | NULL | |
+---------+--------------+------+-----+---------+----------------+

desc items
-> ;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| description | varchar(255) | YES | | NULL | |
| price | int(10) | YES | | NULL | |
| stock | int(10) | YES | | NULL | |
| category_id | int(10) | YES | MUL | NULL | |
+-------------+--------------+------+-----+---------+----------------+

desc categories
-> ;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+

3. Memasukan data pada table

MariaDB [myshop]> insert into users(name,email,pasword) values ("John


Doe","[email protected]","john123"),("John Doe","[email protected]","jenita123");
Query OK, 2 rows affected (0.128 sec)
Records: 2 Duplicates: 0 Warnings: 0

MariaDB [myshop]> select * from users


-> ;
+----+----------+--------------+-----------+
| id | name | email | pasword |
+----+----------+--------------+-----------+
| 1 | John Doe | [email protected] | john123 |
| 2 | John Doe | [email protected] | jenita123 |
+----+----------+--------------+-----------+
2 rows in set (0.075 sec)

MariaDB [myshop]> insert into categories (name) values ("gadget"),("cloth"),


("men"),("women"),("branded");
Query OK, 5 rows affected (0.091 sec)
Records: 5 Duplicates: 0 Warnings: 0

MariaDB [myshop]> insert into items (name,description,price,stock,category_id)


values
-> ("Sumsang b50","hape keren dari merek sumsang",4000000,100,1),
-> ("Uniklooh","baju keren dari brand ternama",500000,50,2),
-> ("IMHO Watch","jam tangan anak yang jujur banget",2000000,10,1);
Query OK, 3 rows affected (0.114 sec)
Records: 3 Duplicates: 0 Warnings: 0

MariaDB [myshop]> select * from items


-> ;
+----+-------------+-----------------------------------+---------+-------
+-------------+
| id | name | description | price | stock |
category_id |
+----+-------------+-----------------------------------+---------+-------
+-------------+
| 1 | Sumsang b50 | hape keren dari merek sumsang | 4000000 | 100 |
1 |
| 2 | Uniklooh | baju keren dari brand ternama | 500000 | 50 |
2 |
| 3 | IMHO Watch | jam tangan anak yang jujur banget | 2000000 | 10 |
1 |
+----+-------------+-----------------------------------+---------+-------
+-------------+
3 rows in set (0.000 sec)

MariaDB [myshop]> select * from categories


-> ;
+----+---------+
| id | name |
+----+---------+
| 1 | gadget |
| 2 | cloth |
| 3 | men |
| 4 | women |
| 5 | branded |
+----+---------+
5 rows in set (0.000 sec)

5. Mengambil data dari Database


a. Mengambil data users

MariaDB [myshop]> select id,name,email from users;


+----+----------+--------------+
| id | name | email |
+----+----------+--------------+
| 1 | John Doe | [email protected] |
| 2 | John Doe | [email protected] |
+----+----------+--------------+
2 rows in set (0.023 sec)

b. Mengambil data items

MariaDB [myshop]> select * from items where price > 1000000;


+----+-------------+-----------------------------------+---------+-------
+-------------+
| id | name | description | price | stock |
category_id |
+----+-------------+-----------------------------------+---------+-------
+-------------+
| 1 | Sumsang b50 | hape keren dari merek sumsang | 4000000 | 100 |
1 |
| 3 | IMHO Watch | jam tangan anak yang jujur banget | 2000000 | 10 |
1 |
+----+-------------+-----------------------------------+---------+-------
+-------------+
2 rows in set (0.000 sec)

MariaDB [myshop]> select * from items where name like '%sang%';


+----+-------------+-------------------------------+---------+-------+-------------
+
| id | name | description | price | stock | category_id
|
+----+-------------+-------------------------------+---------+-------+-------------
+
| 1 | Sumsang b50 | hape keren dari merek sumsang | 4000000 | 100 | 1
|
+----+-------------+-------------------------------+---------+-------+-------------
+
1 row in set (0.000 sec)

c. Menampilkan data items join dari kategori

MariaDB [myshop]> select * from items inner join categories on


items.category_id=categories.id;
+----+-------------+-----------------------------------+---------+-------
+-------------+----+--------+
| id | name | description | price | stock |
category_id | id | name |
+----+-------------+-----------------------------------+---------+-------
+-------------+----+--------+
| 1 | Sumsang b50 | hape keren dari merek sumsang | 4000000 | 100 |
1 | 1 | gadget |
| 2 | Uniklooh | baju keren dari brand ternama | 500000 | 50 |
2 | 2 | cloth |
| 3 | IMHO Watch | jam tangan anak yang jujur banget | 2000000 | 10 |
1 | 1 | gadget |
+----+-------------+-----------------------------------+---------+-------
+-------------+----+--------+
3 rows in set (0.105 sec)

5. Mengubah data dari Database

MariaDB [myshop]> update items set price = 2500000 where id = 1;


Query OK, 1 row affected (0.180 sec)
Rows matched: 1 Changed: 1 Warnings: 0

MariaDB [myshop]> select * from items;


+----+-------------+-----------------------------------+---------+-------
+-------------+
| id | name | description | price | stock |
category_id |
+----+-------------+-----------------------------------+---------+-------
+-------------+
| 1 | Sumsang b50 | hape keren dari merek sumsang | 2500000 | 100 |
1 |
| 2 | Uniklooh | baju keren dari brand ternama | 500000 | 50 |
2 |
| 3 | IMHO Watch | jam tangan anak yang jujur banget | 2000000 | 10 |
1 |
+----+-------------+-----------------------------------+---------+-------
+-------------+
3 rows in set (0.000 sec)

You might also like