学习mysql_day2
數據庫環境準備:
創建數據庫
create database mysql_demo1 charset=utf8;
MariaDB [(none)]> create database mysql_demo1 charset=utf8; Query OK, 1 row affected (0.00 sec)使用數據庫
use mysql_demo1;
MariaDB [(none)]> use mysql_demo1; Database changed創建表參數
unsigned 整型無符號 signed 整型有符號 primary key 詳解參考:http://c.biancheng.net/view/2440.html foreign key 詳解參考:http://c.biancheng.net/view/2441.html unique key 詳解參考:http://c.biancheng.net/view/2445.html auto_increment 自增字段 not null 不為空 default 默認值 int int類型 char 存儲字符串 varchar 存儲字符串(char是屬于固定長度的字符類型,而varchar是屬于可變長度的字符類型。) tinyint 型的字段如果不設置UNSIGNED類型,存儲-128到127的整數。 (tinyint(1),tinyint(2),tinyint(3),tinyint(4))(稍后詳解) DECIMAL (5,2)規定了存儲的值將不會超過5位數字,開且小數點后面有2位數字。 enum 枚舉類型 添加新的數據不為這四個值則會報錯 bit 利用它可定義一個位變量,但不能定義位指針,也不能定義位數組。它的值是一個二進制位,不是0就是1,類似Boolean類型中的True和False。創建students表
create table students(id int unsigned primary key auto_increment not null,name varchar(20) default '',age tinyint unsigned default 0,height decimal(5,2),gender enum('男','女','中性','保密') default '保密',cls_id int unsigned default 0,is_delete bit default 0 ); MariaDB [mysql_demo1]> create table students(-> id int unsigned primary key auto_increment not null,-> name varchar(20) default '',-> age tinyint unsigned default 0,-> height decimal(5,2),-> gender enum('男','女','中性','保密') default '保密',-> cls_id int unsigned default 0,-> is_delete bit default 0-> ); Query OK, 0 rows affected (0.01 sec)創建classes表
create table classes(
 id int unsigned auto_increment primary key not null,
 name varchar(30) not null
 );
查看表是否創建成功
show tables;
MariaDB [mysql_demo1]> show tables; +-----------------------+ | Tables_in_mysql_demo1 | +-----------------------+ | classes | | students | +-----------------------+ 2 rows in set (0.00 sec)準備數據:
向students表中插入數據
insert into students values
 (0,‘小明’,18,180.00,1,1,0),
 (0,‘小張’,18,160.00,1,2,0),
 (0,‘小紅’,20,170.00,2,1,0),
 (0,‘周杰’,38,175.00,1,1,0),
 (0,‘彭玉宴’,56,150.00,1,2,0),
 (0,‘劉德化’,29,150.00,1,2,0),
 (0,‘張學友’,36,180.00,1,1,0),
 (0,‘周杰倫’,25,166.00,1,1,0),
 (0,‘風姐’,16,169.00,2,1,0),
 (0,‘王小明’,57,162.00,1,2,1),
 (0,‘張小華’,46,173.00,2,1,0),
 (0,‘金星’,29,175.00,2,1,1),
 (0,‘黃蓉’,66,185.00,3,2,0),
 (0,‘古天樂’,15,186.00,4,2,0),
 (0,‘劉小海’,31,175.00,4,1,0),
 (0,‘小月月’,57,163.00,2,1,0);
向classes表插入數據
insert into classes values
 (0, “python_01期”),
 (0, “python_02期”);
查詢所有字段
select * from 表名; (也驗證一下上面數據是否插入成功)
 select * from students;
 select * from classes;
查詢指定字段
select 列1,列2,… from 表名;
 select name,age from students;
使用 as 給字段起別名 作用于當前語句
select 字段 as 名字… from 表名;
 select name as 名字,age as 年齡 from students;
使用 as 給表名起別名 作用于當前語句
select 表別名.字段1,表別名.字段2 from 表 as 表別名;
 select s.name,s.age from students as s;
跨表查詢 這種查不準不常用
select students.name,classes.name from students,classes;
MariaDB [mysql_demo1]> select students.name,classes.name from students,classes; +-----------+--------------+ | name | name | +-----------+--------------+ | 小明 | python_01期 | | 小明 | python_02期 | | 小張 | python_01期 | | 小張 | python_02期 | | 小紅 | python_01期 | | 小紅 | python_02期 | | 周杰 | python_01期 | | 周杰 | python_02期 | | 彭玉宴 | python_01期 | | 彭玉宴 | python_02期 | | 劉德化 | python_01期 | | 劉德化 | python_02期 | | 張學友 | python_01期 | | 張學友 | python_02期 | | 周杰倫 | python_01期 | | 周杰倫 | python_02期 | | 風姐 | python_01期 | | 風姐 | python_02期 | | 王小明 | python_01期 | | 王小明 | python_02期 | | 張小華 | python_01期 | | 張小華 | python_02期 | | 金星 | python_01期 | | 金星 | python_02期 | | 黃蓉 | python_01期 | | 黃蓉 | python_02期 | | 古天樂 | python_01期 | | 古天樂 | python_02期 | | 劉小海 | python_01期 | | 劉小海 | python_02期 | | 小月月 | python_01期 | | 小月月 | python_02期 | +-----------+--------------+ 32 rows in set (0.00 sec)distinct 消除重復行 也就是指定列的不重復的值
以students為例 里面有四個性別 我們查詢出來
 select distinct gender from students;
