打开终端输入如下命令:
/usr/local/mysql/bin/mysql -u root -p
其中root为用户名。
这时会出现如下命令:
enter password:
此时如果你没有改密码,直接敲回车。否则,输入你的密码。
这样就可以访问你的数据库服务器了。
1、的操作及管理数据表的基
数据库(database)管理1.1 create 创建数据库
create database firstdb;
1.2 show 查看所有数据库
mysql> show databases;+--------------------+| database |+--------------------+| information_schema || firstdb || mysql || performance_schema |+--------------------+rows in set (0.00 sec)
1.3 alter 修改数据库
alter 命令修改数据库编码:
默认创建的数据库默认不支持中文字符,如果我们需要它支持中文字符,则将它的编码设置为utf8格式:
mysql> alter database testdb character set utf8;query ok, 1 row affected (0.00 sec)
1.4 use 使用数据库
mysql> use firstdb;database changed
1.5 查看当前使用的数据库
mysql> select database();+------------+| database() |+------------+| firstdb |+------------+1 row in set (0.00 sec)
1.6 drop 删除数据库
mysql> drop database firstdb;query ok, 0 rows affected (0.00 sec)
2、数据表(table)管理我们首先创建一个数据库,提供我们往后的使用:
mysql> create database testdb;query ok, 1 row affected (0.00 sec)
创建后记得用use命令进入(使用)数据库,不然后面的操作都会不成功的。
2.1 create 创建表
mysql> create table people ( -> id int auto_increment primary key, -> name varchar(20) not null, -> age int not null, -> birthday datetime); query ok, 0 rows affected (0.01 sec)
2.2 show 显示表
显示当前数据库所有的数据表
mysql> show tables;+------------------+| tables_in_testdb |+------------------+| people |+------------------+1 row in set (0.00 sec)
2.3 desc 查看表结构
mysql> desc people -> ;+----------+-------------+------+-----+---------+----------------+| field | type | null | key | default | extra |+----------+-------------+------+-----+---------+----------------+| id | int(11) | no | pri | null | auto_increment || name | varchar(20) | no | | null | || age | int(11) | no | | null | || birthday | datetime | yes | | null | |+----------+-------------+------+-----+---------+----------------+4 rows in set (0.01 sec)
2.4 alter 修改表结构(增、删、改)
默认创建的表不支持中文字符,所以需将表编码设置为utf8:
mysql> alter table keychain convert to character set utf8;query ok, 1 row affected (0.02 sec)records: 1 duplicates: 0 warnings: 0
2.4.1 insert 在表中添加列(字段)
mysql> alter table people add star bool;query ok, 0 rows affected (0.02 sec)records: 0 duplicates: 0 warnings: 0
提示:在mysql里,布尔类型会自动转换为tinyint(1)类型。
我们不妨使用desc去查看一下people表结构:
mysql> desc people;+----------+-------------+------+-----+---------+----------------+| field | type | null | key | default | extra |+----------+-------------+------+-----+---------+----------------+| id | int(11) | no | pri | null | auto_increment || name | varchar(20) | no | | null | || age | int(11) | no | | null | || birthday | datetime | yes | | null | || star | tinyint(1) | yes | | null | |+----------+-------------+------+-----+---------+----------------+5 rows in set (0.00 sec)
现在,你该相信我了吧?
2.4.2 alter 修改表(列)字段
mysql> alter table people modify star int;query ok, 0 rows affected (0.01 sec)records: 0 duplicates: 0 warnings: 0
也可以指定 int(n) 的长度,比如 int(2)。
我们再次使用desc查看people表结构:
mysql> desc people;+----------+-------------+------+-----+---------+----------------+| field | type | null | key | default | extra |+----------+-------------+------+-----+---------+----------------+| id | int(11) | no | pri | null | auto_increment || name | varchar(20) | no | | null | || age | int(11) | no | | null | || birthday | datetime | yes | | null | || star | int(11) | yes | | null | |+----------+-------------+------+-----+---------+----------------+5 rows in set (0.00 sec)
2.4.3 delete 删除表(列)字段
mysql> alter table people drop column star;query ok, 0 rows affected (0.02 sec)records: 0 duplicates: 0 warnings: 0
删除后,再次查看people表结构:
mysql> desc people;+----------+-------------+------+-----+---------+----------------+| field | type | null | key | default | extra |+----------+-------------+------+-----+---------+----------------+| id | int(11) | no | pri | null | auto_increment || name | varchar(20) | no | | null | || age | int(11) | no | | null | || birthday | datetime | yes | | null | |+----------+-------------+------+-----+---------+----------------+4 rows in set (0.00 sec)
删除字段成功,现在我们已经不能看到star的字段了。
2.4.4 rename 重命名表名
mysql> rename table people to new_people;query ok, 0 rows affected (0.00 sec)
2.4.5 null or not null
修改表字段允许为空或不允许为空:
mysql> alter table people modify age int(3) null;query ok, 0 rows affected (0.01 sec)records: 0 duplicates: 0 warnings: 0
把 people 表的 age 字段设置成“允许为空”,即插入记录时这个字段可以不录入。否则相反。
它的格式为:alter table modify
mysql> create table newtable select * from people;query ok, 0 rows affected (0.01 sec)records: 0 duplicates: 0 warnings: 0
我们查看一下目前数据库存在的表:
mysql> show tables;+------------------+| tables_in_testdb |+------------------+| people || newtable |+------------------+2 rows in set (0.00 sec)
3、数据的操作及管理本操作,包含增、删、改、查数据。
以下命令均在people表上操作。
3.1 增加数据(增)
people表目前是没有数据的,它是空的数据表,我们现在先添加一些数据。
insert into 命令添加数据:
mysql> insert into people values (null, 'anny', 22, '1992-05-22');query ok, 1 row affected (0.00 sec)
使用select命令查看表(会在后面介绍),现在我们查看people数据表的数据:
mysql> select * from people;+----+------+-----+---------------------+| id | name | age | birthday |+----+------+-----+---------------------+| 1 | anny | 22 | 1992-05-22 00:00:00 |+----+------+-----+---------------------+1 row in set (0.00 sec)
数据表现在有一条数据。
我们多添加几条数据,如:
mysql> select * from people;+----+--------+-----+---------------------+| id | name | age | birthday |+----+--------+-----+---------------------+| 1 | anny | 22 | 1992-05-22 00:00:00 || 2 | garvey | 23 | 1991-05-22 00:00:00 || 3 | lisa | 25 | 1989-05-22 00:00:00 || 4 | nick | 24 | 1990-05-22 00:00:00 || 5 | rick | 24 | 1991-05-22 00:00:00 |+----+--------+-----+---------------------+5 rows in set (0.00 sec)
3.2 删除数据(删)
delete 命令删除数据:
mysql> delete from people where name = 'lisa';query ok, 1 row affected (0.01 sec)
再次查询people表:
mysql> select * from people;+----+--------+-----+---------------------+| id | name | age | birthday |+----+--------+-----+---------------------+| 1 | anny | 22 | 1992-05-22 00:00:00 || 2 | garvey | 23 | 1991-05-22 00:00:00 || 4 | nick | 24 | 1990-05-22 00:00:00 || 5 | rick | 24 | 1991-05-22 00:00:00 |+----+--------+-----+---------------------+4 rows in set (0.00 sec)
已经看不到名为“lisa”的数据了。
3.3 修改数据(改)
update 命令修改数据:
mysql> update people set name='calvin' where name = 'garvey';query ok, 1 row affected (0.00 sec)rows matched: 1 changed: 1 warnings: 0
查询people表内容:
mysql> select * from people;+----+--------+-----+---------------------+| id | name | age | birthday |+----+--------+-----+---------------------+| 1 | anny | 22 | 1992-05-22 00:00:00 || 2 | calvin | 23 | 1991-05-22 00:00:00 || 4 | nick | 24 | 1990-05-22 00:00:00 || 5 | rick | 24 | 1991-05-22 00:00:00 |+----+--------+-----+---------------------+4 rows in set (0.00 sec)
名为“garvey”的记录已经修改为“calvin”。
3.4 查询数据(查)
select 命令查询数据,最简单的就是查询表的所有数据,也就是我们最初使用到的那条命令:
mysql> select * from people;+----+--------+-----+---------------------+| id | name | age | birthday |+----+--------+-----+---------------------+| 1 | anny | 22 | 1992-05-22 00:00:00 || 2 | calvin | 23 | 1991-05-22 00:00:00 || 4 | nick | 24 | 1990-05-22 00:00:00 || 5 | rick | 24 | 1991-05-22 00:00:00 |+----+--------+-----+---------------------+4 rows in set (0.00 sec)
格式:select * from <表名>,*代表所有字段。
查询数据时也可指定显示的(列)字段:
mysql> select name, age, birthday from people;+--------+-----+---------------------+| name | age | birthday |+--------+-----+---------------------+| anny | 22 | 1992-05-22 00:00:00 || calvin | 23 | 1991-05-22 00:00:00 || nick | 24 | 1990-05-22 00:00:00 || rick | 24 | 1991-05-22 00:00:00 |+--------+-----+---------------------+4 rows in set (0.00 sec)
格式:select <字段名,字段名,…> from <表名>。
select查询命令还有很多的高级用法,比如用来查找不重复(distinct)的数据,使数据按条件排序(order by),按查询条件显示数据(where)等等。这些都会在下一篇文章作重点介绍,请大家继续留意我的博客,谢谢。
4、管理视图4.1 创建视图
视图是从数据库里导出一个或多个表的虚拟表,是用来方便用户对数据的操作。
mysql> create view people_view ( -> name, age) -> as select name, age from people;
创建成功后查看视图。
people people.age people.birthday people.id people.name mysql> select * from people_view -> ;+--------+-----+| name | age |+--------+-----+| anny | 22 || calvin | 23 || nick | 24 || rick | 24 |+--------+-----+4 rows in set (0.00 sec)
我们也可以使用 desc 命令查看视图的结构。
mysql> desc people_view;+-------+---------+------+-----+---------+-------+| field | type | null | key | default | extra |+-------+---------+------+-----+---------+-------+| id | int(11) | no | | 0 | |+-------+---------+------+-----+---------+-------+1 row in set (0.01 sec)
4.2 替换视图
创建或替换原有视图。
mysql> create or replace view people_view(people_id,people_name,people_age) as select id,name,age from people;query ok, 0 rows affected (0.00 sec)
创建或替换后查看视图。
mysql> select * from people_view;+-----------+-------------+------------+| people_id | people_name | people_age |+-----------+-------------+------------+| 1 | anny | 22 || 2 | calvin | 23 || 4 | nick | 24 || 5 | rick | 24 |+-----------+-------------+------------+4 rows in set (0.00 sec)
4.3 操作视图
当视图数据有变化时(增、删、改),真实的表数据也会随着改变。也就是说,对视图的操作就是对表的数据,所以我们可以把视图当作表。
例:往视图插入一条数据。
mysql> insert into people_view values(null, 'kerry', '33');query ok, 1 row affected (0.00 sec)
插入数据成功后查看视图。
mysql> select * from people_view ;+-----------+-------------+------------+| people_id | people_name | people_age |+-----------+-------------+------------+| 1 | anny | 22 || 2 | calvin | 23 || 4 | nick | 24 || 5 | rick | 24 || 6 | kerry | 33 |+-----------+-------------+------------+5 rows in set (0.00 sec)
可以在视图上看到我们刚刚插入的数据,现在我们就来验证一下真实的表是否也会作出变化。
mysql> select * from people;+----+--------+-----+---------------------+| id | name | age | birthday |+----+--------+-----+---------------------+| 1 | anny | 22 | 1992-05-22 00:00:00 || 2 | calvin | 23 | 1991-05-22 00:00:00 || 4 | nick | 24 | 1990-05-22 00:00:00 || 5 | rick | 24 | 1991-05-22 00:00:00 || 6 | kerry | 33 | null |+----+--------+-----+---------------------+5 rows in set (0.00 sec)
可见,真实的表数据也已经有所改变,刚刚往视图里插入的那一条数据存在于真实表中,真理便是:对视图的操作就是对表的数据。
4.4 删除视图
mysql> drop view people_view;query ok, 0 rows affected (0.00 sec)
相关推荐:
mac使用终端运行mysql,mysql终端,mysql mac,mysql目录,mysql路径,macmysql
mac使用终端运行mysql,mysql终端,mysql mac,mysql目录,mysql路径
以上就是mac上实现终端管理mysql数据库的详细内容。