0% found this document useful (0 votes)
85 views15 pages

MySQL Database and Table Management Guide

Uploaded by

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

MySQL Database and Table Management Guide

Uploaded by

ramlaldudwe813
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Enter password: ****

Welcome to the MySQL monitor. Commands end with ; or \g.


Your MySQL connection id is 17
Server version: 8.0.40 MySQL Community Server - GPL

Copyright (c) 2000, 2024, 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 databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sachin |
| sys |
+--------------------+
5 rows in set (0.00 sec)

mysql> create stu_info


-> ;
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
'stu_info' at line 1
mysql> create database stu_info;
Query OK, 1 row affected (0.00 sec)

mysql> use stu_info;


Database changed
mysql> CREATE TABLE student ( student_id VARCHAR(10) PRIMARY KEY NOT NULL, name
VARCHAR(30), marks INTEGER(5) );
Query OK, 0 rows affected, 1 warning (0.02 sec)

mysql> desc student;


+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| student_id | varchar(10) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| marks | int | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> INSERT INTO student VALUES(101, 'Rohit', 410); INSERT INTO student
VALUES(102, 'Mohit', 425); INSERT INTO student VALUES(103, 'Rahul', 475); INSERT
INTO student VALUES(104, 'Virat', 495);
Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)


mysql> select*from student;
+------------+-------+-------+
| student_id | name | marks |
+------------+-------+-------+
| 101 | Rohit | 410 |
| 102 | Mohit | 425 |
| 103 | Rahul | 475 |
| 104 | Virat | 495 |
+------------+-------+-------+
4 rows in set (0.00 sec)

mysql> DELETE FROM student WHERE name='Rahul'; SELECT * FROM student;


Query OK, 1 row affected (0.00 sec)

+------------+-------+-------+
| student_id | name | marks |
+------------+-------+-------+
| 101 | Rohit | 410 |
| 102 | Mohit | 425 |
| 104 | Virat | 495 |
+------------+-------+-------+
3 rows in set (0.00 sec)

mysql> SELECT * FROM student WHERE marks > 80;


+------------+-------+-------+
| student_id | name | marks |
+------------+-------+-------+
| 101 | Rohit | 410 |
| 102 | Mohit | 425 |
| 104 | Virat | 495 |
+------------+-------+-------+
3 rows in set (0.00 sec)

mysql> CREATE TABLE orderdetails AS SELECT [Link], [Link],


[Link] FROM orders, customer WHERE [Link] =
[Link];
ERROR 1146 (42S02): Table 'stu_info.orders' doesn't exist
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sachin |
| stu_info |
| sys |
+--------------------+
6 rows in set (0.00 sec)

mysql> create database 1;


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 '1' at
line 1
mysql> create 1;
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 '1' at
line 1
mysql> create database q;
Query OK, 1 row affected (0.00 sec)

mysql> use q;
Database changed
mysql> create table orders;
ERROR 4028 (HY000): A table must have at least one visible column.
mysql> create table orders(orderid integer(5),customerid integer(5),orderdate(20));
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 '(20))'
at line 1
mysql> create table orders(orderid integer(5),customerid integer(5),orderdate
varchar(20));
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> desc orders;


+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| orderid | int | YES | | NULL | |
| customerid | int | YES | | NULL | |
| orderdate | varchar(20) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> INSERT INTO student VALUES(1,1,2020/10/10);INSERT INTO student


VALUES(2,1,2020/04/05);INSERT INTO student VALUES(3,2,2019/01/01);
ERROR 1146 (42S02): Table '[Link]' doesn't exist
ERROR 1146 (42S02): Table '[Link]' doesn't exist
ERROR 1146 (42S02): Table '[Link]' doesn't exist
mysql> INSERT INTO orders VALUES(1,1,2020/10/10);INSERT INTO orders
VALUES(2,1,2020/04/05);INSERT INTO orders VALUES(3,2,2019/01/01);
ERROR 1406 (22001): Data too long for column 'orderdate' at row 1
ERROR 1406 (22001): Data too long for column 'orderdate' at row 1
ERROR 1406 (22001): Data too long for column 'orderdate' at row 1
mysql> drop table orders;
Query OK, 0 rows affected (0.01 sec)

mysql> create table orders(orderid integer(5),customerid integer(5),ordrdate


varchar(10));
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> INSERT INTO orders VALUES(1,1,2020/10/10);INSERT INTO orders


