Database learning exchange 1

1.1 command line startup service
net  start  service name
net  stop  service name

However, the dos window must be opened as an administrator

1.2 connecting to the server
local local
host host  -h
username user name  -u
password  password  -p
port port  -P(Capitalize)

Database connection succeeded


dos commands do not need semicolons, but sql statements do.

Ciphertext enter the password and enter after - P

If it is clear that the server is local, the IP address can be omitted
If you clearly know that the MySQL server uses port 3306, the port number can also be omitted

1.3 closing the connection

exit;
quit;
\q;



1.4 database operation

Syntax: show databases;
MySQL comes with four databases
—information_ Schema: information of MySQL server calendar database
—performance_schema:MySQL5.5. Add a new table to save the performance parameters of the database server
- mysql:MySQL system database, which stores the login user name, password and authority of each user
- Test: a database for users to learn and test

1.5 creating database
create database [if not exists]Data name[Character encoding]
—Note: when creating, only one is created, so it is used database,if not exists Is to determine whether there is this database


If it already exists - an error will be reported

resolvent:

mysql> create database if not exists stu;
Query OK, 1 row affected, 1 warning (0.00 sec)

The database name cannot be a keyword or special symbol when it is created

mysql> create database create
    -> ;
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 'create' at line 1
mysql> create database *8&^&%$$
    -> ;
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 '*8&^&%$$' at line 1

But if you have to use it, add a back quote`

mysql> create database `create`
    -> ;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| create             |
| mysql              |
| performance_schema |
| stu                |
| test               |
+--------------------+
6 rows in set (0.00 sec)

In order to be foolproof, all of them are created with backquotes

- the character code is not set when creating, but the default character code when downloading MySQL is used. Here is utf8
- you can also create it yourself

mysql> create database teacher charset = gbk;
Query OK, 1 row affected (0.00 sec)

gbk: Simplified Chinese
Simplified Chinese
utf8: universal coding

charset: character set

1.6 delete database

Syntax: drop database [if exists] database name

Note: if the database name is a keyword or a special character, you still have to add backquotes. Of course, just in case, you'd better put backquotes in front of the database name.

mysql> drop database create;
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 'create' at line 1
mysql> drop database `create`;
Query OK, 0 rows affected (0.16 sec)


mysql>  drop database `teacher`;
Query OK, 0 rows affected (0.00 sec)

mysql> drop database `stu`;
Query OK, 0 rows affected (0.00 sec)

There is no error report

mysql> drop database tercher;
ERROR 1008 (HY000): Can't drop database 'tercher'; database doesn't exist
mysql> drop database if exists `teacher`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
1.7 display creation statement

Syntax: show create database database name;

mysql> show create database teacher;
+----------+------------------------------------------------------------------+
| Database | Create Database                                                  |
+----------+------------------------------------------------------------------+
| teacher  | CREATE DATABASE `teacher` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+------------------------------------------------------------------+
1 row in set (0.00 sec)
1.8 modifying the database

Modify the character encoding of the database
Syntax: alter database database name charset = character code;

mysql> alter database teacher charset = gbk;
Query OK, 1 row affected (0.00 sec)
mysql> alter database teacher charset = gbk;
Query OK, 1 row affected (0.00 sec)

mysql> show create database `teacher`;
+----------+-----------------------------------------------------------------+
| Database | Create Database                                                 |
+----------+-----------------------------------------------------------------+
| teacher  | CREATE DATABASE `teacher` /*!40100 DEFAULT CHARACTER SET **gbk** */ |
+----------+--------------------------------****---------------------------------+
1 row in set (0.00 sec)
1.9 selecting a database

Syntax: use database name

mysql> use stu;
Database changed
mysql> use teacher;
Database changed
mysql>

Keywords: MySQL

Added by greenber on Mon, 24 Jan 2022 23:43:37 +0200