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

Code Mysql

Uploaded by

Rizki dwi novita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Code Mysql

Uploaded by

Rizki dwi novita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Microsoft Windows [Version 10.0.19045.

2251]
(c) Microsoft Corporation. All rights reserved.

C:\Users\WINDOWS 10>mysql -u root -p


Enter password: ***********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 44
Server version: 5.7.40-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its


affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show engines;


+--------------------+---------+----------------------------------------------------------------+--------------
+------+------------+
| Engine | Support | Comment | Transactions | XA |
Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------
+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys |
YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO
| NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables |
NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) |
NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO
| NO |
| CSV | YES | CSV storage engine | NO | NO | NO
|
| ARCHIVE | YES | Archive storage engine | NO | NO |
NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO
| NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL
| NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------
+------+------------+
9 rows in set (0.01 sec)

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| classicmodels |
| mysql |
| performance_schema |
| sakila |
| shop_app |
| sys |
| world |
+--------------------+
8 rows in set (0.01 sec)
mysql> create database belajar_mysql;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| belajar_mysql |
| classicmodels |
| mysql |
| performance_schema |
| sakila |
| shop_app |
| sys |
| world |
+--------------------+
9 rows in set (0.00 sec)

mysql> use belajar_mysql;


Database changed
mysql> create table barang (
-> id INT,
-> nama VARCHAR(100),
-> harga INT,
-> jumlah INT
-> )engine = InnoDB;
Query OK, 0 rows affected (0.07 sec)
mysql> show tables;
+-------------------------+
| Tables_in_belajar_mysql |
+-------------------------+
| barang |
+-------------------------+
1 row in set (0.00 sec)

mysql> describe barang;


+--------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| nama | varchar(100) | YES | | NULL | |
| harga | int(11) | YES | | NULL | |
| jumlah | int(11) | YES | | NULL | |
+--------+--------------+------+-----+---------+-------+
4 rows in set (0.02 sec)

mysql> show create table barang;