VALUES(2,1,2020/04/05);INSERT INTO orders VALUES(3,2,2019/01/01);
ERROR 1406 (22001): Data too long for column 'ordrdate' at row 1
ERROR 1406 (22001): Data too long for column 'ordrdate' at row 1
ERROR 1406 (22001): Data too long for column 'ordrdate' at row 1
mysql> drop table orders;
Query OK, 0 rows affected (0.01 sec)

mysql> create table orders(orderid integer(10),customerid integer(10),ordrdate


varchar(10));
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> INSERT INTO orders VALUES(1,1,2020/10/10);INSERT INTO orders


VALUES(2,1,2020/04/05);INSERT INTO orders VALUES(3,2,2019/01/01);
ERROR 1406 (22001): Data too long for column 'ordrdate' at row 1
ERROR 1406 (22001): Data too long for column 'ordrdate' at row 1
ERROR 1406 (22001): Data too long for column 'ordrdate' at row 1
mysql> drop table orders;
Query OK, 0 rows affected (0.01 sec)

mysql> create table orders(orderid varchar(5),customerid varchar(5),ordrdate


varchar(10));
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO orders VALUES(1,1,2020/10/10);INSERT INTO orders


VALUES(2,1,2020/04/05);INSERT INTO orders VALUES(3,2,2019/01/01);
ERROR 1406 (22001): Data too long for column 'ordrdate' at row 1
ERROR 1406 (22001): Data too long for column 'ordrdate' at row 1
ERROR 1406 (22001): Data too long for column 'ordrdate' at row 1
mysql> drop table orders;
Query OK, 0 rows affected (0.01 sec)

mysql> create table orders(orderid integer(5),customerid integer(5),ordrdate


varchar(20));
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> INSERT INTO orders VALUES(1,1,2020/10/10);INSERT INTO orders


VALUES(2,1,2020/04/05);INSERT INTO orders VALUES(3,2,2019/01/01);
ERROR 1406 (22001): Data too long for column 'ordrdate' at row 1
ERROR 1406 (22001): Data too long for column 'ordrdate' at row 1
ERROR 1406 (22001): Data too long for column 'ordrdate' at row 1
mysql> drop table orders;
Query OK, 0 rows affected (0.01 sec)

mysql> create table orders(orderid integer(10),customerid integer(10),ordrdate


varchar(30));
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> INSERT INTO orders VALUES(1,1,2020/10/10);INSERT INTO orders


VALUES(2,1,2020/04/05);INSERT INTO orders VALUES(3,2,2019/01/01);
Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

mysql> select*from orders;


+---------+------------+-------------------------+
| orderid | customerid | ordrdate |
+---------+------------+-------------------------+
| 1 | 1 | 20.200000000000000000 |
| 2 | 1 | 101.000000000000000000 |
| 3 | 2 | 2019.000000000000000000 |
+---------+------------+-------------------------+
3 rows in set (0.00 sec)

mysql> drop table orders;


Query OK, 0 rows affected (0.01 sec)

mysql> create table orders(orderid integer(5),customerid integer(5),ordrdate


varchar(20));
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> INSERT INTO orders VALUES(1,1,2020-10-10);INSERT INTO orders


VALUES(2,1,2020-04-05);INSERT INTO orders VALUES(3,2,2019-01-01);
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

mysql> select*from orders;


+---------+------------+----------+
| orderid | customerid | ordrdate |
+---------+------------+----------+
| 1 | 1 | 2000 |
| 2 | 1 | 2011 |
| 3 | 2 | 2017 |
+---------+------------+----------+
3 rows in set (0.00 sec)

mysql> drop table orders;


Query OK, 0 rows affected (0.01 sec)

mysql> create table orders(orderid integer(5),customerid integer(5),ordrdate


varchar(20));
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> INSERT INTO orders VALUES(1,1,10-10-2020);INSERT INTO orders VALUES(2,1,05-


04-2020);INSERT INTO orders VALUES(3,2,01-01-2019);
Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

mysql> select*from orders;


+---------+------------+----------+
| orderid | customerid | ordrdate |
+---------+------------+----------+
| 1 | 1 | -2020 |
| 2 | 1 | -2019 |
| 3 | 2 | -2019 |
+---------+------------+----------+
3 rows in set (0.00 sec)

mysql> select*from orders;


