Python_MySQLデータベース操作
13954 ワード
データベース操作
一、データベースの作成
二、データベースの選択/切り替え
MySQLデータベースに接続すると、操作可能なデータベースが複数ある場合がありますので、操作するデータベースを選択する必要があります.表示されるDatabase changedは、データベースの選択が成功したことを示し、現在のデータベースの下にあるすべてのデータテーブルのデータを使用できます.次の操作はpython 21データベースで実行されます. データ を読み込むには、USEを使用してデータベースを開く必要があります(指定したデータベースを選択してください).すべてのデータベース名、テーブル名、テーブルフィールドは大文字と小文字を区別します.SQLコマンドを使用する場合は、正しい名前 を入力する必要があります.
三、データベースの削除
ライブラリを削除するのはリスクがあります.手を出すには慎重にしなければなりません.削除コマンドを実行すると、すべてのデータが消えます.
データテーブルアクション
一、データテーブルの作成
MySQLデータテーブルを作成するには、次の情報が必要です.表名 テーブルフィールド名 フィールドタイプ
注意:データテーブルを作成する場合、データベースが指定されている場合は、現在のデータベースの下にテーブルが新規作成されます.データベースが指定されていない場合は、データベースを指定する必要があります コンソールプロンプト類似:Query OK、0 rows affectedは作成成功 フィールドをNULLにしたくない場合は、フィールドの制約条件をNOT NULLに設定し、データベース操作時にそのフィールドに挿入されたデータが空の場合は、エラー PRIMARY KEY:プライマリ・キーの定義 AUTO_INCREMENT:フィールド値を自己増加として定義し、一般的にプライマリ・キーに使用され、数値は自動的に1 加算されます.CHARSET設定データテーブル符号化utf 8 二、建表文の表示
三、表構造の表示
四、データテーブルの表示
データベースを指定すると、現在のデータベースの下にあるすべてのデータテーブルのリストを表示できます.
テーブル構造の変更
汎用フォーマット
一、フィールドの追加
練習する
二、フィールドの修正
区別するchangeフィールド名、タイプおよび制約を同時に変更する場合に使用します.古いフィールドおよび新しいフィールド を指定する必要があります.modifyフィールド名は変更できません.フィールドタイプと制約のみを変更できます 練習する
三、フィールドの削除
練習する
四、表名の変更
五、表タイプの変更
MySQLデータベースのテーブル・タイプは、MyISAMとInnoDBの2種類で一般的に使用されます.
違い:MyISAMタイプのテーブルデータの削除速度が速く、トランザクションをサポートせず、InnoDBセキュリティがない InnoDBタイプのテーブルデータの添削速度はMyISAMほど速くないが、トランザクションをサポートし、比較的安全なMyISAMタイプのデータファイルは3つのfrm(構造)、MYD(データ)、MYI(インデックス)InnoDBタイプのデータファイルが1つしかない.frm(表構造).ibd(データインデックス) 六、データテーブルの削除
削除表は慎重に!
データ操作
一、データの追加
1、標準追加(すべてのフィールドを指定し、すべての値を指定する)
2、指定部分フィールドの追加
フィールド制約で空の値の入力が許可されている場合、データを挿入するときにこのようなフィールドを追加しなくてもいいです.
3、フィールドを指定せずに順番に値を追加する
に注意
フィールド名を指定せずにデータを挿入する場合、idフィールドがプライマリ・キーに制約され、自己増加した場合、id値はnull、または実際の番号を与えることができますが、挿入データの数とフィールドの数が一致することを保証する値が必要です.
4、一括挿入データ
二、クエリーデータSELECT文を使用してデータテーブルから1つ以上の列を取得 SELECTを使用してテーブルデータを検索するには、少なくとも2つの情報が必要です.どのデータを検索したいのか、どのテーブルから検索したいのか
注意:
作業中はselect*を使用せずにデータを照会
1、基礎クエリークエリー単一列 クエリー部分列または単一列
注意:クエリーされたデータはクエリー結果を明確にソートしておらず、データを返す順序には特に意味がありません.テーブルにデータが追加される順序であるか、そうでない可能性があります.同じ数のローを返すと、通常の 2、where条件照会WHERE句で任意の条件を指定できます ANDまたはORを使用して1つまたは複数の条件 を指定できます.WHERE句プログラム言語のif条件 に類似
3、LIKE句LIKE句では、正規表現のアスタリスク* と同様に、任意の0文字以上の文字をパーセント文字で表すパーセンテージ%が使用されていない場合、LIKE句は等号の効果と同じ 短い横線は任意の文字を表します:-
4、並べ替えMySQLのORDER BY句を使用して、どのフィールドでソートしたいかを設定し、ソート後のクエリー結果を返します 並べ替えに複数のフィールドを設定できる ASC(昇順)またはDESC(降順)キーを使用してソートルールを設定できます.デフォルトでは、昇順でソート
5、統計関数(集約関数) max() min() count() sum() avg()
6、グループ化GROUP BY文結果セットを1つ以上の列に基づいてグループ化 グループの列でCOUNT,SUM,AVG,等関数 を使用できます.
7、部分データの照会
練習する
三、データの修正
練習する
四、データの削除
1、DELETE削除
練習する
2、TRUNCATE削除
DELETEとTRUNCATEの違い
1つ目:
2番目:
3番目
4番目
データベースのコミット・モードの表示
一、データベースの作成
CREATE DATABASE DEFAULT CHARSET=UTF8;
mysql> create DATABASE python21 default charset=utf8;
二、データベースの選択/切り替え
MySQLデータベースに接続すると、操作可能なデータベースが複数ある場合がありますので、操作するデータベースを選択する必要があります.
mysql> USE python21;
Database changed
三、データベースの削除
ライブラリを削除するのはリスクがあります.手を出すには慎重にしなければなりません.削除コマンドを実行すると、すべてのデータが消えます.
DROP DATABASE ;
# Python21
mysql> DROP DATABASE python21;
データテーブルアクション
一、データテーブルの作成
MySQLデータテーブルを作成するには、次の情報が必要です.
CREATE TABLE (
[ ],
[ ],
[ ]
...
)DEFAULT CHARSET=UTF8;
:
-- student , : , , , 1 , ,
create table student(
id int unsigned not null primary key auto_increment,
name varchar(4) not null,
age int,
sex int default 1 not null,
tel varchar(20),
adress varchar(255)
)default charset=utf8;
-- baixing , : , , , ,
create table baixing(
id int not null primary key auto_increment,
title varchar(50),
update_time varchar(50),
see_count varchar(10),
company varchar(50)
)default charset=utf8;
-- user , : , , , ,
create table user(
id int not null primary key auto_increment,
name varchar(10) not null,
email varchar(20),
sex varchar(3),
tel varchar(20),
adress text
)default charset=utf8;
注意:
SHOW CREATE TABLE \G;
SHOW CREATE TABLE
三、表構造の表示
DESC ;
四、データテーブルの表示
データベースを指定すると、現在のデータベースの下にあるすべてのデータテーブルのリストを表示できます.
mysql> SHOW TABLES;
テーブル構造の変更
汎用フォーマット
ALTER TABLE ACTION( );
一、フィールドの追加
alter table add ; ( )
alter table add after ;
練習する
# user num int not null
alter table user add num int not null;
# user email age , int not null default 20;
alter table user add age int not null default 20 after email;
# user aa int
alter table user add aa int not null first;
二、フィールドの修正
ALTER TABLE CHANGE
ALTER TABLE MODIFY
区別する
# user age tinyint、 、 20
alter table user modify age tinyint unsigned not null default 20;
# user age nianling int, 99
alter table user change num mm int not null default 10;
三、フィールドの削除
ALTER TABLE DROP
練習する
# user aa
alter table user drop aa;
四、表名の変更
ALTER TABLE RENAME AS
五、表タイプの変更
MySQLデータベースのテーブル・タイプは、MyISAMとInnoDBの2種類で一般的に使用されます.
ALTER TABLE ENGINE="InnoDB"
違い:
削除表は慎重に!
DROP TABLE ;
kong
DROP TABLE kong;
データ操作
一、データの追加
INSERT INTO (field1, field2,...fieldN)VALUES (value1, value2,...valueN);
1、標準追加(すべてのフィールドを指定し、すべての値を指定する)
insert into student(id,name,age,sex,adress,tel) values(1,' ',20,' ',' ','110');
2、指定部分フィールドの追加
フィールド制約で空の値の入力が許可されている場合、データを挿入するときにこのようなフィールドを追加しなくてもいいです.
insert into student(name,adress) values(' ',' ');
insert into student(name,age,sex,adress) values(' ','32',' ',' ');
3、フィールドを指定せずに順番に値を追加する
insert into student values(null,' ',21,' ',' ','114');
に注意
フィールド名を指定せずにデータを挿入する場合、idフィールドがプライマリ・キーに制約され、自己増加した場合、id値はnull、または実際の番号を与えることができますが、挿入データの数とフィールドの数が一致することを保証する値が必要です.
4、一括挿入データ
insert into student (name,age,sex,adress,tel) values
(' ',12,' ',' ','120'),
(' 1',12,' ',' 1','119'),
(' 2',12,' ',' ','120'),
(' 3',12,' ',' ','120'),
(' 4',12,' ',' ','120'),
(' 5',12,' ',' ','120');
insert into users(username,pssword,sex,age) values
(' ','123456','1','25'),
(' ','123456','1','30'),
(' ','123456','1','38'),
(' ','123456','0','16'),
(' ','123456','0','40'),
(' ','123456','0','60');
二、クエリーデータ
SELECT * FROM ;
SELECT 1, 2 FROM ;
注意:
作業中はselect*を使用せずにデータを照会
1、基礎クエリー
SELECT * FROM ;
+----+----------+-----+-----+---------+
| id | name | age | sex | classid |
+----+----------+-----+-----+---------+
| 1 | zhangsan | 20 | m | lamp138 |
| 2 | lisi | 20 | m | lamp138 |
| 3 | wangwu | 21 | w | lamp138 |
| 4 | zhaoliu | 25 | w | lamp94 |
| 5 | uu01 | 26 | m | lamp94 |
| 6 | uu02 | 28 | w | lamp92 |
| 7 | qq02 | 24 | m | lamp92 |
| 8 | uu03 | 32 | m | lamp138 |
| 9 | qq03 | 23 | w | lamp94 |
| 10 | aa | 19 | m | lamp138 |
| 11 | sad | 35 | m | lamp94 |
| 12 | tt | 25 | m | lamp92 |
| 13 | wer | 25 | w | lamp94 |
| 14 | xx | 25 | m | lamp92 |
| 15 | kk | 0 | w | lamp94 |
+----+----------+-----+-----+---------+
15 rows in set (0.00 sec)
SELECT 1, 2,... FROM ;
SELECT name, age FROM user;
SELECT name FROM user;
+----+----------+------
| id | name | age |
+----+----------+------
| 1 | zhangsan | 20 |
| 2 | lisi | 20 |
| 3 | wangwu | 21 |
| 4 | zhaoliu | 25 |
| 5 | uu01 | 26 |
| 6 | uu02 | 28 |
| 7 | qq02 | 24 |
| 8 | uu03 | 32 |
+----+----------+------
8 rows in set (0.00 sec)
注意:
# python03
select * from class where class_name='python03';
# age 12
select * from stu where age=12 and sex=' ';
# id 5
select * from stu where id>5;
# 20 25
select * from stu where age>=20 and age<=25;
select * from stu where age between 20 and 25;
# 20 25
select * from stu where age not between 20 and 25;
select * from stu where age<20 or age>25;
# id 1,3,5,7,9
select * from stu where id in (1,3,5,7,9);
select * from stu where id=1 or id=3 or id=5 or id=7 or id=9;
# lamp138 lamp94
select * from stu where classid in('lamp138','lamp94') and sex='w';
select * from stu where (classid='lamp138' or classid='lamp94') and sex='w
select [ ] from
[where ]
[group by ]
[order by ]
[limit ]
3、LIKE句
-- name zh
select * from stu where name like "zh%";
-- name
select * from users where username like ' %';
-- name ang
select * from stu where name like "%ang%";
-- name i
select * from users where username like '%i%';
-- 。
select * from stu where name like "____";
-- 2 。
select * from users where username like '__';
4、並べ替え
SELECT * FROM ORDER BY
--
mysql> select * from stu order by age;
-- asc
mysql> select * from stu order by age asc;
--
mysql> select * from stu order by age desc;
-- , ,
mysql> select * from stu order by classid asc,age desc;
5、統計関数(集約関数)
COUNT(*)
-- 、 ?
mysql> select max(age),min(age),avg(age) from stu;
--
select count(id) from users;
-- m
select count(*) from users where sex='1';
6、グループ化
-- , , ;
MySQL> select sex,count(*) from stu group by sex;
--
MySQL> select classid,count(*) from stu group by classid;
-- , 。
MySQL> select classid,sex,count(*) from stu group by classid,sex;
7、部分データの照会
SELECT * FROM LIMIT num; m
SELECT * FROM LIMIT m,n; m , n
-- 5
mysql> select * from stu limit 5;
-- 2 4
mysql> select * from stu limit 2,4;( 0 )
-- 4 ,
mysql> select * from stu limit 0,4;
-- 4 , 。
mysql> select * from stu limit 4,4;
-- 4 , 。
mysql> select * from stu limit 8,4;
-- 4 , 。
mysql> select * from stu limit 12,4;
:.... ( -1)* , ;
練習する
--
mysql> select count(id) from
-- 5 ?
mysql> select * from stu order by age desc limit 5;
-- , , 3 。
mysql> select classid,count(*) num from stu group by classid order by num desc limit 3;
三、データの修正
--
UPDATE SET 1= 1, 2= 2, n= n...;
--
UPDATE SET 1= 1, 2= 2, n= n... WHERE ;
練習する
-- id 11 age 35,sex m
mysql> update stu set age=35,sex='m' where id=11;
-- id 12 14 sex m,classid lamp92
mysql> update stu set sex='m',classid='lamp92' where id=12 or id=14
--
mysql> update stu set sex='m',classid='lamp92' where id in(12,14);
-- id 1,2,3
mysql> update users set age='20' where id=1 or id=2 or id=3;
-- id 11 age 35,sex 0
update users set age="35",sex='0' where id=11;
-- id 6 9 sex 0,email [email protected]
update users set sex='0',email='[email protected]' where id=6 or id=9;
-- 20 , 747474
update users set pssword='74747' where username=" " and age=20;
-- 20 abc
update users set pssword='abc' where age
四、データの削除
1、DELETE削除
--
DELETE FROM ;
--
DELETE FROM [WHERE ]
練習する
-- stu id 100
mysql> delete from stu where id=100;
-- stu id 20 30
mysql> delete from stu where id>=20 and id<=30;
-- stu id 20 30
mysql> delete from stu where id between 20 and 30;
-- stu id 200
mysql> delete from stu where id>200;
2、TRUNCATE削除
TRUNCATE TABLE ;
-- class
truncate table class:
DELETEとTRUNCATEの違い
1つ目:
1、delete: ( where ), , where , ;
2、trancete: ,
2番目:
1、 (auto_increment) , delete , , , , 1 ,delete , (auto_increment) 1
2、truncate , 1
3、delete , ;trancate ,
3番目
1、 truncate delete ,delete mysql , truncate mysql , , 。
4番目
1、delete (dml),
2、truncate (ddl), , , ,
データベースのコミット・モードの表示
show variables like 'autocommit'\G