您好,登錄后才能下訂單哦!
本篇內容主要講解“MySQL管理數據庫、數據表、數據的基本操作是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“MySQL管理數據庫、數據表、數據的基本操作是什么”吧!
注意:MySQL數據庫命令不區分大小寫,但在Mac端如果想使用tab鍵自動補全命令,就必須使用大寫。
1、數據庫管理
1.1 create 創建數據庫
mysql> create database test; Query OK, 1 row affected (0.50 sec)
1.2 show 查看所有數據庫
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.32 sec)
1.3 alter 修改數據庫
alter命令修改數據庫編碼:
一般我們設置的字符集使用的都是UTF8,若發現數據庫編碼不是UTF8,可使用該語句更改數據庫編碼:
mysql> alter database test character set utf8; Query OK, 1 row affected (0.01 sec)
1.4 use 使用數據庫
mysql> use test; Database changed
1.5 查看當前使用的數據庫
mysql> select database(); +------------+ | database() | +------------+ | test | +------------+ 1 row in set (0.00 sec)
1.6 drop 刪除數據庫
mysql> drop database test; Query OK, 0 rows affected (0.00 sec)
2、數據庫表管理
我們先創建一個數據庫,以供我們后面使用:
mysql> create database test; Query OK, 1 row affected (0.06 sec)
創建后,使用use命令進入數據庫:
mysql> use test; Database changed
2.1 create 創建表
mysql> create table test(id int auto_increment primary key, -> name varchar(20) not null, -> age int not null, -> birthday datetime); Query OK, 0 rows affected (0.14 sec)
2.2 show 顯示表
mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | test | +----------------+ 1 row in set (0.00 sec)
2.3 desc 查看表結構
mysql> desc test; +----------+-------------+------+-----+---------+----------------+ | 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 修改表結構(增、刪、改)
2.4.1 add 在表中添加字段(列)
mysql> alter table test add star bool; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0
注意:在MySQL中布爾類型會自動轉變為tinyint(1)類型
用desc查看可得到:
mysql> desc test; +----------+-------------+------+-----+---------+----------------+ | 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 modify 修改表字段(列)
mysql> alter table test modify star int; Query OK, 0 rows affected (0.43 sec) Records: 0 Duplicates: 0 Warnings: 0
注意:int可以指定長度,也可以不指定,不指定時默認長度為int(11),修改后再次用desc查看:
mysql> desc test; +----------+-------------+------+-----+---------+----------------+ | 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 drop 刪除表字段(列)
mysql> alter table test drop column star; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0
查看表結構可以看到沒有star字段了:
mysql> desc test; +----------+-------------+------+-----+---------+----------------+ | 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)
2.4.4 rename 重命名表名
mysql> rename table test to people; Query OK, 0 rows affected (0.01 sec)
再次查看表格,發現表名已改變:
mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | people | +----------------+ 1 row in set (0.00 sec)
2.5 create 利用已有數據創建新表
mysql> create table newpeople select * from people; Query OK, 0 rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0
查看數據庫存在的表發現已經有了:
mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | newpeople | | people | +----------------+ 2 rows in set (0.00 sec)
3、數據的操作及管理
3.1 增加數據(增)
mysql> insert into people values(null,'Anny',22,'1990-09-09'); Query OK, 1 row affected (0.00 sec)
用select查詢:
mysql> select * from people; +----+------+-----+---------------------+ | id | name | age | birthday | +----+------+-----+---------------------+ | 1 | Anny | 22 | 1990-09-09 00:00:00 | +----+------+-----+---------------------+ 1 row in set (0.00 sec)
3.2 刪除數據(刪)
mysql> delete from people where name='Anny'; Query OK, 1 row affected (0.00 sec)
再用select查詢,發現沒有刪除的數據了:
mysql> select * from people; +----+------+-----+---------------------+ | id | name | age | birthday | +----+------+-----+---------------------+ | 2 | Lisa | 20 | 1992-09-09 00:00:00 | +----+------+-----+---------------------+ 1 row in set (0.00 sec)
3.3 修改數據(改)
mysql> update people set name='Anny' where name='Lisa'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
再查數據可以看到Lisa名字變為anny了:
mysql> select * from people; +----+------+-----+---------------------+ | id | name | age | birthday | +----+------+-----+---------------------+ | 2 | Anny | 20 | 1992-09-09 00:00:00 | +----+------+-----+---------------------+ 1 row in set (0.00 sec)
3.4 查詢數據(查)
最簡單的就是下面的語句:
mysql> select * from people; +----+------+-----+---------------------+ | id | name | age | birthday | +----+------+-----+---------------------+ | 2 | Anny | 20 | 1992-09-09 00:00:00 | +----+------+-----+---------------------+ 1 row in set (0.00 sec)
* 表示所有字段,也可以指定字段查詢,如下:
mysql> select name from people; +------+ | name | +------+ | Anny | +------+ 1 row in set (0.00 sec)
4、管理視圖
4.1 創建視圖
mysql> create view people_view(name,age) as select name,age from people; Query OK, 0 rows affected (0.00 sec)
創建成功后可以查看視圖:
mysql> select * from people_view; +------+-----+ | name | age | +------+-----+ | Anny | 20 | +------+-----+ 1 row in set (0.00 sec)
可以用desc 查看視圖字段:
mysql> desc people_view; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | name | varchar(20) | NO | | NULL | | | age | int(11) | NO | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
4.2 替換視圖
創建或替換原有視圖:
mysql> create or replace view people_view (id,name,age) as select id,name,age from people; Query OK, 0 rows affected (0.00 sec)
查看現有視圖:
mysql> select * from people_view; +----+------+-----+ | id | name | age | +----+------+-----+ | 2 | Anny | 20 | | 3 | Lisa | 22 | +----+------+-----+ 2 rows in set (0.00 sec)
4.3 操作視圖
當視圖有變化時,對應的表格也會跟著改變,即操作視圖就是操作表格,我們在視圖中插入一條數據:
mysql> insert into people_view values('Lisa',22); Query OK, 1 row affected (0.08 sec)
插入成功后,我們查看視圖數據:
mysql> select * from people_view; +------+-----+ | name | age | +------+-----+ | Anny | 20 | | Lisa | 22 | +------+-----+ 2 rows in set (0.00 sec)
可以看到視圖中已經有了,再查看我們表格數據:
mysql> select * from people; +----+------+-----+---------------------+ | id | name | age | birthday | +----+------+-----+---------------------+ | 2 | Anny | 20 | 1992-09-09 00:00:00 | | 3 | Lisa | 22 | NULL | +----+------+-----+---------------------+ 2 rows in set (0.00 sec)
發現也有了對應的數據
4.4 刪除視圖:
mysql> drop view people_view; Query OK, 0 rows affected (0.00 sec)
到此,相信大家對“MySQL管理數據庫、數據表、數據的基本操作是什么”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。