mysqlの簡単な基本操作
15433 ワード
インストール
デフォルトでインストールされているパスワードの取得
サービスの開始
サービスの停止
サービスの再起動
データベースのリモート接続を許可
1.mysqlプロファイルを見つけて変更する
2.mysqlにログインし、コマンドを実行
3.mysqlを再起動する
mysqlに含まれるデータ型は多く、ここでは主によく使われるいくつかの種類を列挙します.数字:int,decimal 文字列:varchar,text 日付:datetime ブール:bit 拘束プライマリ・キーprimary key 非空not null 唯一unique デフォルトdefault 外部キーforeign key コマンドを使用したデータベースの接続
バージョンの表示:select version();
データベースの作成
データベースの削除
データベースの切り替え
現在選択されているデータベースの表示
テーブルアクション
現在のデータベース内のすべてのテーブルを表示
テーブルの作成
auto_incrementは自動成長を表す
テーブル構造の表示
テーブルの変更
テーブルの削除
テーブル名の変更
テーブルの作成文の表示
データ操作
検索
ふえる主キー列は自動的に増加するが、全列挿入時に占有が必要であり、通常は0を用いる、挿入に成功した後は実際のデータを基準とする である.
変更
削除
論理削除、本質は操作updateを修正することです
バックアップとリカバリ
データバックアップ
スーパーアドミニストレータへ
mysqlライブラリディレクトリへ
mysqldumpコマンドの実行
データ・リカバリ
mysqkに接続してデータベースを作成する
接続を終了し、次のコマンドを実行します.
詳細クエリー
基本クエリー
重複行の除去(distinctは同じ行を比較)
条件
比較演算子= より大きい> 以上>= 未満< 以下<= は等しくありません!=または<> クエリー番号が3より大きい学生
論理演算子 and or not
ファジイクエリ like %は任意の複数の任意の文字を表す . _任意の文字を表す
範囲クエリー inは、1つの非連続範囲における を表す. between ... and ...1つの連続する範囲内で を表す.
くうだんてい注意:nullと''は異なる 判空is null
優先度小かっこ、not、比較演算子、論理演算子 and比or先演算、同時に出現して先算orを望む場合は、結合() を使用する必要がある.
じゅうごう
統計を迅速に取得するために、5つの集約関数を提供します.
count(*)は合計行数を計算し、カッコに星と列名を書き、結果は同じです.
max(列)はこの列の最大値を求めることを表します
min(列)はこの列の最小値を求めることを表す
sum(列)はこの列の和を求める
AVg(列)はこの列の平均値を求めることを表す
グループ化
フィールドのグループ化によって、このフィールドの同じデータが1つのグループにグループ化されることを示すと、同じデータ列しかクエリーできません.差のあるデータ列は結果セットに表示されません.グループ化されたデータを統計し、集約演算を行うことができます.
グループ化されたデータフィルタ
whereとhavingの比較
whereはfromの後に指定したテーブルをデータフィルタリングし、元のデータのフィルタリングに属するhavingはgroupbyの結果をフィルタリングする
ツールバーの
行の一部を取得(ページング)
完全なsql文
データベースの詳細
関係
外部キー
カスケード・アクションのタイプは次のとおりです. restrict(制限):デフォルト値、放出異常 cascade(カスケード):プライマリ・テーブルのレコードが削除すると、テーブルから関連するレコードは 削除されます. set null:外部キーを空の に設定 no action:何もしない せつぞく
接続クエリーは次のように分類されます.表A inner join表B:表Aと表Bが一致する行が結果に現れる 表A left join表B:表Aと表Bが一致する行が結果に表示され、表Aに独自のデータが加算され、対応しないデータはnullで が埋め込まれる.表A right join表B:表Aと表Bが一致する行が結果に表示されます.表Bに独自のデータが加算されます.対応していないデータはnullを使用してクエリーや条件に「表名.列名」を推奨する構文です.複数の表の列名が重複しない場合は「表名」を省略できます.部分的に、テーブルの名前が長すぎる場合は、テーブル名の後に「as簡写名」または「簡写名」を使用して、テーブルに一時的な簡写名 を付けることができます.
自己参照
表示
取引 1 1つのビジネスロジックが複数のsqlの完了を必要とする場合、あるsql文が間違っている場合は、操作全体を に戻す必要があります.トランザクションを使用して返品機能を完了し、ビジネスロジックの正確性を保証するトランザクションの4つの特性(ACIDと略称) 2.1原子間性(Atomicity):トランザクション内のすべての操作は、データベース内で分割できないか、すべて完了するか、 を実行しないかのいずれかです.2-2コンシステンシ(Consistency):いくつかの並列に実行されるトランザクションで、その実行結果は、ある順序でシリアルに実行された結果と一致する必要があります. -3独立性(Isolation):トランザクションの実行は他のトランザクションに干渉されず、トランザクションの実行の中間結果は他のトランザクションに対して透明である必要があります. 2-4永続性(Durability):コミットされたトランザクションのいずれかについて、システムは、データベースに障害が発生した場合でも、トランザクションのデータベースへの変更が失われないことを保証する必要があります.
要件:トランザクション を使用するには、テーブルのタイプがinnodbまたはbdbタイプである必要があります.
トランザクション文
索引
sudo apt-get install mysql-server mysql-client
デフォルトでインストールされているパスワードの取得
sudo grep mysql_root_passwd /root/env.txt
サービスの開始
service mysql start
サービスの停止
service mysql stop
サービスの再起動
service mysql restart
データベースのリモート接続を許可
1.mysqlプロファイルを見つけて変更する
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
bind-address=127.0.0.1
2.mysqlにログインし、コマンドを実行
grant all privileges on *.* to 'root'@'%' identified by 'mysql' with grant option;
flush privileges;
3.mysqlを再起動する
mysqlに含まれるデータ型は多く、ここでは主によく使われるいくつかの種類を列挙します.
mysql -uroot -p
バージョンの表示:select version();
データベースの作成
mysql> create database charset=utf8;
データベースの削除
mysql> drop database ;
データベースの切り替え
mysql> use ;
現在選択されているデータベースの表示
mysql> show databases;
テーブルアクション
現在のデータベース内のすべてのテーブルを表示
mysql> show tables;
テーブルの作成
auto_incrementは自動成長を表す
create table ( ); :
mysql> create table students( id int auto_increment primary key not null, name varchar(10) not null, gender bit default 1, birthday datetime);
テーブル構造の表示
desc ; :
mysql> desc students;
テーブルの変更
alter table add|change|drop ;
:
mysql> alter table students add isDelete bit default 0;
テーブルの削除
drop table ;
テーブル名の変更
rename table to ;
テーブルの作成文の表示
show create table ;
:mysql> show create table students;
データ操作
検索
select * from
ふえる
:insert into values(...)
: mysql> insert into students values(0,' ',1,'1990-7-7',0);
:insert into ( 1,...) values( 1,...)
:mysql> insert into students(name) values(' ');
mysql> insert into students(gender,name) values(0,' ');
:insert into values(...),(...)...;
:mysql> insert into students(name) values(' '),(' ');
insert into ( 1,...) values( 1,...),( 1,...)...;
変更
update set 1= 1,... where
:mysql> update students set birthday='1990-2-2' where id=2;
mysql> update students set gender=0,birthday='2017-9-21' where id=4;
削除
delete from where
:mysql> delete from students where id=5;
truncate table ( id 1 )
論理削除、本質は操作updateを修正することです
update students isdelete=1 where ...;
: mysql> update students set isDelete= 1 where id=4;
mysql> select * from students where isDelete=0;
バックアップとリカバリ
データバックアップ
スーパーアドミニストレータへ
sudo -s
mysqlライブラリディレクトリへ
cd /var/lib/mysql
mysqldumpコマンドの実行
mysqldump –uroot –p > ~/Desktop/ .sql;
mysql
: root@ubuntu:/var/lib/mysql# mysqldump -uroot -p python3 > ~/Desktop/bak.sql
データ・リカバリ
mysqkに接続してデータベースを作成する
接続を終了し、次のコマンドを実行します.
mysql -uroot –p < ~/Desktop/ .sql
mysql
:lin@ubuntu:~/Desktop$ mysql -uroot -p py31 < bak.sql
詳細クエリー
基本クエリー
select * from
:mysql> select * from students;
:
mysql> select id,name from students;
+----+-----------+
| id | name |
+----+-----------+
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
+----+-----------+
5 rows in set (0.01 sec)
重複行の除去(distinctは同じ行を比較)
mysql> select distinct name from students;
+-----------+
| name |
+-----------+
| |
| |
| |
| |
+-----------+
4 rows in set (0.00 sec)
mysql> select distinct id,name from students;
+----+-----------+
| id | name |
+----+-----------+
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
+----+-----------+
5 rows in set (0.00 sec)
条件
select * from where ; ( true )
比較演算子
mysql> select id,name from students where id<3;
+----+-----------+
| id | name |
+----+-----------+
| 1 | |
| 2 | |
+----+-----------+
2 rows in set (0.02 sec)
論理演算子
mysql> select id,name from students where id<3 and gender=0;
+----+-----------+
| id | name |
+----+-----------+
| 2 | |
+----+-----------+
1 row in set (0.00 sec)
ファジイクエリ
mysql> select id,name from students;
+----+-----------+
| id | name |
+----+-----------+
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 6 | |
| 7 | |
+----+-----------+
6 rows in set (0.00 sec)
mysql> select id,name from students where name like ' %';
+----+-----------+
| id | name |
+----+-----------+
| 1 | |
| 6 | |
| 7 | |
+----+-----------+
3 rows in set (0.00 sec)
mysql> select id,name from students where name like ' _';
+----+--------+
| id | name |
+----+--------+
| 1 | |
| 7 | |
+----+--------+
2 rows in set (0.00 sec)
範囲クエリー
mysql> select id,name from students where id in(1,3,6);
+----+-----------+
| id | name |
+----+-----------+
| 1 | |
| 3 | |
| 6 | |
+----+-----------+
3 rows in set (0.00 sec)
mysql> select id,name from students where id between 2 and 4;
+----+-----------+
| id | name |
+----+-----------+
| 2 | |
| 3 | |
| 4 | |
+----+-----------+
3 rows in set (0.00 sec)
くうだんてい
mysql> select id,name,birthday from students where birthday is null;
+----+-----------+----------+
| id | name | birthday |
+----+-----------+----------+
| 2 | | NULL |
| 3 | | NULL |
| 4 | | NULL |
+----+-----------+----------+
3 rows in set (0.00 sec)
mysql> select id,name,birthday from students where birthday is not null;
+----+-----------+---------------------+
| id | name | birthday |
+----+-----------+---------------------+
| 1 | | 1990-01-01 00:00:00 |
| 6 | | 1990-01-02 00:00:00 |
| 7 | | 1990-01-02 00:00:00 |
+----+-----------+---------------------+
3 rows in set (0.00 sec)
優先度
じゅうごう
統計を迅速に取得するために、5つの集約関数を提供します.
count(*)は合計行数を計算し、カッコに星と列名を書き、結果は同じです.
mysql> select count(*) from students;
+----------+
| count(*) |
+----------+
| 6 |
+----------+
1 row in set (0.01 sec)
max(列)はこの列の最大値を求めることを表します
mysql> select max(id) from students where gender=0;
+---------+
| max(id) |
+---------+
| 7 |
+---------+
1 row in set (0.00 sec)
min(列)はこの列の最小値を求めることを表す
mysql> select min(id) from students where gender=0;
+---------+
| min(id) |
+---------+
| 2 |
+---------+
1 row in set (0.00 sec)
sum(列)はこの列の和を求める
mysql> select sum(id) from students where gender=1;
+---------+
| sum(id) |
+---------+
| 8 |
+---------+
1 row in set (0.00 sec)
AVg(列)はこの列の平均値を求めることを表す
mysql> select avg(id) from students where gender=0;
+---------+
| avg(id) |
+---------+
| 5.0000 |
+---------+
1 row in set (0.00 sec)
グループ化
フィールドのグループ化によって、このフィールドの同じデータが1つのグループにグループ化されることを示すと、同じデータ列しかクエリーできません.差のあるデータ列は結果セットに表示されません.グループ化されたデータを統計し、集約演算を行うことができます.
select 1, 2, ... from group by 1, 2, 3...
mysql> select * from students;
+----+-----------+--------+---------------------+----------+
| id | name | gender | birthday | isDelete |
+----+-----------+--------+---------------------+----------+
| 1 | | 1 | 1990-07-07 00:00:00 | |
| 2 | | 1 | 1990-02-02 00:00:00 | |
| 3 | | | NULL | |
| 4 | | | 2017-09-21 00:00:00 | 1 |
+----+-----------+--------+---------------------+----------+
4 rows in set (0.00 sec)
mysql> select gender as gender,count(*) from students group by gender;
+--------+----------+
| gender | count(*) |
+--------+----------+
| | 2 |
| 1 | 2 |
+--------+----------+
2 rows in set (0.04 sec)
グループ化されたデータフィルタ
select 1, 2, ... from
group by 1, 2, 3...
having 1,... ...
mysql> select gender,count(*) from students group by gender having gender=1;
+--------+----------+
| gender | count(*) |
+--------+----------+
| 1 | 2 |
+--------+----------+
1 row in set (0.00 sec)
whereとhavingの比較
whereはfromの後に指定したテーブルをデータフィルタリングし、元のデータのフィルタリングに属するhavingはgroupbyの結果をフィルタリングする
ツールバーの
select * from
order by 1 asc|desc, 2 asc|desc,...
1 , 1 , 2 ,
asc ,
desc ,
id
mysql> select id,name from students order by id;
+----+-----------+
| id | name |
+----+-----------+
| 1 | |
| 2 | |
| 3 | |
| 4 | |
+----+-----------+
4 rows in set (0.00 sec)
mysql> select id,name from students where gender = 1 order by id desc;
+----+--------+
| id | name |
+----+--------+
| 2 | |
| 1 | |
+----+--------+
2 rows in set (0.00 sec)
行の一部を取得(ページング)
select * from limit start,count
start , count
start 0
mysql> select id,name from students limit 2,1;
+----+-----------+
| id | name |
+----+-----------+
| 3 | |
+----+-----------+
1 row in set (0.00 sec)
select * from students
where isdelete=0
limit (n-1)*m,m
完全なsql文
select distinct *
from
where ....
group by ... having ...
order by ...
limit star,count
:
from
where ....
group by ...
select distinct *
having ...
order by ...
limit star,count
データベースの詳細
関係
外部キー
mysql> create table scores(
-> id int primary key auto_increment not null,
-> score decimal(4,1),
-> stuid int,
-> subid int,
-> foreign key(stuid) references students(id),
-> foreign key(subid) references subjects(id));
カスケード・アクションのタイプは次のとおりです.
接続クエリーは次のように分類されます.
mysql> select students.name, subjects.title,scores.score
from scores
inner join students on scores.stuid=students.id
inner join subjects on scores.subid=subjects.id;
+-----------+--------+-------+
| name | title | score |
+-----------+--------+-------+
| | python | 100.0 |
| | python | 99.0 |
| | python | 71.0 |
| | linux | 90.0 |
| | linux | 79.0 |
| | linux | 89.0 |
| | java | 93.0 |
| | java | 91.0 |
| | java | 95.0 |
+-----------+--------+-------+
9 rows in set (0.01 sec)
mysql> select students.name,subjects.title,scores.score from students
-> inner join scores on students.id=scores.stuid
-> inner join subjects on scores.subid=students.id;
+-----------+--------+-------+
| name | title | score |
+-----------+--------+-------+
| | python | 100.0 |
| | linux | 100.0 |
| | java | 100.0 |
| | redis | 100.0 |
| | python | 79.0 |
| | linux | 79.0 |
| | java | 79.0 |
| | redis | 79.0 |
| | python | 95.0 |
| | linux | 95.0 |
| | java | 95.0 |
| | redis | 95.0 |
+-----------+--------+-------+
12 rows in set (0.00 sec)
自己参照
mysql> create table areas(id int primary key auto_increment not null,
-> title varchar(20),pid int,
-> foreign key(pid) references areas(id));
表示
mysql> create view v_1 as
-> select stu.*,sco.score,sub.title from scores as sco
-> inner join students as stu on sco.stuid=stu.id
-> inner join subjects as sub on sco.subid=sub.id;
mysql> select * from v_1;
+----+-----------+--------+---------------------+-------+--------+
| id | name | gender | birthday | score | title |
+----+-----------+--------+---------------------+-------+--------+
| 1 | | 1 | 1990-01-01 00:00:00 | 100.0 | python |
| 2 | | | NULL | 90.0 | python |
| 3 | | 1 | NULL | 85.0 | python |
| 1 | | 1 | 1990-01-01 00:00:00 | 99.0 | kotlin |
| 2 | | | NULL | 93.0 | kotlin |
| 3 | | 1 | NULL | 81.0 | kotlin |
| 1 | | 1 | 1990-01-01 00:00:00 | 71.0 | php |
| 2 | | | NULL | 95.0 | php |
| 3 | | 1 | NULL | 79.0 | php |
+----+-----------+--------+---------------------+-------+--------+
取引
トランザクション文
begin;
commit;
rollback;
索引
create index on ( (length))