+---------+------------+----------+
| orderid | customerid | ordrdate |
+---------+------------+----------+
| 1 | 1 | -2020 |
| 2 | 1 | -2019 |
| 3 | 2 | -2019 |
+---------+------------+----------+
3 rows in set (0.00 sec)

mysql> drop table orders;


Query OK, 0 rows affected (0.01 sec)

mysql> drop database q;


Query OK, 0 rows affected (0.01 sec)

mysql> create database q;


Query OK, 1 row affected (0.00 sec)
mysql> CREATE TABLE Orders (
-> order_ID INT PRIMARY KEY,
-> customer_ID INT,
-> order_Date DATE
-> );
ERROR 1046 (3D000): No database selected
mysql> use q;
Database changed
mysql> CREATE TABLE Orders (
-> order_ID INT PRIMARY KEY,
-> customer_ID INT,
-> order_Date DATE
-> );
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO Orders (order_ID, customer_ID, order_Date) VALUES


-> (1, 101, '2024-11-17'),
-> (2, 102, '2024-11-16'),
-> (3, 103, '2024-11-15'),
-> (4, 104, '2024-11-14'),
-> (5, 105, '2024-11-13');
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> select*from Orders;


+----------+-------------+------------+
| order_ID | customer_ID | order_Date |
+----------+-------------+------------+
| 1 | 101 | 2024-11-17 |
| 2 | 102 | 2024-11-16 |
| 3 | 103 | 2024-11-15 |
| 4 | 104 | 2024-11-14 |
| 5 | 105 | 2024-11-13 |
+----------+-------------+------------+
5 rows in set (0.00 sec)

mysql> CREATE TABLE Customers (


-> customer_ID INT PRIMARY KEY,
-> customer_Name VARCHAR(100)
-> );
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO Customers (customer_ID, customer_Name) VALUES


-> (101, 'John Doe'),
-> (102, 'Jane Smith'),
-> (103, 'Michael Brown'),
-> (104, 'Emily Davis'),
-> (105, 'Daniel Wilson');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> select * from Customers;


+-------------+---------------+
| customer_ID | customer_Name |
+-------------+---------------+
| 101 | John Doe |
| 102 | Jane Smith |
| 103 | Michael Brown |
| 104 | Emily Davis |
| 105 | Daniel Wilson |
+-------------+---------------+
5 rows in set (0.00 sec)

mysql> desc table Customers;


+----+-------------+-----------+------------+------+---------------+------
+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len
| ref | rows | filtered | Extra |
+----+-------------+-----------+------------+------+---------------+------
+---------+------+------+----------+-------+
| 1 | SIMPLE | Customers | NULL | ALL | NULL | NULL | NULL
| NULL | 5 | 100.00 | NULL |
+----+-------------+-----------+------------+------+---------------+------
+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| q |
| sachin |
| stu_info |
| sys |
+--------------------+
7 rows in set (0.00 sec)

mysql> use q;
Database changed
mysql> show tables;
+-------------+
| Tables_in_q |
+-------------+
| customers |
| orders |
+-------------+
2 rows in set (0.00 sec)

mysql> drop Customers;


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
'Customers' at line 1
mysql> drop table Customers;
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO Customers (customer_ID, customer_Name) VALUES


-> (101, 'John Doe'),
-> (102, 'Jane Smith'),
-> (103, 'Michael Brown'),
-> (104, 'Emily Davis'),
-> (105, 'Daniel Wilson');
ERROR 1146 (42S02): Table '[Link]' doesn't exist
mysql> CREATE TABLE Customers (
-> customer_ID INT PRIMARY KEY,
-> customer_Name VARCHAR(100)
-> );
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO Customers (customer_ID, customer_Name) VALUES


-> (101, 'John Doe'),
-> (102, 'Jane Smith'),
-> (103, 'Michael Brown'),
-> (104, 'Emily Davis'),
-> (105, 'Daniel Wilson');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> select * from;


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 *
-> ;
ERROR 1096 (HY000): No tables used
mysql> select * from customers;
+-------------+---------------+
| customer_ID | customer_Name |
+-------------+---------------+
| 101 | John Doe |
| 102 | Jane Smith |
| 103 | Michael Brown |
| 104 | Emily Davis |
| 105 | Daniel Wilson |
+-------------+---------------+
5 rows in set (0.00 sec)

mysql> select * from orders;


