Sie sind auf Seite 1von 3

Tugas Basis Data

Microsoft Windows [Version 6.3.9600]


(c) 2013 Microsoft Corporation. All rights reserved.

C:\Users\user>CD..

C:\Users>CD..

C:\>CD xampp/mysql/bin

C:\xampp\mysql\bin>mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.1.31-MariaDB mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> CREATE DATABASE basisdata;


Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]> USE basisdata;


Database changed
MariaDB [basisdata]> CREATE TABLE students (SID INT, Name VARCHAR (10), Age INT,
GPA DECIMAL (3,2));
Query OK, 0 rows affected (0.24 sec)

MariaDB [basisdata]> ALTER TABLE students ADD PRIMARY KEY (SID);


Query OK, 0 rows affected (0.80 sec)
Records: 0 Duplicates: 0 Warnings: 0

MariaDB [basisdata]> DESCRIBE students;


+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| SID | int(11) | NO | PRI | NULL | |
| Name | varchar(10) | YES | | NULL | |
| Age | int(11) | YES | | NULL | |
| GPA | decimal(3,2) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.04 sec)

MariaDB [basisdata]> INSERT INTO students VALUES ('53666','Jones','18','3.4'),('


53668','Smith','18','3.2'),('53669','Melissa','17','2.75'),('53670','Hilden','19
','2.89');
Query OK, 4 rows affected (0.13 sec)
Records: 4 Duplicates: 0 Warnings: 0

MariaDB [basisdata]> SELECT * FROM students;


+-------+---------+------+------+
| SID | Name | Age | GPA |
+-------+---------+------+------+
| 53666 | Jones | 18 | 3.40 |
| 53668 | Smith | 18 | 3.20 |
| 53669 | Melissa | 17 | 2.75 |
| 53670 | Hilden | 19 | 2.89 |
+-------+---------+------+------+
4 rows in set (0.00 sec)

MariaDB [basisdata]> CREATE TABLE consultant (CID VARCHAR (5), CName VARCHAR (10
), Stat VARCHAR (3), SID INT);
Query OK, 0 rows affected (0.27 sec)

MariaDB [basisdata]> INSERT INTO consultant VALUES ('C001','James','C1','53666')


,('C002','Schole','C1','53668'),('C003','Brown','C2','53666'),('C005','Murphy','
C2','53668'),('C006','Andre','C2','53671'),('C007','Jenny','C1','53671');
Query OK, 6 rows affected (0.20 sec)
Records: 6 Duplicates: 0 Warnings: 0

MariaDB [basisdata]> SELECT * FROM consultant;


+------+--------+------+-------+
| CID | CName | Stat | SID |
+------+--------+------+-------+
| C001 | James | C1 | 53666 |
| C002 | Schole | C1 | 53668 |
| C003 | Brown | C2 | 53666 |
| C005 | Murphy | C2 | 53668 |
| C006 | Andre | C2 | 53671 |
| C007 | Jenny | C1 | 53671 |
+------+--------+------+-------+
6 rows in set (0.00 sec)

MariaDB [basisdata]> DESCRIBE consultant;


+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| CID | varchar(5) | YES | | NULL | |
| CName | varchar(10) | YES | | NULL | |
| Stat | varchar(3) | YES | | NULL | |
| SID | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.02 sec)

MariaDB [basisdata]> SELECT a.SID, a.Name, a.Age, a.GPA, b.CID, b.CName, b.Stat
FROM students a JOIN consultant b ON a.SID=b.SID;
+-------+-------+------+------+------+--------+------+
| SID | Name | Age | GPA | CID | CName | Stat |
+-------+-------+------+------+------+--------+------+
| 53666 | Jones | 18 | 3.40 | C001 | James | C1 |
| 53666 | Jones | 18 | 3.40 | C003 | Brown | C2 |
| 53668 | Smith | 18 | 3.20 | C002 | Schole | C1 |
| 53668 | Smith | 18 | 3.20 | C005 | Murphy | C2 |
+-------+-------+------+------+------+--------+------+
4 rows in set (0.01 sec)

MariaDB [basisdata]> SELECT a.SID, a.Name, a.Age, a.GPA, b.CID, b.CName, b.Stat
FROM students a LEFT JOIN consultant b ON a.SID=b.SID;
+-------+---------+------+------+------+--------+------+
| SID | Name | Age | GPA | CID | CName | Stat |
+-------+---------+------+------+------+--------+------+
| 53666 | Jones | 18 | 3.40 | C001 | James | C1 |
| 53668 | Smith | 18 | 3.20 | C002 | Schole | C1 |
| 53666 | Jones | 18 | 3.40 | C003 | Brown | C2 |
| 53668 | Smith | 18 | 3.20 | C005 | Murphy | C2 |
| 53669 | Melissa | 17 | 2.75 | NULL | NULL | NULL |
| 53670 | Hilden | 19 | 2.89 | NULL | NULL | NULL |
+-------+---------+------+------+------+--------+------+
6 rows in set (0.01 sec)

MariaDB [basisdata]> SELECT a.SID, a.Name, a.Age, a.GPA, b.CID, b.CName, b.Stat
FROM students a RIGHT JOIN consultant b ON a.SID=b.SID;
+-------+-------+------+------+------+--------+------+
| SID | Name | Age | GPA | CID | CName | Stat |
+-------+-------+------+------+------+--------+------+
| 53666 | Jones | 18 | 3.40 | C001 | James | C1 |
| 53668 | Smith | 18 | 3.20 | C002 | Schole | C1 |
| 53666 | Jones | 18 | 3.40 | C003 | Brown | C2 |
| 53668 | Smith | 18 | 3.20 | C005 | Murphy | C2 |
| NULL | NULL | NULL | NULL | C006 | Andre | C2 |
| NULL | NULL | NULL | NULL | C007 | Jenny | C1 |
+-------+-------+------+------+------+--------+------+
6 rows in set (0.00 sec)

MariaDB [basisdata]> SELECT * FROM students LEFT JOIN consultant ON students.SID


=consultant.SID UNION SELECT * FROM students RIGHT JOIN consultant ON students.S
ID=consultant.SID;
+-------+---------+------+------+------+--------+------+-------+
| SID | Name | Age | GPA | CID | CName | Stat | SID |
+-------+---------+------+------+------+--------+------+-------+
| 53666 | Jones | 18 | 3.40 | C001 | James | C1 | 53666 |
| 53668 | Smith | 18 | 3.20 | C002 | Schole | C1 | 53668 |
| 53666 | Jones | 18 | 3.40 | C003 | Brown | C2 | 53666 |
| 53668 | Smith | 18 | 3.20 | C005 | Murphy | C2 | 53668 |
| 53669 | Melissa | 17 | 2.75 | NULL | NULL | NULL | NULL |
| 53670 | Hilden | 19 | 2.89 | NULL | NULL | NULL | NULL |
| NULL | NULL | NULL | NULL | C006 | Andre | C2 | 53671 |
| NULL | NULL | NULL | NULL | C007 | Jenny | C1 | 53671 |
+-------+---------+------+------+------+--------+------+-------+
8 rows in set (0.11 sec)

MariaDB [basisdata]>

a = students
b = consultant

Das könnte Ihnen auch gefallen