程序媛计划——mysql基本操作

本文适用于mac

在官网上下载community 版mysql,选择dmy这种。在终端中安装好mysql。

#进入mysql

 /usr/local/mysql/bin/mysql -uroot -p

#修改root密码(root是默认的用户名)

#账户名默认都是root,注意输入账户和新密码时不需要再带' '号

[mysql> set password for ACCOUNT@localhost = password(NEW_PASSWORD);

#直接敲 exit 退出mysql

创建数据库、创建表

#创建名为test的数据库

mysql> create database test;
#Query OK, 1 row affected (0.01 sec)

#查看当前所有数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.02 sec)

#删除某数据库

mysql> drop database test;
#Query OK, 0 rows affected (0.01 sec)

#查看当前数据库

mysql> select database();
+------------+
| database() |
+------------+
| test       |
+------------+
1 row in set (0.00 sec)

#对数据表进行操作

#连接到数据库test
[mysql> use test; Database changed
#创建表
#表中的各个列组成了一个元组 mysql
> create table if not exists exam_score( -> id int(4) not null primary key auto_increment, #auto_increment自增 -> name char(20) not null, -> score double); Query OK, 0 rows affected (0.03 sec) #显示表的结构(各个列都有些啥属性) mysql> show columns from exam_score; +-------+----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+----------------+ | id | int(4) | NO | PRI | NULL | auto_increment | | name | char(20) | NO | | NULL | | | score | double | YES | | NULL | | +-------+----------+------+-----+---------+----------------+ 3 rows in set (0.01 sec) #展示所有表 mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | exam_score | +----------------+ 1 row in set (0.00 sec) #删除表(注意mysql中删除表和删除数据库都是drop,sqlite中删除表用delete) mysql> drop table exam_score; Query OK, 0 rows affected (0.00 sec)

原文地址:https://www.cnblogs.com/IcarusYu/p/7495125.html