This code creates a new database named "test" in MySQL:
mysql> CREATE DATABASE test;
Query OK, 1 row affected (0.13 sec)
mysql>
The statement is executed successfully and the query result shows "Query OK, 1 row affected", indicating that one database was created.
mysql> USE test;
Database changed
mysql>
This code uses the MySQL command USE
to switch to the test
database that was just created with the CREATE DATABASE
command.
The USE
command is used to select the database that you want to work with in MySQL.
In this case, the test
database is now the active database and any subsequent queries or commands will be executed within this database.
mysql> CREATE TABLE friends (
-> id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
-> Name CHAR(30),
-> Lastname CHAR(30),
-> Telephone INT);
Query OK, 0 rows affected (1.02 sec)
mysql>
This code creates a new table named "friends" in the currently selected database ("test" in this case). The table has four columns:
- "id": an integer column that serves as the primary key for the table. It is set to not allow NULL values, and is set to auto-increment, meaning that a new value will be automatically generated for each new row inserted into the table.
- "Name": a character column that can store up to 30 characters.
- "Lastname": another character column that can store up to 30 characters.
- "Telephone": an integer column that can store phone numbers.
The query returns "Query OK, 0 rows affected" indicating that the table was successfully created.
mysql> SHOW TABLES FROM test;
+----------------+
| Tables_in_test |
+----------------+
| friends |
+----------------+
1 row in set (0.00 sec)
mysql>
This code is displaying the list of tables in the "test" database using the SHOW TABLES
statement.
It then returns a table with a single row containing the name of the only table in the "test" database, which is "friends".
mysql> DESCRIBE friends;
+-----------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| Name | char(30) | YES | | NULL | |
| Lastname | char(30) | YES | | NULL | |
| Telephone | int | YES | | NULL | |
+-----------+----------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql>
The DESCRIBE statement is used to display the structure of the "friends" table, which shows the names, data types, and other details about each of the table's columns.
No comments:
Post a Comment