Python数据可视化

10分钟MariaDB数据库初级教程

2020-05-10  本文已影响0人  老王叔叔

本文来源于MariaDB官方网站。

1.登录MariaDB:用户名替换称自己的用户

mysql -u 用户名 -p -h ip地址 数据库名字
或者
mysql -u 用户名 -p 

2.创建数据库:
下面的例子,建立了一个数据库test,然后建立了3个表。

CREATE DATABASE IF NOT EXISTS test;                //创建数据库
USE test;                                          //使用数据库test
CREATE TABLE IF NOT EXISTS books (                 //创建表books
  BookID INT NOT NULL PRIMARY KEY AUTO_INCREMENT, 
  Title VARCHAR(100) NOT NULL, 
  SeriesID INT, AuthorID INT);

CREATE TABLE IF NOT EXISTS authors              //创建表authors
(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT);

CREATE TABLE IF NOT EXISTS series               //创建表series
(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT);

INSERT INTO books (Title,SeriesID,AuthorID) //在books表中插入数据
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);

3.查看一下数据库test中的所有表:

SHOW TABLES;

+----------------+
| Tables_in_test |
+----------------+
| authors        |
| books          |
| series         |
+----------------+
3 rows in set (0.00 sec)

4.获得一个表的详细信息:

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    |                |
+----------+--------------+------+-----+---------+----------------+

5.检索一下books表。

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.00 sec)

" * "是选择所有列。
如果要选择单独的列或者指定的列
比如:

    SELECT Title,BookID  FROM books;
    或者
    SELECT Title FROM books;

  1. 插入数据
INSERT INTO books (Title, SeriesID, AuthorID)
VALUES ("Lair of Bones", 2, 2);

Query OK, 1 row affected (0.00 sec)
  1. 修改数据
UPDATE books 
SET Title = "The Hobbit" //修改最后一行Hobbbit
WHERE BookID = 7;

Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
上一篇下一篇

猜你喜欢

热点阅读