上海市手拉手艺术团 2025-05-09 14:09:24
有趣的青蛙游戏汇总2025 玩不腻的青蛙游戏合集 2025-05-30 06:16:58
​美图秀秀背景透明怎么设置 2025-05-08 17:17:39
如何选罗技键盘?Logitech 键盘选购指南 2025-05-23 08:31:09
新《水浒》创新尺度大 潘金莲西门庆有真爱(图) 2025-06-15 04:27:10
玉髓是玉吗_玉髓和玉有什么区别 2025-06-13 14:23:12
阿迪达斯携手四大球星推世界杯新广告 2025-06-23 02:41:31
投诉一下,下载了博尔量化,有个一元的服务先买了试试,说是一个月,结果半个月不到就不能用了! 2025-06-23 16:20:06
作为日本国宝级女歌手,椎名林檎到底哪儿“爵”了? 2025-05-18 01:29:36
B&O A9音质怎么样,值得买吗? 2025-06-11 03:35:20

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 my6、新建一个表单: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,这里打成了alert10、表结构改变成功后查看结构

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)