Database Management System:關係資料庫管理系統)應用軟體之一
最早MySQL數據,瑞典AB公司開發的一款開源型的關係型資料庫。隨著時間的推移,瑞典AB公司把MySQL資料庫轉讓給Sun公司(Java語言的創始公司)後來,Sun公司經營不善,又把MySQL資料庫轉讓給甲骨文公司(Oracle資料庫)。MySQL 是一種關聯資料庫管理系統,關聯資料庫將數據保存在不同的表中,而不是將所有數據放在一個大倉庫內,這樣就增加了速度並提高了靈活性。一:資料庫基本操作命令1.1:mysql查看資料庫結構1.2:查看資料庫信息mysql> show databases;
++
| Database |
++
| information_schema |
| mysql |
| performance_schema |
| sys |
++
4 rows in set (0.00 sec)
1.3:查看資料庫中的數據表信息mysql> use mysql; '進入mysql資料庫中'
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+--+
| Tables_in_mysql |
+--+
| columns_priv |
| db |
| engine_cost |
| event |
...省略內容
1.4:顯示數據表的結構(欄位)mysql> describe user; '也可以使用desc user;'
+----++-++---+--+
| Field | Type | Null | Key | Default | Extra |
+----++-++---+--+
| Host | char(60) | NO | PRI | | |
二:SQL操作管理命令2.1:SQL語言概述是Structured Query Language的縮寫,及結構化查詢語言用於維護管理資料庫,如數據查詢,數據更新,訪問控制,對象管理等功能2.2:DDL操作命令2.2.1:創建資料庫和表2.2.2:DDL語句創建庫、表的命令創建資料庫:create database 資料庫名創建數據表:create table 表名(欄位定義…)mysql> create database school;
Query OK, 1 row affected (0.00 sec)
mysql> use school;
Database changedmysql> create table info (id int(3) not null primary key auto_increment,name varchar(10) not null,score decimal(5,2),address varchar(50) default '未知');
Query OK, 0 rows affected (0.02 sec)
'info表名','not null設置不為空','primary key主鍵','auto_increment自動增長'
2.2.3:DDL語句刪除庫,表的命令刪除指定的數據表:drop table [資料庫名.]表名
刪除指定的資料庫:drop database 資料庫名mysql> drop table info; '刪除表'
Query OK, 0 rows affected (0.00 sec)
mysql> drop database shcool; '刪除庫'
Query OK, 0 rows affected (0.00 sec)
2.3:DML操作命令2.3.1:DML語句的作用2.3.2:向數據表中插入新的數據記錄命令mysql> use school '進入school資料庫'
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from info; '查看表中數據'
Empty set (0.00 sec)
mysql> show tables; '查看表'
+---+
| Tables_in_school |
+---+
| info |
+---+
1 row in set (0.00 sec)
mysql> desc info; '查看表結構'
+----+----+-++----+-+
| Field | Type | Null | Key | Default | Extra |
+----+----+-++----+-+
| id | int(3) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| address | varchar(50) | YES | | 未知 | |
+----+----+-++----+-+
4 rows in set (0.00 sec)
mysql> insert into info (id,name,score,address) values (1,'zhangsan',88.5,'nanjing'); '插入新的數據記錄'
Query OK, 1 row affected (0.00 sec)
mysql> select * from info;
+----++--+----+
| id | name | score | address |
+----++--+----+
| 1 | zhangsan | 88.50 | nanjing |
+----++--+----+
1 row in set (0.00 sec)
mysql> insert into info values (2,'lisi',90,'beijing'),(3,'wangwu',77,'hangzhou');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
'如果不加欄位名,默認是所有欄位,並且是按照從左往右'
mysql> select * from info;
+----++--++
| id | name | score | address |
+----++--++
| 1 | zhangsan | 88.50 | nanjing |
| 2 | lisi | 90.00 | beijing |
| 3 | wangwu | 77.00 | hangzhou |
+----++--++
3 rows in set (0.00 sec)
mysql> select * from info where score > 80; '篩選出80分以上的數據'
+----++--+----+
| id | name | score | address |
+----++--+----+
| 1 | zhangsan | 88.50 | nanjing |
| 2 | lisi | 90.00 | beijing |
+----++--+----+
2 rows in set (0.00 sec)
mysql> create table tmp as select * from info where score > 80; '篩選出80分以上的並導入進新的表中'
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
2.3.3:向表中添加欄位mysql> alter table sibiao add (id int(3) not null primary key auto_increment);
2.3.4:修改,更新數據表中的數據記錄的命令update 表名 set 欄位名 1=值1[,欄位名2=值2] where條件表達式mysql> update info set score=66 where name='zhangsan'; '修改張三的分數為66'
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from info;
+----++--++
| id | name | score | address |
+----++--++
| 1 | zhangsan | 66.00 | nanjing |
| 2 | lisi | 90.00 | beijing |
| 3 | wangwu | 77.00 | hangzhou |
+----++--++
3 rows in set (0.00 sec)
mysql> update info set name='zhaoliu'; '修改所有id的name'
2.3.5:在數據表中刪除指定的數據記錄命令delete from 表名 where 條件表達式mysql> select * from info;
+----++--++
| id | name | score | address |
+----++--++
| 1 | zhangsan | 66.00 | nanjing |
| 2 | lisi | 90.00 | beijing |
| 3 | wangwu | 77.00 | hangzhou |
+----++--++
3 rows in set (0.00 sec)
mysql> delete from info where score >= 90;
Query OK, 1 row affected (0.00 sec)
mysql> select * from info;
+----++--++
| id | name | score | address |
+----++--++
| 1 | zhangsan | 66.00 | nanjing |
| 3 | wangwu | 77.00 | hangzhou |
+----++--++
2 rows in set (0.00 sec)
不帶where條件的語句表示刪除表中所有記錄(高危操作)mysql> delete from info;
Query OK, 4 rows affected (0.00 sec)
mysql> select * from info;
Empty set (0.00 sec)
2.3.6:查看表結構命令mysql> desc info;
+----+----+-++----+-+
| Field | Type | Null | Key | Default | Extra |
+----+----+-++----+-+
| id | int(3) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| address | varchar(50) | YES | | 未知 | |
+----+----+-++----+-+
4 rows in set (0.00 sec)
2.2.7:修改用戶登錄mysql的密碼mysql> update mysql.user set authentication_string=password('123456') where user='root';
[root@localhost ~]# vim /etc/my.cnf
skip-grant-tables '在[mysqld]下添加'
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# mysql -uroot -p '輸入密碼123456'
2.4:DQL操作命令2.4.1:DQL語句的作用2.4.2:不指定條件查詢命令mysql> select name from info;
++
| name |
++
| zhangsan |
| wangwu |
++
2 rows in set (0.00 sec)
mysql> select name,score from info;
++--+
| name | score |
++--+
| zhangsan | 66.00 |
| wangwu | 77.00 |
++--+
2 rows in set (0.00 sec)
2.4.3:指定條件查詢的命令select欄位名1,欄位名2…from表名 WHERE 條件表達式mysql> select name from info where id=1 or id=3;
++
| name |
++
| zhangsan |
| wangwu |
++
2 rows in set (0.00 sec)
2.5:DCL操作命令2.5.1:DCL語句的作用2.5.2:設置用戶權限的命令grant 權限列表 on 資料庫名.表名 to 用戶名@來源地址 identified by 『密碼′mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123' with grant option;
'all privileges:所有權限,%:所有終端'
Query OK, 0 rows affected, 1 warning (0.00 sec)
2.5.3:查看用戶權限的命令mysql> show grants;
+----+
| Grants for root@localhost |
+----+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+----+
2 rows in set (0.00 sec)
mysql> show grants for 'root'@'localhost';
+----+
| Grants for root@localhost |
+----+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+----+
2 rows in set (0.00 sec)
2.5.4:撤銷用戶權限的命令revoke 權限列表 on 資料庫名.表名 from 用戶名@來源地址mysql> show grants;
+----+
| Grants for root@localhost |
+----+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+----+
2 rows in set (0.00 sec)
mysql> revoke all on *.* from 'root'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for 'root'@'localhost';
+--+
| Grants for root@localhost |
+--+
| GRANT USAGE ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+--+
2 rows in set (0.00 sec)
三:資料庫高級操作3.1:清空表mysql> delete from tmp;
Query OK, 1 row affected (0.00 sec)
mysql> select * from tmp;
Empty set (0.00 sec)
3.2:臨時表臨時建立的表,用於保存一些臨時數據,不會長期存在(連接斷開,臨時表被刪除)
mysql> create temporary table temp_info (id int(4) not null auto_increment,name varchar(10) not null,hobbby varchar(10) not null,primary key(id))engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> desc temp_info;
+--+---+-++----+-+
| Field | Type | Null | Key | Default | Extra |
+--+---+-++----+-+
| id | int(4) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | | NULL | |
| hobby | varchar(10) | NO | | NULL | |
+--+---+-++----+-+
3 rows in set (0.00 sec)
mysql> show tables; '沒有看到臨時表,因為是放在內存當中'
+---+
| Tables_in_school |
+---+
| info |
| tmp |
+---+
2 rows in set (0.00 sec)
'退出,重新進,臨時表沒有了'
3.3:克隆表mysql> create table kelong as select * from info; '克隆表結構和數據'
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> show tables;
+---+
| Tables_in_school |
+---+
| info |
| kelong |
| tmp |
+---+
3 rows in set (0.00 sec)從表完整複製結構生成新表,再導入數據
mysql> create table k like info; '相比較like不能克隆表中的數據'
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+---+
| Tables_in_school |
+---+
| info |
| k |
| kelong |
| tmp |
+---+
4 rows in set (0.00 sec)
mysql> select * from k;
Empty set (0.00 sec)
mysql> insert into k select * from info;
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from k;
+----++--++
| id | name | score | address |
+----++--++
| 1 | zhangsan | 66.00 | nanjing |
| 3 | wangwu | 77.00 | hangzhou |
+----++--++
2 rows in set (0.00 sec)mysql> show create table info\G;
*************************** 1. row ***************************
Table: info
Create Table: CREATE TABLE "info" (
"id" int(3) NOT NULL AUTO_INCREMENT,
"name" varchar(10) NOT NULL,
"score" decimal(5,2) DEFAULT NULL,
"address" varchar(50) DEFAULT '未知',
PRIMARY KEY ("id")
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
ERROR:
No query specified
mysql> create table test (id int(3) not null primary key auto_increment,name varchar(10) not null,score decimal(5,2),address varchar(50) default '未知');
Query OK, 0 rows affected (0.01 sec)
mysql> insert into test select * from info;
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
'先查看info表完整結構,根據此結構創建名字不同結構相同的表test,再導入數據'