一、如何进入MySQL数据库
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 5
Server version: 5.6.50 MySQL Community Server (GPL)
affiliates. Other names may be trademarks of their respective
owners.
二、查看数据库信息
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lixing |
| lx |
| mysql |
| performance_schema |
+--------------------+
5 rows in set (0.00 sec)
三、查看当前数据库有哪些表
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
28 rows in set (0.00 sec)
四、查看表的结构
1 mysql> USE mysql;
2 mysql> DESCRIBE user;
五、SQL语句概述
SQL语言:
Structured Query Language的缩写,即结构化查询语言
关系型数据库的标准语言
用于维护管理数据库,包括数据查询、数据更新、访问控制、对象管理等功能
SQL分类:
DDL(Data Definition Language)数据定义语言:用来建立数据库、数据库对象和定义字段,如CREATE、ALTER、DROP
DML(Data Manipulation Language)数据操纵语言:用来插入、删除和修改数据库中的数据,如INSERT、UPDATE、DELETE
DQL(Data Query Language)数据查询语言:用来查询数据中的数据,如SELECT
DCL(Data Control Language)数据控制语言:用来控制数据库组件的存取许可、存取权限等,如COMMIT、ROLLBACK、GRANT、REVOKE
创建新的数据库
ql> create database lx;
Query OK, 1 row affected (0.00 sec)
创建新的表
Database changed
mysql> show tables;
+--------------+
| Tables_in_lx |
+--------------+
| abcd |
+--------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
删除一个数据库(原有数据库名称:lx)
| Database |
+--------------------+
| information_schema |
| lx |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
+--------------+
| Tables_in_lx |
+--------------+
| aaa |
+--------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
Empty set (0.00 sec)
+----------+---------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------+
| lx | CREATE DATABASE `lx` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)
Query OK, 1 row affected (0.00 sec)
+----------+-------------------------------------------------------------+
| Database | Create Database |
+----------+-------------------------------------------------------------+
| lx | CREATE DATABASE `lx` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-------------------------------------------------------------+
1 row in set (0.00 sec)
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| name | int(32) | NO | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
-> add column age int(32);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| name | int(32) | NO | | NULL | |
| age | int(32) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
-> drop column weight;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| name | int(32) | NO | | NULL | |
| height | int(200) | YES | | NULL | |
+--------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
-> (
-> '李兴' , 3 , 300 , 300 , 3
-> )
-> ;
Query OK, 1 row affected (0.01 sec)
+--------+------+--------+--------+------+
| name | age | weight | height | tel |
+--------+------+--------+--------+------+
| 李兴 | 3 | 434 | 343 | 3 |
| 李兴 | 3 | 300 | 300 | 3 |
| 李兴 | 3 | 300 | 300 | 3 |
+--------+------+--------+--------+------+
3 rows in set (0.00 sec)