1.創建表Teacher:
create table Teacher(
teaId int not null,
teaname varchar(100),
age int,
sex enum('M', 'F'),
phone int);
注意:

創新互聯-成都網站建設公司,專注
成都網站設計、網站制作、網站營銷推廣,
域名申請,網絡空間,
網站托管有關企業網站制作方案、改版、費用等問題,請聯系創新互聯。
char 和varchar區別:
‘123’ ------>varchar(10) # 3位
'123 ' -------> char(10) # 10位 不足10位空格補全
查看新建的Teacher表:
MariaDB [(none)]> use test;
Database changed
MariaDB [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| Teacher |
+----------------+
1 row in set (0.00 sec)
2.建立超級用戶:
grant all privileges on *.* to 'fxq'@'%' identified by '123456' with grant option;
3.插入數據:
insert into Teacher(teaid,teaname,age) values(1,'feng',20);
插入多行數據:
insert into Teater(teaid,teaname,age) values(101,'fengxiaoqing',20),(102,'zhangsan',30),(103,'wangwu',40);
查看插入的數據:
MariaDB [test]> select * from Teacher;
+-------+--------------+------+------+------------+
| teaId | teaname | age | sex | phone |
+-------+--------------+------+------+------------+
| 1 | feng | 20 | NULL | NULL |
| 2 | wang | 20 | M | 2147483647 |
| 2 | wang | 20 | M | 2147483647 |
| 3 | zhang | 30 | M | 2147483647 |
| 4 | li | 40 | M | 2147483647 |
| 5 | zhao | 50 | F | 2147483647 |
| 5 | zhao | 50 | F | 1821113120 |
| 101 | fengxiaoqing | 20 | NULL | NULL |
| 102 | zhangsan | 30 | NULL | NULL |
| 103 | wangwu | 40 | NULL | NULL |
+-------+--------------+------+------+------------+
10 rows in set (0.00 sec)
MariaDB [test]>
4.數據查詢:
select * from Teacher where teaId > 4;
select * from Teacher where teaId in(1,4,101);
select * from Teacher where teaId like ('%1%');
測試結果:
MariaDB [test]> select * from Teacher where teaId > 3;
+-------+--------------+------+------+------------+
| teaId | teaname | age | sex | phone |
+-------+--------------+------+------+------------+
| 4 | li | 40 | M | 2147483647 |
| 5 | zhao | 50 | F | 2147483647 |
| 5 | zhao | 50 | F | 1821113120 |
| 101 | fengxiaoqing | 20 | NULL | NULL |
| 102 | zhangsan | 30 | NULL | NULL |
+-------+--------------+------+------+------------+
5 rows in set (0.00 sec)
MariaDB [test]> select * from Teacher where teaId in(1,4,101);
+-------+--------------+------+------+------------+
| teaId | teaname | age | sex | phone |
+-------+--------------+------+------+------------+
| 1 | feng | 20 | NULL | 188188188 |
| 4 | li | 40 | M | 2147483647 |
| 101 | fengxiaoqing | 20 | NULL | NULL |
+-------+--------------+------+------+------------+
3 rows in set (0.01 sec)
MariaDB [test]> select * from Teacher where teaId like ('%1%');
+-------+--------------+------+------+-----------+
| teaId | teaname | age | sex | phone |
+-------+--------------+------+------+-----------+
| 1 | feng | 20 | NULL | 188188188 |
| 101 | fengxiaoqing | 20 | NULL | NULL |
| 102 | zhangsan | 30 | NULL | NULL |
+-------+--------------+------+------+-----------+
3 rows in set (0.00 sec)
MariaDB [test]>
MariaDB [test]>
查詢重復數據:
select * from Teacher group by teaname having count(*) >1;
測試結果:
MariaDB [test]> select * from Teacher;
+-------+--------------+------+------+------------+
| teaId | teaname | age | sex | phone |
+-------+--------------+------+------+------------+
| 1 | feng | 20 | NULL | 188188188 |
| 2 | wang | 20 | M | 2147483647 |
| 2 | wang | 20 | M | 2147483647 |
| 3 | zhang | 30 | M | 2147483647 |
| 4 | li | 40 | M | 2147483647 |
| 5 | zhao | 50 | F | 2147483647 |
| 5 | zhao | 50 | F | 1821113120 |
| 101 | fengxiaoqing | 20 | NULL | NULL |
| 102 | zhangsan | 30 | NULL | NULL |
+-------+--------------+------+------+------------+
9 rows in set (0.01 sec)
MariaDB [test]> select * from Teacher group by teaname having count(*) >1;
+-------+---------+------+------+------------+
| teaId | teaname | age | sex | phone |
+-------+---------+------+------+------------+
| 2 | wang | 20 | M | 2147483647 |
| 5 | zhao | 50 | F | 2147483647 |
+-------+---------+------+------+------------+
2 rows in set (0.00 sec)
MariaDB [test]>
聯合查詢:
select * from a,c where a.id = c.組id
查看表結構:
show create Teacher\G;
desc Teacher;
測試結果 :
MariaDB [test]> show create table Teacher \G;
*************************** 1. row ***************************
Table: Teacher
Create Table: CREATE TABLE `Teacher` (
`teaId` int(11) NOT NULL,
`teaname` varchar(100) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`sex` enum('M','F') DEFAULT NULL,
`phone` int(11) DEFAULT NULL,
KEY `test_Teacher_teaId_teaname` (`teaId`,`teaname`)
) ENGINE=InnoDB DEFAULT CHARSET=latin11 row in set (0.00 sec)
ERROR: No query specified
MariaDB [test]>
MariaDB [test]> desc Teacher;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| teaId | int(11) | NO | MUL | NULL | |
| teaname | varchar(100) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| sex | enum('M','F') | YES | | NULL | |
| phone | int(11) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
MariaDB [test]>
5.刪除數據:
delete from Teacher where teaid='103' #刪除指定id數據truncate Teacher1; #清空數據drop table Teacher1; #刪除表
測試結果:
delete:刪除表中指定數據條目
MariaDB [test]> delete from Teacher where teaid='103';
Query OK, 1 row affected (0.02 sec)
MariaDB [test]> select * from Teacher;
+-------+--------------+------+------+------------+
| teaId | teaname | age | sex | phone |
+-------+--------------+------+------+------------+
| 1 | feng | 20 | NULL | NULL |
| 2 | wang | 20 | M | 2147483647 |
| 2 | wang | 20 | M | 2147483647 |
| 3 | zhang | 30 | M | 2147483647 |
| 4 | li | 40 | M | 2147483647 |
| 5 | zhao | 50 | F | 2147483647 |
| 5 | zhao | 50 | F | 1821113120 |
| 101 | fengxiaoqing | 20 | NULL | NULL |
| 102 | zhangsan | 30 | NULL | NULL |
+-------+--------------+------+------+------------+
9 rows in set (0.00 sec)
MariaDB [test]>
truncate: 清空表數據,表結構保留
MariaDB [test]> select * from Teacher1;
+-------+---------------+------+------+-------+
| teaId | teaname | age | sex | phone |
+-------+---------------+------+------+-------+
| 101 | fengxiaoqing | 20 | NULL | NULL |
| 102 | zhangsan | 30 | NULL | NULL |
| 103 | wangwu | 40 | NULL | NULL |
| 104 | fengxiaoqing1 | 20 | NULL | NULL |
| 105 | zhangsan2 | 30 | NULL | NULL |
| 106 | wangwu3 | 40 | NULL | NULL |
| 107 | fengxiaoqing4 | 20 | NULL | NULL |
| 108 | zhangsan5 | 30 | NULL | NULL |
| 109 | wangwu6 | 40 | NULL | NULL |
| 110 | fengxiaoqing7 | 20 | NULL | NULL |
| 111 | zhangsan8 | 30 | NULL | NULL |
| 112 | wangwu9 | 40 | NULL | NULL |
+-------+---------------+------+------+-------+
12 rows in set (0.00 sec)
MariaDB [test]> truncate Teacher1;
Query OK, 0 rows affected (0.02 sec)
MariaDB [test]> select * from Teacher1;
Empty set (0.00 sec)
MariaDB [test]>
drop: 刪除整個表
MariaDB [test]> drop table Teacher1;
Query OK, 0 rows affected (0.01 sec)
MariaDB [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| Teacher |
+----------------+
1 row in set (0.00 sec)
MariaDB [test]>
6.更新數據
update Teacher set phone=188188188 where teaId=1;
測試結果:
MariaDB [test]> select * from Teacher;
+-------+--------------+------+------+------------+
| teaId | teaname | age | sex | phone |
+-------+--------------+------+------+------------+
| 1 | feng | 20 | NULL | NULL |
| 2 | wang | 20 | M | 2147483647 |
| 2 | wang | 20 | M | 2147483647 |
| 3 | zhang | 30 | M | 2147483647 |
| 4 | li | 40 | M | 2147483647 |
| 5 | zhao | 50 | F | 2147483647 |
| 5 | zhao | 50 | F | 1821113120 |
| 101 | fengxiaoqing | 20 | NULL | NULL |
| 102 | zhangsan | 30 | NULL | NULL |
+-------+--------------+------+------+------------+
9 rows in set (0.01 sec)
MariaDB [test]> update Teacher set phone=188188188 where teaId=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [test]> select * from Teacher;
+-------+--------------+------+------+------------+
| teaId | teaname | age | sex | phone |
+-------+--------------+------+------+------------+
| 1 | feng | 20 | NULL | 188188188 |
| 2 | wang | 20 | M | 2147483647 |
| 2 | wang | 20 | M | 2147483647 |
| 3 | zhang | 30 | M | 2147483647 |
| 4 | li | 40 | M | 2147483647 |
| 5 | zhao | 50 | F | 2147483647 |
| 5 | zhao | 50 | F | 1821113120 |
| 101 | fengxiaoqing | 20 | NULL | NULL |
| 102 | zhangsan | 30 | NULL | NULL |
+-------+--------------+------+------+------------+
9 rows in set (0.00 sec)
MariaDB [test]>
7.創建索引:
create index 庫名_表名_列名1_列名2 (列名1,列名2);
create index test_Teacher_teaId_teaname (teaId,teaname);
查看表有無索引:
MariaDB [test]> show index from Teacher;
Empty set (0.01 sec)
創建索引:
MariaDB [test]> show index from Teacher; #查看Empty set (0.01 sec)
MariaDB [test]> alter table Teacher add index test_Teacher_teaId_teaname (teaId,teaname); #創建索引
Query OK, 0 rows affected (0.34 sec)
Records: 0 Duplicates: 0 Warnings:
0MariaDB [test]> show index from Teacher; #再查看,索引就會創建成功
+---------+------------+----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Teacher | 1 | test_Teacher_teaId_teaname | 1 | teaId | A | 9 | NULL | NULL | | BTREE | | |
| Teacher | 1 | test_Teacher_teaId_teaname | 2 | teaname | A | 9 | NULL | NULL | YES | BTREE | | |
+---------+------------+----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
MariaDB [test]>
查看是否走索引:
explain select * from Teacher where teaname = 'feng';
MariaDB [test]> explain select * from Teacher where teaname = 'feng';
+------+-------------+---------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+---------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | Teacher | ALL | NULL | NULL | NULL | NULL | 9 | Using where |
+------+-------------+---------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
MariaDB [test]> explain select * from Teacher where teaname = 'feng' and teaId = 1;
+------+-------------+---------+------+----------------------------+----------------------------+---------+-------------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+---------+------+----------------------------+----------------------------+---------+-------------+------+-----------------------+
| 1 | SIMPLE | Teacher | ref | test_Teacher_teaId_teaname | test_Teacher_teaId_teaname | 107 | const,const | 1 | Using index condition |
+------+-------------+---------+------+----------------------------+----------------------------+---------+-------------+------+-----------------------+
1 row in set (0.01 sec)
MariaDB [test]>
MariaDB [test]> select * from Teacher where teaname = 'feng' and teaId = 1;
+-------+---------+------+------+-----------+
| teaId | teaname | age | sex | phone |
+-------+---------+------+------+-----------+
| 1 | feng | 20 | NULL | 188188188 |
+-------+---------+------+------+-----------+
1 row in set (0.01 sec)
MariaDB [test]>
另外有需要云服務器可以了解下創新互聯cdcxhl.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
文章題目:Python中操作mysql知識(二)-創新互聯
網站路徑:http://www.2m8n56k.cn/article2/dgheic.html
成都網站建設公司_創新互聯,為您提供網站內鏈、小程序開發、Google、企業網站制作、App開發、企業建站
廣告
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:[email protected]。內容未經允許不得轉載,或轉載時需注明來源:
創新互聯