+--------
+-------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------+
| Table | Create Table
|
+--------
+-------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------+
| barang | CREATE TABLE `barang` (
`id` int(11) DEFAULT NULL,
`nama` varchar(100) DEFAULT NULL,
`harga` int(11) DEFAULT NULL,
`jumlah` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+--------
+-------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> alter table barang


-> add column deskripsi text;
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> describe barang;


+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| nama | varchar(100) | YES | | NULL | |
| harga | int(11) | YES | | NULL | |
| jumlah | int(11) | YES | | NULL | |
| deskripsi | text | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
5 rows in set (0.01 sec)

mysql> alter table barang


-> add column salah text;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc barang;


+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| nama | varchar(100) | YES | | NULL | |
| harga | int(11) | YES | | NULL | |
| jumlah | int(11) | YES | | NULL | |
| deskripsi | text | YES | | NULL | |
| salah | text | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

mysql> alter table barang


-> drop column salah;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc barabg;


ERROR 1146 (42S02): Table 'belajar_mysql.barabg' doesn't exist
mysql> desc barang;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| nama | varchar(100) | YES | | NULL | |
| harga | int(11) | YES | | NULL | |
| jumlah | int(11) | YES | | NULL | |
| deskripsi | text | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> alter table barang


-> modify nama varchar(200)after deskripsi;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc barang;


+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| harga | int(11) | YES | | NULL | |
| jumlah | int(11) | YES | | NULL | |
| deskripsi | text | YES | | NULL | |
| nama | varchar(200) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> alter table barang


-> modify nama varchar(200) firts;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'firts' at line 2
mysql> alter table barang
-> modify nama varchar(200)first;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc barang;


+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| nama | varchar(200) | YES | | NULL | |
| id | int(11) | YES | | NULL | |
| harga | int(11) | YES | | NULL | |
| jumlah | int(11) | YES | | NULL | |
| deskripsi | text | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> alter table barang


-> modify id int not null;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc barang;


+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| nama | varchar(200) | YES | | NULL | |
| id | int(11) | NO | | NULL | |
| harga | int(11) | YES | | NULL | |
| jumlah | int(11) | YES | | NULL | |
| deskripsi | text | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> alter table barang


-> modify nama varchar(200)not null;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc barang;


+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| nama | varchar(200) | NO | | NULL | |
| id | int(11) | NO | | NULL | |
| harga | int(11) | YES | | NULL | |
| jumlah | int(11) | YES | | NULL | |
| deskripsi | text | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> show create table barang;


+--------
+-------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------+
| Table | Create Table
|
+--------
+-------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------+
| barang | CREATE TABLE `barang` (
`nama` varchar(200) NOT NULL,
`id` int(11) NOT NULL,
`harga` int(11) DEFAULT NULL,
`jumlah` int(11) DEFAULT NULL,
`deskripsi` text
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+--------
+-------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> desc barang;


+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| nama | varchar(200) | NO | | NULL | |
| id | int(11) | NO | | NULL | |
| harga | int(11) | YES | | NULL | |
| jumlah | int(11) | YES | | NULL | |
| deskripsi | text | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> alter table barang


-> modify harga,jumlah int not null default 0;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'jumlah int not null
default 0' at line 2
mysql> alter table barang
-> modify harga int not null default 0;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table barang


-> modify jumlah int not null default 0;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc barang;


+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| nama | varchar(200) | NO | | NULL | |
| id | int(11) | NO | | NULL | |
| harga | int(11) | NO | |0 | |
| jumlah | int(11) | NO | |0 | |
| deskripsi | text | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> alter table barang


-> add waktu dibuat timestamp not null default current_timestamp;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'dibuat timestamp not
null default current_timestamp' at line 2
mysql> alter table barang
-> add waktu_dibuat timestamp not null default current_timestamp;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc barang;


+--------------+--------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+-------------------+-------+
| nama | varchar(200) | NO | | NULL | |
| id | int(11) | NO | | NULL | |
| harga | int(11) | NO | |0 | |
| jumlah | int(11) | NO | |0 | |
| deskripsi | text | YES | | NULL | |
| waktu_dibuat | timestamp | NO | | CURRENT_TIMESTAMP | |
+--------------+--------------+------+-----+-------------------+-------+
6 rows in set (0.00 sec)

mysql> insert into barang (id, nama) values(1,"Apel");


Query OK, 1 row affected (0.01 sec)

mysql> select * from barang;


+------+----+-------+--------+-----------+---------------------+
| nama | id | harga | jumlah | deskripsi | waktu_dibuat |
+------+----+-------+--------+-----------+---------------------+
| Apel | 1 | 0| 0 | NULL | 2023-01-18 21:04:12 |
+------+----+-------+--------+-----------+---------------------+
1 row in set (0.00 sec)
mysql> create table barang (
-> -> id INT,
-> -> nama VARCHAR(100),
-> -> harga INT,
-> -> jumlah INT
->
-> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '-> id INT,
-> nama VARCHAR(100),
-> harga INT,
-> jumlah INT' at line 2
mysql> CREATE TABLE products(
-> id VARCHAR (100) NOT NULL,
-> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '' at line 2
mysql> CREATE TABLE products(
-> id VARCHAR(10) NOT NULL,
-> name VARCHAR(100) NOT NULL,
-> description TEXT,
-> price INT UNSIGNED NOT NULL,
-> quantity INT UNSIGNED NOT NULL DEFAULT 0,
-> created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
-> )ENGINE =InnoDB;
Query OK, 0 rows affected (0.03 sec)

mysql> SHOW TABLES;


+-------------------------+
| Tables_in_belajar_mysql |
+-------------------------+
| barang |
| products |
+-------------------------+
2 rows in set (0.00 sec)

mysql> DESC products;


+-------------+------------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+-------------------+-------+
| id | varchar(10) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| description | text | YES | | NULL | |
| price | int(10) unsigned | NO | | NULL | |
| quantity | int(10) unsigned | NO | |0 | |
| created_at | timestamp | NO | | CURRENT_TIMESTAMP | |
+-------------+------------------+------+-----+-------------------+-------+
6 rows in set (0.01 sec)

mysql> INSERT INTO products(id,name,price,quantity)


-> VALUES ("P001","Mie Ayam Original",1500,100);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM products;


+------+-------------------+-------------+-------+----------+---------------------+
| id | name | description | price | quantity | created_at |
+------+-------------------+-------------+-------+----------+---------------------+
| P001 | Mie Ayam Original | NULL | 1500 | 100 | 2023-01-18 21:33:58 |
+------+-------------------+-------------+-------+----------+---------------------+
1 row in set (0.00 sec)

mysql> INSERT INTO products(id,name,description,price,quantity)


-> VALUES("P002", "Mie Ayam Bakso","Mie Ayam Original+Bakso",15000,100);
Query OK, 1 row affected (0.01 sec)

mysql> SELECT * FROM products;


+------+-------------------+-------------------------+-------+----------+---------------------+
| id | name | description | price | quantity | created_at |
+------+-------------------+-------------------------+-------+----------+---------------------+
| P001 | Mie Ayam Original | NULL | 1500 | 100 | 2023-01-18 21:33:58 |
| P002 | Mie Ayam Bakso | Mie Ayam Original+Bakso | 15000 | 100 | 2023-01-18 21:35:54 |
+------+-------------------+-------------------------+-------+----------+---------------------+
2 rows in set (0.00 sec)

mysql> INSERT INTO products(id,name,price,quantity)


-> VALUES("P003", "Mie Ayam Bakso","Mie Ayam Original+Bakso",15000,100),
-> ("P004", "Mie Ayam Bakso","Mie Ayam Original+Bakso",15000,100),
-> ("P005", "Mie Ayam Bakso","Mie Ayam Original+Bakso",15000,100);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> ALTER TABLE products
-> ADD PRIMARY KEY(id);
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM products;
+------+-------------------+-------------------------+-------+----------+---------------------+
| id | name | description | price | quantity | created_at |
+------+-------------------+-------------------------+-------+----------+---------------------+
| P001 | Mie Ayam Original | NULL | 1500 | 100 | 2023-01-18 21:33:58 |
| P002 | Mie Ayam Bakso | Mie Ayam Original+Bakso | 15000 | 100 | 2023-01-18 21:35:54 |
+------+-------------------+-------------------------+-------+----------+---------------------+
2 rows in set (0.00 sec)

mysql> DESC products;


+-------------+------------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+-------------------+-------+
| id | varchar(10) | NO | PRI | NULL | |
| name | varchar(100) | NO | | NULL | |
| description | text | YES | | NULL | |
| price | int(10) unsigned | NO | | NULL | |
| quantity | int(10) unsigned | NO | |0 | |
| created_at | timestamp | NO | | CURRENT_TIMESTAMP | |
+-------------+------------------+------+-----+-------------------+-------+
6 rows in set (0.00 sec)

mysql> SHOW CREATE TABLE products;


+----------
+-------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------+
| Table | Create Table
|
+----------
+-------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------+
| products | CREATE TABLE `products` (
`id` varchar(10) NOT NULL,
`name` varchar(100) NOT NULL,
`description` text,
`price` int(10) unsigned NOT NULL,
`quantity` int(10) unsigned NOT NULL DEFAULT '0',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+----------
+-------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT * FROM products


-> WHERE quantity =0;
Empty set (0.01 sec)

mysql> SELECT * FROM products


-> WHERE quantity =100;
+------+-------------------+-------------------------+-------+----------+---------------------+
| id | name | description | price | quantity | created_at |
+------+-------------------+-------------------------+-------+----------+---------------------+
| P001 | Mie Ayam Original | NULL | 1500 | 100 | 2023-01-18 21:33:58 |
| P002 | Mie Ayam Bakso | Mie Ayam Original+Bakso | 15000 | 100 | 2023-01-18 21:35:54 |
+------+-------------------+-------------------------+-------+----------+---------------------+
2 rows in set (0.00 sec)

mysql> DESC products;


+-------------+------------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+-------------------+-------+
| id | varchar(10) | NO | PRI | NULL | |
| name | varchar(100) | NO | | NULL | |
| description | text | YES | | NULL | |
| price | int(10) unsigned | NO | | NULL | |
| quantity | int(10) unsigned | NO | |0 | |
| created_at | timestamp | NO | | CURRENT_TIMESTAMP | |
+-------------+------------------+------+-----+-------------------+-------+
6 rows in set (0.00 sec)

mysql> ALTER TABLE products


-> ADD COLUMN category ENUM ("Makanan","Minuman","Lain-lain")
-> AFTER name;
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM products;


+------+-------------------+----------+-------------------------+-------+----------+---------------------+
| id | name | category | description | price | quantity | created_at |
+------+-------------------+----------+-------------------------+-------+----------+---------------------+
| P001 | Mie Ayam Original | NULL | NULL | 1500 | 100 | 2023-01-18
21:33:58 |
| P002 | Mie Ayam Bakso | NULL | Mie Ayam Original+Bakso | 15000 | 100 | 2023-01-
18 21:35:54 |
+------+-------------------+----------+-------------------------+-------+----------+---------------------+
2 rows in set (0.01 sec)

mysql> UPDATE products


-> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '' at line 1
mysql> SELECT * FROM products
-> WHERE name="P001";
Empty set (0.01 sec)

mysql> UPDATE products


-> SET category="Makanan"
-> WHERE id="P001";
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> SELECT * FROM products;


+------+-------------------+----------+-------------------------+-------+----------+---------------------+
| id | name | category | description | price | quantity | created_at |
+------+-------------------+----------+-------------------------+-------+----------+---------------------+
| P001 | Mie Ayam Original | Makanan | NULL | 1500 | 100 | 2023-01-18
21:33:58 |
| P002 | Mie Ayam Bakso | NULL | Mie Ayam Original+Bakso | 15000 | 100 | 2023-01-
18 21:35:54 |
+------+-------------------+----------+-------------------------+-------+----------+---------------------+
2 rows in set (0.00 sec)
mysql>

You might also like