3.认识和操作一下mysql的基本命令
3.認識和操作一下mysql的基本命令
登錄mysql,在終端輸入以下命令,進行登錄
mysql -u root -pMacBook-Pro:~ yc$ mysql -u root -pEnter password:Welcome to the MySQL monitor.? Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.7.28 HomebrewCopyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>查看當前mysql中所有的庫
庫==>數(shù)據(jù)庫==>就像文件夾一樣,庫里面可以存儲很多個表)
show databases;
+--------------------+
| Database ? ? ? ? ? |
+--------------------+
| information_schema |
| mysql? ? ? ? ? ? ? |
| performance_schema |
| sys? ? ? ? ? ? ? ? |
+--------------------+
4 rows in set (0.00 sec)
選擇需要操作的庫,打開庫
use mysql; 查看當前庫中的所有數(shù)據(jù)表
show tables;
+---------------------------+
| Tables_in_mysql ? ? ? ? ? |
查看表中的數(shù)據(jù)
# 查看user表中的所有數(shù)據(jù)的所有字段 select * from user;
# 查看 user表中的所有數(shù)據(jù)的 host和user字段列 select host,user from user;
+-----------+---------------+
| host? ? ? | user? ? ? ? ? |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys ? ? |
| localhost | root? ? ? ? ? |
+-----------+---------------+
庫和表的概念與關系
庫就像是文件夾,庫中可以有很多個表 表就像是我們的excel表格文件一樣 每一個表中都可以存儲很多數(shù)據(jù)
mysql中可以有很多不同的庫,庫中可以有很多不同的表 表中可以定義不同的列(字段), 表中可以根據(jù)結構去存儲很多的數(shù)據(jù)
如何創(chuàng)建自己的庫?
create database 庫名 default charset=utf8; 創(chuàng)建庫
+---------------------------+
| columns_priv? ? ? ? ? ? ? |
| db? ? ? ? ? ? ? ? ? ? ? ? |
| engine_cost ? ? ? ? ? ? ? |
....
| time_zone_name? ? ? ? ? ? |
| time_zone_transition? ? ? |
| time_zone_transition_type |
| user? ? ? ? ? ? ? ? ? ? ? |
+---------------------------+
31 rows in set (0.00 sec)
create database tlxy default charset=utf8;
-- Query OK, 1 row affected (0.01 sec)
查看所有庫
show databases;
+--------------------+
| Database ? ? ? ? ? |
+--------------------+
| information_schema |
| mysql? ? ? ? ? ? ? |
| performance_schema |
| sys? ? ? ? ? ? ? ? |
| tlxy ? ? ? ? ? ? ? |
+--------------------+
5 rows in set (0.00 sec)
-- 進入庫 use tlxy;
創(chuàng)建表的語法
create table 表名( 字段名 類型 字段約束, 字段名 類型 字段約束, 字段名 類型 字段約束, )engine=innodb default
charset=utf8;
-- 創(chuàng)建用戶表 create table user(
? ? name varchar(20),
? ? age int,
? ? sex char(1)
)engine=innodb default charset=utf8;
-- Query OK, 0 rows affected (0.16 sec)
添加數(shù)據(jù)
-- 向 user 表中 添加 name,age,sex 數(shù)據(jù)
insert into user(name,age,sex) values('admin',26,'男'); -- Query OK, 1 row affected (0.00 sec)
insert into user(name,age,sex) values('張三',22,'女');
查看表中的數(shù)據(jù)
select * from user; +--------+------+------+ |name |age |sex | +--------+------+------+ |admin| 26|男 | |張三 | 22|女 | +--------+------+------+ 2 rows in set (0.00 sec)
總結
認識庫,表的概念和關系 mysql的基本命令: 登錄,查看庫,選擇庫,查看表, 創(chuàng)建庫,創(chuàng)建表,添加數(shù)據(jù),查詢數(shù)據(jù)。
總結
以上是生活随笔為你收集整理的3.认识和操作一下mysql的基本命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【每日SQL打卡】
- 下一篇: 实验四51单片机并口实验