MariaDB數據庫基本操作


1、CMD打開執行命令:mysql -u root -p
    輸入數據庫密碼,進入數據庫

PS C:\Users\admin> mysql -u root -p
Enter password: ****
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.4.7-MariaDB mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 



2、執行show databases; ,查看MariaDB的數據庫結構。

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| django             |
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.000 sec)

 




3、新建一個庫,名字叫my。

MariaDB [test]> create database my;
Database changed

 




4、可以再次通過show databases查看新建的庫

5、使用庫:use my

6、新建一個表單:sql語句如下
    

    MariaDB [my]> create table my.student(
    -> id int(4) primary key,
    -> name varchar(4) not null,
    -> age int(2) not null
    -> );



  注:語句的結構如果錯誤,就會出現問題。錯一點點都不行,反例如下:

MariaDB [my]> create table my.student(
    -> id int(4) primary key,
    -> name varchar(4) not null,
    -> age int(2) not null,               #此處多了一個,導致了報錯
    -> );
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 5

 



7、查看表情況

MariaDB [my]> show tables;
+--------------+
| Tables_in_my |
+--------------+
| student      |
+--------------+
1 row in set (0.000 sec)

 


8、查看表的字段信息

MariaDB [my]> desc student
    -> ;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id    | int(4)     | NO   | PRI | NULL    |       |
| name  | varchar(4) | NO   |     | NULL    |       |
| age   | int(2)     | NO   |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
3 rows in set (0.011 sec)


注:CMD內執行命令,如果結束了必須得帶上分號 ;



9、 改變表的結構
eg1 新增字段address

MariaDB [my]> alter table student add address varchar(48);
Query OK, 0 rows affected (0.007 sec)
Records: 0  Duplicates: 0  Warnings: 0


eg2 新增字段gender

MariaDB [my]> alter table student add gender enum("boy","girl") after age;
Query OK, 0 rows affected (0.008 sec)
Records: 0  Duplicates: 0  Warnings: 0


語句必須要正確,不然一定會報錯。如下:

MariaDB [my]> alert table student add address varchar(48);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'alert table student add address varchar(48)' at line 1

這里的錯誤是語句alter,這里打成了alert


10、表結構改變成功后查看結構

MariaDB [my]> desc student;

+---------+--------------------+------+-----+---------+-------+
| Field   | Type               | Null | Key | Default | Extra |
+---------+--------------------+------+-----+---------+-------+
| id      | int(4)             | NO   | PRI | NULL    |       |
| name    | varchar(4)         | NO   |     | NULL    |       |
| age     | int(2)             | NO   |     | NULL    |       |
| gender  | enum('boy','girl') | YES  |     | NULL    |       |
| address | varchar(48)        | YES  |     | NULL    |       |
+---------+--------------------+------+-----+---------+-------+
5 rows in set (0.007 sec)


11、修改表字段
eg1 將gender修改為sex

MariaDB [my]> alter table student change gender sex enum("boy","girl") not null;

Query OK, 0 rows affected (0.014 sec)
Records: 0  Duplicates: 0  Warnings: 0


查看表結構信息

MariaDB [my]> desc student;
+---------+--------------------+------+-----+---------+-------+
| Field   | Type               | Null | Key | Default | Extra |
+---------+--------------------+------+-----+---------+-------+
| id      | int(4)             | NO   | PRI | NULL    |       |
| name    | varchar(4)         | NO   |     | NULL    |       |
| age     | int(2)             | NO   |     | NULL    |       |
| sex     | enum('boy','girl') | NO   |     | NULL    |       |
| address | varchar(48)        | YES  |     | NULL    |       |
+---------+--------------------+------+-----+---------+-------+
5 rows in set (0.007 sec)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM