MySQL基本操作指南,MySQL数据库基本操作。小编来告诉你更多相关信息。
MySQL数据库基本操作
关于这方面的知识你知道吗?MySQL数据库基本操作的话题,接下来带大家一起了解。
基本操作有:查看有哪些数据库、查看有哪些表、创建数据库、创建表、查看表信息、向表中插入数据等
# 查看有哪些数据库MariaDB [(none)]> show databases;# 切换到test数据库MariaDB [(none)]> use test;# 查看当前数据库有哪些表MariaDB [test]> show tables;Empty set (0.000 sec) # 表明当前数据库是空的# 如果test数据库不存在,则创建MariaDB [test]> CREATE DATABASE IF NOT EXISTS test;# 在数据库test种创建表三个表:books、authors、seriesMariaDB [test]> CREATE TABLE IF NOT EXISTS books ( # 创建books表(前提是books表不存在,如果已经存在,则不创建) -> BookID INT NOT NULL PRIMARY KEY AUTO_INCREMENT, -> Title VARCHAR(100) NOT NULL, -> SeriesID INT, AuthorID INT);Query OK, 0 rows affected (0.033 sec)MariaDB [test]> CREATE TABLE IF NOT EXISTS authors # 创建authors表(前提是authors表不存在,如果已经存在,则不创建) -> (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT);Query OK, 0 rows affected (0.005 sec)MariaDB [test]> CREATE TABLE IF NOT EXISTS series # 创建series表(前提是series表不存在,如果已经存在,则不创建) -> (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT);Query OK, 0 rows affected (0.005 sec)# 接下来我们再来看看表是否添加成功MariaDB [test]> show tables;+----------------+| Tables_in_test |+----------------+| authors || books || series |+----------------+3 rows in set (0.000 sec)# 向books表中插入数据MariaDB [test]> INSERT INTO books (Title,SeriesID,AuthorID) -> VALUES(\'The Fellowship of the Ring\',1,1), -> (\'The Two Towers\',1,1), (\'The Return of the King\',1,1), -> (\'The Sum of All Men\',2,2), (\'Brotherhood of the Wolf\',2,2), -> (\'Wizardborn\',2,2), (\'The Hobbbit\',0,1);Query OK, 7 rows affected (0.004 sec)Records: 7 Duplicates: 0 Warnings: 0# 查看表信息MariaDB [test]> describe books;+----------+--------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------+--------------+------+-----+---------+----------------+| BookID | int(11) | NO | PRI | NULL | auto_increment || Title | varchar(100) | NO | | NULL | || SeriesID | int(11) | YES | | NULL | || AuthorID | int(11) | YES | | NULL | |+----------+--------------+------+-----+---------+----------------+4 rows in set (0.002 sec)# 查询数据(从表中查询数据)MariaDB [test]> select * from books;+--------+----------------------------+----------+----------+| BookID | Title | SeriesID | AuthorID |+--------+----------------------------+----------+----------+| 1 | The Fellowship of the Ring | 1 | 1 || 2 | The Two Towers | 1 | 1 || 3 | The Return of the King | 1 | 1 || 4 | The Sum of All Men | 2 | 2 || 5 | Brotherhood of the Wolf | 2 | 2 || 6 | Wizardborn | 2 | 2 || 7 | The Hobbbit | 0 | 1 |+--------+----------------------------+----------+----------+7 rows in set (0.000 sec)
小知识:sql语句允许换行,直到遇到分号+回车才会认为sql语句输入结束,进入执行阶段
以上是网关于MySQL数据库基本操作的IT小经验,希望能为您在生活中带来帮助!