[3/100] SQL文の完全な統合

SQL文

一般的な操作

1.ディスプレイのデータベース

show databases;

2.現在のバージョンのMySQL

show version();

3. Connectデータベース

mysql -u root -p******

データベースの操作

データベースを作成します。1.

create database xxx;
create database xxx charset=utf8;

データベースを削除します。2.

drop database xxx;

3.データベース

select database();
use xxx;

4.データベース権限

追加します!

データ・テーブルを使用するには

データベース内のすべてのテーブルを表示1.

show tables;

データベーステーブルを作成します。2.

create table x1table(id int, name varchar(30));
create table x2table(id int primary key not null auto_increment, name varchar(30));

表示データベースのテーブル構造
制約を設定

create table x3table(
    id int unsigned not null auto_increment primary key,
    name varchar(30),
    age tinyint unsigned default 0,
    high decimal(5,2),
    gender enum("男","女","中性","保密") default "保密",
    cls_id int unsigned
);

表示データベースのテーブル構造

desc x1table;

4.削除データシート

drop table x1table;

修正テーブル構造

フィールドを増やして1

alter table x3table add birthday datetime;

フィールドを変更します。2.

alter table x3table modify birthday date;

フィールド名を変更します。3.

alter table x3table change birthday birth date default "2000-01-01";

4.削除フィールド名

alter table x3table drop high;

データの種類

1.デジタルタイプ

デジタルタイプ

2.文字列型
文字列型

3.タイムタイプ

タイムタイプ

4.bitタイプ

削除の変更の検索データ(CURD)

データを挿入することにより

--全部插入
insert into x3table values(0, "老王", 18, 188.88, "男", 0);
--部分插入
insert into x3table (name,gender) values ("金莲", "女");
--批量插入
insert into x3table (name,gender) values ("金莲", "女"), ("大乔","女");

修正データ

update x3table set age=23, gender=1 where id =3;

お問い合わせ

--查询所有列
select * from x3table;
--查询条件(and 和 or 和 not)
select * from x3table where name="老王";
select * from x3table where id>2;
select * from x3table where id>2 and id<10;
--查询条件之 in
select * from x3table where id in (12, 15, 19);
select * from x3table where id not  in (12, 15, 19);
--查询条件之 between ... and ...
select * from x3table where id between 12 and 22;
select * from x3table where id not between 12 and 22;
--查询条件之判断空
select * from x3table where birth is null;
select * from x3table where birth is not null;
--查询指定列
select name,gender from x3table;
select name as 姓名,gender as 性别 from x3table;
select x.name x.gender from x3table as x;
--消除重复行
select distinct gender from x3table;
--模糊查询(like %替换1个或多个 _替换1个)
select * from x3table where name like "%老%";
--模糊查询之rlike,正则表达式
select * from x3table where name rikie "^小.*";
--排序
select * from x3table order by age asc;
select * from x3table order by age desc;
select * from x3table order by age desc, id desc;
--聚合函数(count sum max min avg 和 round)
select count(id) from x3table; --返回总行数
select max(age) from x3table; --返回最大值
select avg(age) from x3table; --返回平均值
select round(sum(age)/count(id), 2) from x3table; --保留小数点位数
--分组 group by
select avg(age), gender from x3table group by gender;  --要跟聚合一起用
select gender, group_concat(name,"_",id,"_",age) from x3table group by gender;  --group_concat
select gender, group_concat(name,"_",id,"_",age), avg(age) from x3table group by gender having avg(age)>22;  --having对查出来点结果进行条件判断
--分页
select * from x3table limit 3;  --limit 个数
select * from x3table limit 0, 5;  --limit(第N页-1)*每页的个数,每页的个数
select * from x3table limit 1, 5;  --limit(第N页-1)*每页的个数,每页的个数
--连接查询
--内连接
select * from students inner join classes on students.cls_id = classes.id;
--左连接
select * from students as s left join classes as c on s.clsid = c.id;  --右连接把两个表点顺序换下就行
--子查询
select * from student where height = (select max(height) from students);

和GROUP_CONCATでグループ

[削除]

delete from x3table where id=3;

他の

データベースエンジン

二つの主あり:MyISAMテーブルとInnoDBは、主な違いは、InnoDBのサポートトランザクション処理と外部キー、および行レベルロックです。

show create table x3table;

データベーステーブルの情報を返します。

おすすめ

転載: www.cnblogs.com/j44p5/p/12380760.html