您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關python中mysql常見語法有哪些的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。
一、數(shù)據(jù)庫的命令
查看所有的數(shù)據(jù)庫:show databases; 創(chuàng)建一個叫test的數(shù)據(jù)庫:create database test; 刪除一個叫test的數(shù)據(jù)庫:drop database test; 選中庫 ,在建表之前必須要選擇數(shù)據(jù)庫:use test; 在選中的數(shù)據(jù)庫之中查看所有的表:show tables; 表名 (字段1 類型, 字段2 類型):create table ; 表名;查看所在的表的字段:desc ; 表名; 刪除表:drop table ; 庫名;查看創(chuàng)建庫的詳細信息:show create database; 表名; 查看創(chuàng)建表的詳細信息:show create table ;
二、數(shù)據(jù)表的命令
修改表名:ALTER TABLE 表名1 RENAME TO 表名2; 添加列:ALTER TABLE 表名 ADD COLUMN 列名 列類型(如:VARCHAR(30)); 刪除列:ALTER TABLE 表名 DROP COLUMN 列名; 選擇:SELECT * FROM 表名 WHERE 范圍; 插入:INSERT INTO 表名(字段名1,字段名2) VALUES(字段值1,字段值2); 刪除:DELETE FROM 表名 WHERE 范圍; 更新:UPDATE 表名 SET 字段名=字段值 WHERE 范圍; 查找:SELECT * FROM 表名 WHERE 字段名 LIKE ’% 字段值 %’ ; 排序:SELECT * FROM 表名 ORDER BY 字段名1,字段名2 [DESC]; 總數(shù):SELECT COUNT AS totalcount FROM 表名; 求和:SELECT SUM(字段值) AS sumvalue FROM 表名; 平均:SELECT AVG(字段名) AS avgvalue FROM 表名; 最大:SELECT MAX(字段名) AS maxvalue FROM 表名; 最?。篠ELECT MIN(字段名) AS minvalue FROM 表名;
三、數(shù)據(jù)命令
增加數(shù)據(jù) INSERT INTO n VALUES (1, 'tom', '23'), (2, 'john', '22'); INSERT INTO n SELECT * FROM n; # 把數(shù)據(jù)復制一遍重新插入 刪除數(shù)據(jù) DELETE FROM n WHERE id = 2; 更改數(shù)據(jù) UPDATE n SET name = 'tom' WHERE id = 2; 數(shù)據(jù)查找 SELECT * FROM n WHERE name LIKE '%h%'; 數(shù)據(jù)排序(反序) SELECT * FROM n ORDER BY name, id DESC ;
四、多表聯(lián)合查詢
1.內(nèi)連接 隱式內(nèi)連接 select username,name from user,goods where user,gid=gods,gid; 顯示內(nèi)連接 select username,from user inner join goods on user.gid=goods.gid; select * from user left join goods on user.gid=goods.gid; 2.外鏈接 左連接 包含所有的左邊表中的記錄以及右邊表中沒有和他匹配的記錄 右連接 select * from user where gid in(select gid from goods); select * from user right jOin goods on user.gid=goods.gid; 子嵌套查詢 數(shù)據(jù)聯(lián)合查詢 select * from user left join goods on user.gid=goods.gid union select * from user right join goods on user.gid=goods.gid; 兩個表同時更新 update user u, goods g set u.gid=12,g.price=1 where u.id=2 and u.gid=g.gid;
五、視圖命令
創(chuàng)建視圖 CREATE VIEW v AS SELECT id, name FROM n; CREATE VIEW v(id, name) AS SELECT id, name FROM n; 查看視圖(與表操作類似) SELECT * FROM v; DESC v; 查看創(chuàng)建視圖語句 SHOW CREATE VIEW v; 更改視圖 CREATE OR REPLACE VIEW v AS SELECT name, age FROM n; ALTER VIEW v AS SELECT name FROM n ; 刪除視圖 DROP VIEW IF EXISTS v;
五、用戶命令
增加用戶 CREATE USER 'test'@'localhost' IDENTIFIED BY 'test'; INSERT INTO mysql.user(Host, User, Password) VALUES ('localhost', 'test', Password('test')); # 在用戶表中插入用戶信息,直接操作User表不推薦 刪除用戶 DROP USER 'test'@'localhost' ; DELETE FROM mysql.user WHERE User='test' AND Host='localhost'; FLUSH PRIVILEGES ; 更改用戶密碼 SET PASSWORD FOR 'test'@'localhost' = PASSWORD('test'); UPDATE mysql.user SET Password=Password('t') WHERE User='test' AND Host='localhost'; FLUSH PRIVILEGES ; 用戶授權 GRANT ALL PRIVILEGES ON *.* TO test@localhost IDENTIFIED BY 'test'; 授予用'test'密碼登陸成功的 test@localhost 用戶操作所有數(shù)據(jù)庫的所有表的所有的權限 FLUSH PRIVILEGES ; # 刷新系統(tǒng)權限表,使授予權限生效 撤銷用戶授權 REVOKE DELETE ON *.* FROM 'test'@'localhost' ; # 取消該用戶的刪除權限
六:事物命令
mysql> use RUNOOB; Database changed mysql> CREATE TABLE runoob_transaction_test( id int(5)) engine=innodb; Query OK, 0 rows affected (0.04 sec) mysql> select * from runoob_transaction_test; Empty set (0.01 sec) mysql> begin; # 開始事務 Query OK, 0 rows affected (0.00 sec) mysql> insert into runoob_transaction_test value(5); Query OK, 1 rows affected (0.01 sec) mysql> insert into runoob_transaction_test value(6); Query OK, 1 rows affected (0.00 sec) mysql> commit; # 提交事務 Query OK, 0 rows affected (0.01 sec) mysql> select * from runoob_transaction_test; +------+ | id | +------+ | 5 | | 6 | +------+ 2 rows in set (0.01 sec) mysql> begin; # 開始事務 Query OK, 0 rows affected (0.00 sec) mysql> insert into runoob_transaction_test values(7); Query OK, 1 rows affected (0.00 sec) mysql> rollback; # 回滾 Query OK, 0 rows affected (0.00 sec) mysql> select * from runoob_transaction_test; # 因為回滾所以數(shù)據(jù)沒有插入 +------+ | id | +------+ | 5 | | 6 | +------+ 2 rows in set (0.01 sec)
七、備份與還原
數(shù)據(jù)庫備份 mysqldump -u root -p db_name > file.sql mysqldump -u root -p db_name table_name > file.sql 數(shù)據(jù)庫還原 mysql -u root -p < C:\file.sql
感謝各位的閱讀!關于python中mysql常見語法有哪些就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。