如果distinct后有多個字段, 只有當查詢的多列的查詢結果完全相同才能去重
查詢age 大于18歲的所有數據
select * from students where age > 18;
MariaDB [mysql_demo1]> select * from students where age > 18; +----+-----------+------+--------+--------+--------+-----------+ | id | name | age | height | gender | cls_id | is_delete | +----+-----------+------+--------+--------+--------+-----------+ | 3 | 小紅 | 20 | 170.00 | 女 | 1 | | | 4 | 周杰 | 38 | 175.00 | 男 | 1 | | | 5 | 彭玉宴 | 56 | 150.00 | 男 | 2 | | | 6 | 劉德化 | 29 | 150.00 | 男 | 2 | | | 7 | 張學友 | 36 | 180.00 | 男 | 1 | | | 8 | 周杰倫 | 25 | 166.00 | 男 | 1 | | | 10 | 王小明 | 57 | 162.00 | 男 | 2 | | | 11 | 張小華 | 46 | 173.00 | 女 | 1 | | | 12 | 金星 | 29 | 175.00 | 女 | 1 | | | 13 | 黃蓉 | 66 | 185.00 | 中性 | 2 | | | 15 | 劉小海 | 31 | 175.00 | 保密 | 1 | | | 16 | 小月月 | 57 | 163.00 | 女 | 1 | | +----+-----------+------+--------+--------+--------+-----------+查詢age 大于18 小于40的所有數據 有兩種方法
select * from students where age > 18 and age < 40;
 select * from students where age between 18 and 40;
 注意兩種不同之處 between是并等于條件
查詢age 大于18小于40的所有女性的數據
select * from students where age between 18 and 40 and gender=‘女’;
MariaDB [mysql_demo1]> select * from students where age between 18 and 40 and gender='女'; +----+--------+------+--------+--------+--------+-----------+ | id | name | age | height | gender | cls_id | is_delete | +----+--------+------+--------+--------+--------+-----------+ | 3 | 小紅 | 20 | 170.00 | 女 | 1 | | | 12 | 金星 | 29 | 175.00 | 女 | 1 | | +----+--------+------+--------+--------+--------+-----------+ 2 rows in set (0.00 sec)order by 排序 asc正序 desc倒序 查詢男性身高從高到底查詢
select * from students where gender=‘男’ order by height desc;
MariaDB [mysql_demo1]> select * from students where gender='男' order by height desc; +----+-----------+------+--------+--------+--------+-----------+ | id | name | age | height | gender | cls_id | is_delete | +----+-----------+------+--------+--------+--------+-----------+ | 1 | 小明 | 18 | 180.00 | 男 | 1 | | | 7 | 張學友 | 36 | 180.00 | 男 | 1 | | | 4 | 周杰 | 38 | 175.00 | 男 | 1 | | | 8 | 周杰倫 | 25 | 166.00 | 男 | 1 | | | 10 | 王小明 | 57 | 162.00 | 男 | 2 | | | 2 | 小張 | 18 | 160.00 | 男 | 2 | | | 5 | 彭玉宴 | 56 | 150.00 | 男 | 2 | | | 6 | 劉德化 | 29 | 150.00 | 男 | 2 | | +----+-----------+------+--------+--------+--------+-----------+ 8 rows in set (0.00 sec)查詢男性身高從高到底查詢 如果身高一樣則年齡從高到底來顯示 依次類推
select * from students where gender=‘男’ order by height desc,age desc;
MariaDB [mysql_demo1]> select * from students where gender='男' order by height desc,age desc; +----+-----------+------+--------+--------+--------+-----------+ | id | name | age | height | gender | cls_id | is_delete | +----+-----------+------+--------+--------+--------+-----------+ | 7 | 張學友 | 36 | 180.00 | 男 | 1 | | | 1 | 小明 | 18 | 180.00 | 男 | 1 | | | 4 | 周杰 | 38 | 175.00 | 男 | 1 | | | 8 | 周杰倫 | 25 | 166.00 | 男 | 1 | | | 10 | 王小明 | 57 | 162.00 | 男 | 2 | | | 2 | 小張 | 18 | 160.00 | 男 | 2 | | | 5 | 彭玉宴 | 56 | 150.00 | 男 | 2 | | | 6 | 劉德化 | 29 | 150.00 | 男 | 2 | | +----+-----------+------+--------+--------+--------+-----------+ 8 rows in set (0.01 sec)總結
以上是生活随笔為你收集整理的学习mysql_day2的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 安身立命之本
- 下一篇: 量子力学中,全体自然数之和是负十二分之一