+----------+-------------+------------+
| order_ID | customer_ID | order_Date |
+----------+-------------+------------+
| 1 | 101 | 2024-11-17 |
| 2 | 102 | 2024-11-16 |
| 3 | 103 | 2024-11-15 |
| 4 | 104 | 2024-11-14 |
| 5 | 105 | 2024-11-13 |
+----------+-------------+------------+
5 rows in set (0.00 sec)

mysql> CREATE TABLE NewOrders AS


-> SELECT o.order_ID, c.customer_Name, o.order_Date
-> FROM Orders o
-> JOIN Customers c ON o.customer_ID = c.customer_ID;
Query OK, 5 rows affected (0.02 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> show tables;


+-------------+
| Tables_in_q |
+-------------+
| customers |
| neworders |
| orders |
+-------------+
3 rows in set (0.00 sec)
mysql> select * from neworders;
+----------+---------------+------------+
| order_ID | customer_Name | order_Date |
+----------+---------------+------------+
| 1 | John Doe | 2024-11-17 |
| 2 | Jane Smith | 2024-11-16 |
| 3 | Michael Brown | 2024-11-15 |
| 4 | Emily Davis | 2024-11-14 |
| 5 | Daniel Wilson | 2024-11-13 |
+----------+---------------+------------+
5 rows in set (0.00 sec)

mysql> ALTER TABLE Orders


-> ADD CONSTRAINT fk_customer
-> FOREIGN KEY (customer_ID) REFERENCES Customers(customer_ID);
Query OK, 5 rows affected (0.04 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> select * from neworders;


+----------+---------------+------------+
| order_ID | customer_Name | order_Date |
+----------+---------------+------------+
| 1 | John Doe | 2024-11-17 |
| 2 | Jane Smith | 2024-11-16 |
| 3 | Michael Brown | 2024-11-15 |
| 4 | Emily Davis | 2024-11-14 |
| 5 | Daniel Wilson | 2024-11-13 |
+----------+---------------+------------+
5 rows in set (0.00 sec)

mysql> show tables;


+-------------+
| Tables_in_q |
+-------------+
| customers |
| neworders |
| orders |
+-------------+
3 rows in set (0.00 sec)

mysql> desc customers;


+---------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| customer_ID | int | NO | PRI | NULL | |
| customer_Name | varchar(100) | YES | | NULL | |
+---------------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> desc orders;;


+-------------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------+------+-----+---------+-------+
| order_ID | int | NO | PRI | NULL | |
| customer_ID | int | YES | MUL | NULL | |
| order_Date | date | YES | | NULL | |
+-------------+------+------+-----+---------+-------+
3 rows in set (0.00 sec)
ERROR:
No query specified

mysql> desc orders;


+-------------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------+------+-----+---------+-------+
| order_ID | int | NO | PRI | NULL | |
| customer_ID | int | YES | MUL | NULL | |
| order_Date | date | YES | | NULL | |
+-------------+------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> desc neworders;


+---------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| order_ID | int | NO | | NULL | |
| customer_Name | varchar(100) | YES | | NULL | |
| order_Date | date | YES | | NULL | |
+---------------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| q |
| sachin |
| stu_info |
| sys |
+--------------------+
7 rows in set (0.00 sec)

mysql> use stu_info;


Database changed
mysql> show tables;
+--------------------+
| Tables_in_stu_info |
+--------------------+
| student |
+--------------------+
1 row in set (0.00 sec)

mysql> select * from stu_info;


ERROR 1146 (42S02): Table 'stu_info.stu_info' doesn't exist
mysql> select * from students;
ERROR 1146 (42S02): Table 'stu_info.students' doesn't exist
mysql> select * from student;
+------------+-------+-------+
| student_id | name | marks |
+------------+-------+-------+
| 101 | Rohit | 410 |
| 102 | Mohit | 425 |
| 104 | Virat | 495 |
+------------+-------+-------+
3 rows in set (0.00 sec)

mysql> select min(marks),max(marks),sum(marks),avg(marks) from student;


+------------+------------+------------+------------+
| min(marks) | max(marks) | sum(marks) | avg(marks) |
+------------+------------+------------+------------+
| 410 | 495 | 1330 | 443.3333 |
+------------+------------+------------+------------+
1 row in set (0.00 sec)

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| q |
| sachin |
| stu_info |
| sys |
+--------------------+
7 rows in set (0.00 sec)

mysql> create database x;


Query OK, 1 row affected (0.00 sec)

mysql> use x;
Database changed
mysql> CREATE TABLE Customers (
-> customer_ID INT PRIMARY KEY,
-> customer_Name VARCHAR(100),
-> country VARCHAR(100)
-> );
Query OK, 0 rows affected (0.02 sec)

mysql> INSERT INTO Customers (customer_ID, customer_Name, country) VALUES


-> (1, 'John Doe', 'USA'),
-> (2, 'Jane Smith', 'Canada'),
-> (3, 'Michael Brown', 'USA'),
-> (4, 'Emily Davis', 'UK'),
-> (5, 'Daniel Wilson', 'Canada'),
-> (6, 'Sophia Johnson', 'Australia'),
-> (7, 'Liam Martinez', 'USA'),
-> (8, 'Olivia Garcia', 'UK');
Query OK, 8 rows affected (0.00 sec)
Records: 8 Duplicates: 0 Warnings: 0

mysql> select * from customers;


+-------------+----------------+-----------+
| customer_ID | customer_Name | country |
+-------------+----------------+-----------+
| 1 | John Doe | USA |
| 2 | Jane Smith | Canada |
| 3 | Michael Brown | USA |
| 4 | Emily Davis | UK |
| 5 | Daniel Wilson | Canada |
| 6 | Sophia Johnson | Australia |
| 7 | Liam Martinez | USA |
| 8 | Olivia Garcia | UK |
+-------------+----------------+-----------+
8 rows in set (0.00 sec)

mysql> SELECT country, COUNT(customer_ID) AS TotalCustomers


-> FROM Customers
-> GROUP BY country;
+-----------+----------------+
| country | TotalCustomers |
+-----------+----------------+
| USA | 3 |
| Canada | 2 |
| UK | 2 |
| Australia | 1 |
+-----------+----------------+
4 rows in set (0.00 sec)

mysql> SELECT country, COUNT(*) "TotalCustomers"from customer group by country;


ERROR 1146 (42S02): Table '[Link]' doesn't exist
mysql> SELECT country, COUNT(*) "TotalCustomers"from country group by customer;
ERROR 1146 (42S02): Table '[Link]' doesn't exist
mysql> use student;
ERROR 1049 (42000): Unknown database 'student'
mysql> use stu_info;
Database changed
mysql> show tables;
+--------------------+
| Tables_in_stu_info |
+--------------------+
| student |
+--------------------+
1 row in set (0.00 sec)

mysql> select * from student;


+------------+-------+-------+
| student_id | name | marks |
+------------+-------+-------+
| 101 | Rohit | 410 |
| 102 | Mohit | 425 |
| 104 | Virat | 495 |
+------------+-------+-------+
3 rows in set (0.00 sec)

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| q |
| sachin |
| stu_info |
| sys |
| x |
+--------------------+
8 rows in set (0.00 sec)
mysql> use x;
Database changed
mysql> CREATE TABLE StudentNames (
-> student_ID INT PRIMARY KEY,
-> name VARCHAR(100)
-> );
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO StudentNames (student_ID, name) VALUES


-> (1, 'John Doe'),
-> (2, 'Jane Smith'),
-> (3, 'Michael Brown'),
-> (4, 'Emily Davis'),
-> (5, 'Daniel Wilson');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> CREATE TABLE StudentDOB (


-> student_ID INT PRIMARY KEY,
-> date_of_birth DATE
-> );
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO StudentDOB (student_ID, date_of_birth) VALUES


-> (1, '2000-01-15'),
-> (2, '1999-05-22'),
-> (3, '2001-07-11'),
-> (4, '2000-11-30'),
-> (5, '1998-02-17');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> select * from studentnames;


+------------+---------------+
| student_ID | name |
+------------+---------------+
| 1 | John Doe |
| 2 | Jane Smith |
| 3 | Michael Brown |
| 4 | Emily Davis |
| 5 | Daniel Wilson |
+------------+---------------+
5 rows in set (0.00 sec)

mysql> select * from studentdob;


+------------+---------------+
| student_ID | date_of_birth |
+------------+---------------+
| 1 | 2000-01-15 |
| 2 | 1999-05-22 |
| 3 | 2001-07-11 |
| 4 | 2000-11-30 |
| 5 | 1998-02-17 |
+------------+---------------+
5 rows in set (0.00 sec)

mysql> CREATE TABLE StudentInfo AS


-> SELECT [Link], sd.date_of_birth
-> FROM StudentNames sn
-> JOIN StudentDOB sd ON sn.student_ID = sd.student_ID;
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> show tables;


+--------------+
| Tables_in_x |
+--------------+
| customers |
| studentdob |
| studentinfo |
| studentnames |
+--------------+
4 rows in set (0.00 sec)

mysql> select * from studentinfo;


+---------------+---------------+
| name | date_of_birth |
+---------------+---------------+
| John Doe | 2000-01-15 |
| Jane Smith | 1999-05-22 |
| Michael Brown | 2001-07-11 |
| Emily Davis | 2000-11-30 |
| Daniel Wilson | 1998-02-17 |
+---------------+---------------+
5 rows in set (0.00 sec)

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| q |
| sachin |
| stu_info |
| sys |
| x |
+--------------------+
8 rows in set (0.00 sec)

mysql> use stu_info;


Database changed
mysql> show tables;
+--------------------+
| Tables_in_stu_info |
+--------------------+
| student |
+--------------------+
1 row in set (0.00 sec)

mysql> select * from student;


+------------+-------+-------+
| student_id | name | marks |
+------------+-------+-------+
| 101 | Rohit | 410 |
| 102 | Mohit | 425 |
| 104 | Virat | 495 |
+------------+-------+-------+
3 rows in set (0.00 sec)

mysql> SELECT student_ID, marks


-> FROM StudentMarks
-> ORDER BY marks DESC;
ERROR 1146 (42S02): Table 'stu_info.studentmarks' doesn't exist
mysql> select student_id,marks from student order by marks desc;
+------------+-------+
| student_id | marks |
+------------+-------+
| 104 | 495 |
| 102 | 425 |
| 101 | 410 |
+------------+-------+
3 rows in set (0.00 sec)

mysql>

Common questions

Powered by AI

SQL errors in creating tables often involve syntax mistakes, such as incorrect use of types or constraints, as seen when creating tables like 'orders' with errors near '(20)' or lack of visible columns . When creating databases, errors are more likely to occur from syntax issues like the use of reserved words or invalid characters in names, as shown by attempts to create a database named '1' .

Troubleshooting in these examples shows trial and error and iterative refinement, such as correcting data type definitions and adjusting syntax errors. However, reliance on error messages rather than proactive design considerations limits effectiveness, as seen in repeated syntax and type mismatch issues. Structured planning could improve these methods .

The database 'q' appears to focus on managing orders and customer relations, requiring tables like 'Orders', 'Customers', and 'NewOrders', indicating complex relational data handling. In contrast, 'stu_info' centers around educational data, like the 'student' table, necessitating simpler data structures .

Using integers for IDs ensures numeric consistency but restricts flexibility for mixed-character IDs, potentially complicating integration with external systems using alphanumeric IDs. VARCHAR provides flexibility at a potential cost of increased storage and case sensitivity issues . Choosing depends on application requirements.

Creating 'NewOrders' using a SELECT statement demonstrates using subqueries to initialize a table with derived data. It selects data from the 'Orders' table and joins it with 'Customers' to create a comprehensive dataset with combined fields, thus leveraging subqueries for data amalgamation .

The attempt to create the 'orderdetails' table failed due to a missing dependent table, 'orders', in 'stu_info'. The query intended to join 'orders' and 'customer' tables, but the non-existence of 'orders' caused the operation to fail . Proper database integrity and table existence checks are crucial to prevent such issues.

Altering tables to add constraints reflects schema flexibility, allowing the enhancement of data integrity post-creation. This aids in evolving database structures to accommodate changing requirements while maintaining integrity, as shown by adding 'fk_customer' to 'Orders' . It highlights balancing initial schema design and adaptability.

Character limitations influence design by dictating maximum data length, impacting how data is stored and queried. For example, the failure to insert data due to 'Data too long for column 'ordrdate'' highlights the need to appropriately define VARCHAR lengths . Designers must anticipate data size needs to prevent such issues.

The use of foreign keys ensures referential integrity by enforcing a link between tables. In the Orders database schema, the foreign key 'fk_customer' ensures that entries in the 'Orders' table correspond to valid entries in the 'Customers' table, thus preventing orphan records .

Using aggregate functions such as min, max, sum, and avg helps derive statistical insights from the data, such as evaluating student performance distribution. In the 'student' table, these calculations provide a comprehensive view of mark distribution, aiding in identifying trends and potential anomalies .

You might also like