show DATABASES ;

create database nulige character set utf8;

use nulige;

show tables;

#创建表
CREATE TABLE ExamResult( id INT PRIMARY KEY auto_increment,
name VARCHAR (20),
JS DOUBLE ,
Django DOUBLE ,
flask DOUBLE
); #往表中插入数据
INSERT INTO ExamResult VALUES (1,"yuan",98,98,98),
(2,"xialv",35,98,67),
(3,"alex",59,59,62),
(4,"wusir",88,89,82),
(5,"alvin",88,98,67),
(6,"yuan",86,100,55); #查询表中所有学生的信息
select * from ExamResult; #查询表中所有学生的姓名和对应的英语成绩
SELECT name,JS FROM ExamResult; #过滤表中重复数据
select DISTINCT Js,name from ExamResult; SELECT name,JS,Django,flask FROM ExamResult; SELECT name,JS+10,Django+10,flask+20 FROM ExamResult; SELECT name as 姓名,JS+10 as JS成绩,Django+10,flask+20 FROM ExamResult; SELECT name JS from ExamResult; select * from ExamResult WHERE name='yuan' SELECT name,Js from ExamResult WHERE JS>90; SELECT name,JS from ExamResult WHERE JS!=88; #between在什么之间 在88=100之间
SELECT name,JS FROM ExamResult WHERE JS BETWEEN 88 and 100; #in在什么之内
SELECT name,JS FROM ExamResult WHERE JS IN (88,99,77); #like 模糊查询,查询名字内有y的同学
SELECT name,JS FROM ExamResult WHERE name LIKE "y%"; SELECT name,JS FROM ExamResult WHERE name LIKE "a____"; #查询js分=98同学
SELECT name,JS from ExamResult WHERE name='yuan' and JS=98; insert into ExamResult (name) VALUE ('刘洋'); SELECT * from ExamResult; #查询值为空
SELECT name from ExamResult WHERE JS is NULL; #排序
SELECT name,JS from ExamResult WHERE JS>70 ORDER BY JS; SELECT name,JS FROM ExamResult WHERE JS>70 ORDER BY Js DESC ; #as重命名
SELECT name,JS+Django+flask as 总成绩 from ExamResult ORDER BY 总成绩 DESC; #按yuan 总成绩进行降序排列(mysql语句有执行顺序:from where select group by having order by)
SELECT name,JS+Django+flask as 总成绩 from ExamResult WHERE name="yuan" ORDER BY 总成绩; #分组查询 group by
SELECT * from ExamResult; #按名字进行分组
SELECT * from ExamResult GROUP BY name; #按JS进行分组
SELECT * from ExamResult GROUP BY JS; #按第3列进行分组
SELECT * from ExamResult GROUP BY 3; #按名字进行排序
SELECT name,sum(JS) from ExamResult GROUP BY name; #对成绩表按名字分组后,显示每一类名字的Django的分数总和>150的
SELECT name,sum(Django) from ExamResult GROUP BY name having sum(Django)>150; #having和where两者都可以对查询结果进行进一步的过滤,差别有:
# <1>where语句只能用在分组之前的筛选,having可以用在分组之后的筛选;
# <2>使用where语句的地方都可以用having进行替换
# <3>having中可以用聚合函数,where中就不行。 SELECT * from ExamResult WHERE id=3;
SELECT * from ExamResult HAVING id=3; #聚合函数:先把要求的内容查出来再包上聚合函数即可。 #count(列名):统计行的个数
SELECT count(name) from ExamResult WHERE js>70; #统计一个班级共有多少学生
select count(*) from ExamResult; SELECT sum(JS)/count(name) from ExamResult; SELECT AVG(JS) from ExamResult; #遇到ifnull的时候转换成0
#统计总分大于280的人数有多少?
select count(name) from ExamResult where (ifnull(JS,0)+ifnull(Django,0)+ifnull(flask,0))>280; SELECT * from ExamResult; #max最大值
SELECT max(JS) FROM ExamResult; #遇到null的时候,就转成0
#min最小值
SELECT min(ifnull(JS,0)) FROM ExamResult;
SELECT max(JS+Django+flask) from ExamResult; #limit 跳过几条显示几条
SELECT * FROM ExamResult LIMIT 1; #跳过1,从2开始到5
SELECT * FROM ExamResult LIMIT 1,4;

增加外键

#查看数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lessens |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set #创建abc数据库
mysql> create database abc character set utf8;
Query OK, 1 row affected #进入abc表
mysql> use abc ;
Database changed #创建表
mysql> CREATE TABLE ClassCharger( id TINYINT PRIMARY KEY auto_increment,
name VARCHAR (20),
age INT ,
is_marriged boolean
)ENGINE=INNODB;
Query OK, 0 rows affected #往表中插入数据
mysql> INSERT INTO ClassCharger (name,age,is_marriged) VALUES ("冰冰",52,0),
("丹丹",34,0),
("歪歪",32,0),
("姗姗",28,0),
("小雨",61,0);
Query OK, 5 rows affected
Records: 5 Duplicates: 0 Warnings: 0 #创建表
mysql> CREATE TABLE Student(
id INT PRIMARY KEY auto_increment,
name VARCHAR (20),
charger_id TINYINT
) ENGINE=INNODB;
Query OK, 0 rows affected #往表中插入数据
mysql> INSERT INTO Student(name,charger_id) VALUES ("alvin1",2),
("alvin2",4),
("alvin3",1),
("alvin4",3),
("alvin5",5);
Query OK, 5 rows affected
Records: 5 Duplicates: 0 Warnings: 0 #查看表中数据
mysql> select * from Student;
+----+--------+------------+
| id | name | charger_id |
+----+--------+------------+
| 1 | alvin1 | 2 |
| 2 | alvin2 | 4 |
| 3 | alvin3 | 1 |
| 4 | alvin4 | 3 |
| 5 | alvin5 | 5 |
+----+--------+------------+
5 rows in set #查看表中数据
mysql> select * from ClassCharger;
+----+------+-----+-------------+
| id | name | age | is_marriged |
+----+------+-----+-------------+
| 1 | 冰冰 | 52 | 0 |
| 2 | 丹丹 | 34 | 0 |
| 3 | 歪歪 | 32 | 0 |
| 4 | 姗姗 | 28 | 0 |
| 5 | 小雨 | 61 | 0 |
+----+------+-----+-------------+
5 rows in set #创建外键
mysql> ALTER TABLE Student ADD CONSTRAINT abc
FOREIGN KEY(charger_id)
REFERENCES classcharger(id);
Query OK, 5 rows affected
Records: 5 Duplicates: 0 Warnings: 0 #查看表结构
mysql> desc Student;
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| charger_id | tinyint(4) | YES | MUL | NULL | |
+------------+-------------+------+-----+---------+----------------+
3 rows in set #查看表结构
mysql> desc ClassCharger;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| id | tinyint(4) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| is_marriged | tinyint(1) | YES | | NULL | |
+-------------+-------------+------+-----+---------+----------------+
4 rows in set #查看建表语句
mysql> show CREATE TABLE Student;
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Student | CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`charger_id` tinyint(4) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `abc` (`charger_id`),
CONSTRAINT `abc` FOREIGN KEY (`charger_id`) REFERENCES `classcharger` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set

mysql> select * from student;
+----+--------+------------+
| id | name | charger_id |
+----+--------+------------+
| 1 | alvin1 | 2 |
| 2 | alvin2 | 4 |
| 3 | alvin3 | 1 |
| 4 | alvin4 | 3 |
| 5 | alvin5 | 5 |
+----+--------+------------+
5 rows in set

#插入数据
mysql> insert into student(name,charger_id) values("alvin1",2),("alvin1",4);
Query OK, 2 rows affected
Records: 2 Duplicates: 0 Warnings: 0

mysql> select * from student;
+----+--------+------------+
| id | name | charger_id |
+----+--------+------------+
| 1 | alvin1 | 2 |
| 2 | alvin2 | 4 |
| 3 | alvin3 | 1 |
| 4 | alvin4 | 3 |
| 5 | alvin5 | 5 |
| 6 | alvin1 | 2 |
| 7 | alvin1 | 4 |
+----+--------+------------+
7 rows in set

#查看表
mysql> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| classcharger |
| student |
+---------------+
2 rows in set

#查看表内容
mysql> select * from classcharger;
+----+------+-----+-------------+
| id | name | age | is_marriged |
+----+------+-----+-------------+
| 1 | 冰冰 | 52 | 0 |
| 2 | 丹丹 | 34 | 0 |
| 3 | 歪歪 | 32 | 0 |
| 4 | 姗姗 | 28 | 0 |
| 5 | 小雨 | 61 | 0 |
+----+------+-----+-------------+
5 rows in set

#查看表内容
mysql> select * from student;
+----+--------+------------+
| id | name | charger_id |
+----+--------+------------+
| 1 | alvin1 | 2 |
| 2 | alvin2 | 4 |
| 3 | alvin3 | 1 |
| 4 | alvin4 | 3 |
| 5 | alvin5 | 5 |
| 6 | alvin1 | 2 |
| 7 | alvin1 | 4 |
+----+--------+------------+
7 rows in set

#更新表中数据
mysql> update student set charger_id=4 where id=1 or id=6;
Query OK, 2 rows affected
Rows matched: 2 Changed: 2 Warnings: 0

mysql> select * from student;
+----+--------+------------+
| id | name | charger_id |
+----+--------+------------+
| 1 | alvin1 | 4 |
| 2 | alvin2 | 4 |
| 3 | alvin3 | 1 |
| 4 | alvin4 | 3 |
| 5 | alvin5 | 5 |
| 6 | alvin1 | 4 |
| 7 | alvin1 | 4 |
+----+--------+------------+
7 rows in set

#删除掉id=2 的 丹丹 ,再插入数据就会报错。
mysql> delete from classcharger where id=2;
Query OK, 1 row affected

mysql> select * from classcharger;
+----+------+-----+-------------+
| id | name | age | is_marriged |
+----+------+-----+-------------+
| 1 | 冰冰 | 52 | 0 |
| 3 | 歪歪 | 32 | 0 |
| 4 | 姗姗 | 28 | 0 |
| 5 | 小雨 | 61 | 0 |
+----+------+-----+-------------+
4 rows in set

再插入数据就会报错
mysql> insert into student (name,charger_id) values("alvin8",2);
1452 - Cannot add or update a child row: a foreign key constraint fails (`abc`.`student`, CONSTRAINT `abc` FOREIGN KEY (`charger_id`) REFERENCES `classcharger` (`id`))

#删除外键

mysql> alter table student drop foreign key abc;
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0

#查看建表语句

mysql> show create table student;
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`charger_id` tinyint(4) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `abc` (`charger_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set

#增加外键

mysql> ALTER TABLE student ADD CONSTRAINT abc
FOREIGN KEY(charger_id)
REFERENCES classcharger(id);
Query OK, 7 rows affected
Records: 7 Duplicates: 0 Warnings: 0

#查看增加的外键

mysql> show create table student;
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`charger_id` tinyint(4) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `abc` (`charger_id`),
CONSTRAINT `abc` FOREIGN KEY (`charger_id`) REFERENCES `classcharger` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set

INNODB支持的ON语句

#查看数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| abc |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set #创建数据库并设置字符集
mysql> create database s1 character set utf8;
Query OK, 1 row affected mysql> use s1;
Database changed #创建表cc
mysql> CREATE TABLE CC( id TINYINT PRIMARY KEY auto_increment,
name VARCHAR (20),
age INT ,
is_marriged boolean
);
Query OK, 0 rows affected #创建表s3,并设置外键为 delete cascade 方式
mysql> create table s3(
id int primary key auto_increment,
name varchar(20),
charger_id tinyint,
foreign key (charger_id) references CC(id) on delete cascade
) engine=innodb;
Query OK, 0 rows affected #往表中插入数据
mysql> INSERT INTO CC (name,age,is_marriged) VALUES ("冰冰",52,0),
("丹丹",34,0),
("歪歪",32,0),
("姗姗",28,0),
("小雨",61,0);
Query OK, 5 rows affected
Records: 5 Duplicates: 0 Warnings: 0 #往表中插入数据
mysql> INSERT INTO S3(name,charger_id) VALUES ("alvin1",2),
("alvin2",4),
("alvin3",1),
("alvin4",3),
("alvin5",5);
Query OK, 5 rows affected
Records: 5 Duplicates: 0 Warnings: 0 #查看表内容
mysql> select * from cc;
+----+------+-----+-------------+
| id | name | age | is_marriged |
+----+------+-----+-------------+
| 1 | 冰冰 | 52 | 0 |
| 2 | 丹丹 | 34 | 0 |
| 3 | 歪歪 | 32 | 0 |
| 4 | 姗姗 | 28 | 0 |
| 5 | 小雨 | 61 | 0 |
+----+------+-----+-------------+
5 rows in set #查看表内容
mysql> select * from s3;
+----+--------+------------+
| id | name | charger_id |
+----+--------+------------+
| 1 | alvin1 | 2 |
| 2 | alvin2 | 4 |
| 3 | alvin3 | 1 |
| 4 | alvin4 | 3 |
| 5 | alvin5 | 5 |
+----+--------+------------+
5 rows in set #删除表中数据
mysql> delete from cc where id=4;
Query OK, 1 row affected #再查看表内容,4的数据被删除了。
mysql> select * from s3;
+----+--------+------------+
| id | name | charger_id |
+----+--------+------------+
| 1 | alvin1 | 2 |
| 3 | alvin3 | 1 |
| 4 | alvin4 | 3 |
| 5 | alvin5 | 5 |
+----+--------+------------+
4 rows in set #查看建表语句
mysql> show create table s3;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| s3 | CREATE TABLE `s3` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`charger_id` tinyint(4) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `charger_id` (`charger_id`),
CONSTRAINT `s3_ibfk_1` FOREIGN KEY (`charger_id`) REFERENCES `cc` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set #########################设置外键为 set null 方式####################### #查看建表语句的外键信息
mysql> show create table s3;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| s3 | CREATE TABLE `s3` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`charger_id` tinyint(4) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `charger_id` (`charger_id`),
CONSTRAINT `s3_ibfk_1` FOREIGN KEY (`charger_id`) REFERENCES `cc` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set #删除外键
mysql> alter table s3 drop foreign key s3_ibfk_1;
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0 #查看删除结果
mysql> show create table s3;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| s3 | CREATE TABLE `s3` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`charger_id` tinyint(4) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `charger_id` (`charger_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set #增加外键为set null 方式
mysql> alter table s3 add constraint s3_fk_cc foreign key (charger_id)
-> references cc(id) on delete set null;
Query OK, 4 rows affected
Records: 4 Duplicates: 0 Warnings: 0 mysql> select * from cc;
+----+------+-----+-------------+
| id | name | age | is_marriged |
+----+------+-----+-------------+
| 1 | 冰冰 | 52 | 0 |
| 2 | 丹丹 | 34 | 0 |
| 3 | 歪歪 | 32 | 0 |
| 5 | 小雨 | 61 | 0 |
+----+------+-----+-------------+
4 rows in set mysql> delete from CC where id=3;
Query OK, 1 row affected mysql> select * from s3;
+----+--------+------------+
| id | name | charger_id |
+----+--------+------------+
| 1 | alvin1 | 2 |
| 3 | alvin3 | 1 |
| 4 | alvin4 | NULL |
| 5 | alvin5 | 5 |
+----+--------+------------+
4 rows in set mysql>

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| abc |
| crm |
| mysql |
| performance_schema |
| s1 |
| sys |
+--------------------+
7 rows in set

#创建数据库
mysql> create database t2 character set utf8;
Query OK, 1 row affected

mysql> use t2;
Database changed

#创建表
mysql> create table tableA(id int primary key,name varchar(20));
Query OK, 0 rows affected

#创建表
mysql> create table tableB(id int primary key,name varchar(20),tableA_id int);
Query OK, 0 rows affected

#往表中插入数据
mysql> insert into tableA values(1,'alvin'),(2,'xialv'),
(3,'yuan');
Query OK, 3 rows affected
Records: 3 Duplicates: 0 Warnings: 0

#往表中插入数据
mysql> insert into tableB values(1,'小雨',1),(2,'冰冰',2),(3,'周周',4);
Query OK, 3 rows affected
Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from tableA;
+----+-------+
| id | name |
+----+-------+
| 1 | alvin |
| 2 | xialv |
| 3 | yuan |
+----+-------+
3 rows in set

mysql> select * from tableB;
+----+------+-----------+
| id | name | tableA_id |
+----+------+-----------+
| 1 | 小雨 | 1 |
| 2 | 冰冰 | 2 |
| 3 | 周周 | 4 |
+----+------+-----------+
3 rows in set

#笛卡尔积查询
mysql> select * from tableA,tableB;
+----+-------+----+------+-----------+
| id | name | id | name | tableA_id |
+----+-------+----+------+-----------+
| 1 | alvin | 1 | 小雨 | 1 |
| 2 | xialv | 1 | 小雨 | 1 |
| 3 | yuan | 1 | 小雨 | 1 |
| 1 | alvin | 2 | 冰冰 | 2 |
| 2 | xialv | 2 | 冰冰 | 2 |
| 3 | yuan | 2 | 冰冰 | 2 |
| 1 | alvin | 3 | 周周 | 4 |
| 2 | xialv | 3 | 周周 | 4 |
| 3 | yuan | 3 | 周周 | 4 |
+----+-------+----+------+-----------+
9 rows in set

#内连接查询
mysql> select * from tableA,tableB where tableA.id=tableB.id;
+----+-------+----+------+-----------+
| id | name | id | name | tableA_id |
+----+-------+----+------+-----------+
| 1 | alvin | 1 | 小雨 | 1 |
| 2 | xialv | 2 | 冰冰 | 2 |
| 3 | yuan | 3 | 周周 | 4 |
+----+-------+----+------+-----------+
3 rows in set

#只显示关联人的id,姓名,
mysql> select tableA.id,tableA.name,tableB.name from tableA,tableB where tableA.id=tableB.id;
+----+-------+------+
| id | name | name |
+----+-------+------+
| 1 | alvin | 小雨 |
| 2 | xialv | 冰冰 |
| 3 | yuan | 周周 |
+----+-------+------+
3 rows in set

mysql> select * from tableB inner join tableA on tableB.tableA_id =tableA.id;
+----+------+-----------+----+-------+
| id | name | tableA_id | id | name |
+----+------+-----------+----+-------+
| 1 | 小雨 | 1 | 1 | alvin |
| 2 | 冰冰 | 2 | 2 | xialv |
+----+------+-----------+----+-------+
2 rows in set

#多表查询之连接查询

#创建表
mysql>create table employee(
emp_id int auto_increment primary key not null,
emp_name varchar(50),
age int,
dept_id int
);
Query OK, 0 rows affected

#往表中插入数据
mysql> insert into employee(emp_name,age,dept_id) values
('A',19,200),
('B',26,201),
('C',30,201),
('D',24,202),
('E',20,200),
('F',38,204);
Query OK, 6 rows affected
Records: 6 Duplicates: 0 Warnings: 0

#创建表
mysql>create table department(
dept_id int,
dept_name varchar(100)
);
Query OK, 0 rows affected

#往表中插入数据
mysql>insert into department values
(200,'人事部'),
(201,'技术部'),
(202,'销售部'),
(203,'财政部');

Query OK, 4 rows affected
Records: 4 Duplicates: 0 Warnings: 0

#查看表
mysql> show tables;
+--------------+
| Tables_in_t2 |
+--------------+
| department |
| employee |
| tablea |
| tableb |
+--------------+
4 rows in set

#笛卡尔积查询
mysql> select * from employee,department;
+--------+----------+-----+---------+---------+-----------+
| emp_id | emp_name | age | dept_id | dept_id | dept_name |
+--------+----------+-----+---------+---------+-----------+
| 1 | A | 19 | 200 | 200 | 人事部 |
| 1 | A | 19 | 200 | 201 | 技术部 |
| 1 | A | 19 | 200 | 202 | 销售部 |
| 1 | A | 19 | 200 | 203 | 财政部 |
| 2 | B | 26 | 201 | 200 | 人事部 |
| 2 | B | 26 | 201 | 201 | 技术部 |
| 2 | B | 26 | 201 | 202 | 销售部 |
| 2 | B | 26 | 201 | 203 | 财政部 |
| 3 | C | 30 | 201 | 200 | 人事部 |
| 3 | C | 30 | 201 | 201 | 技术部 |
| 3 | C | 30 | 201 | 202 | 销售部 |
| 3 | C | 30 | 201 | 203 | 财政部 |
| 4 | D | 24 | 202 | 200 | 人事部 |
| 4 | D | 24 | 202 | 201 | 技术部 |
| 4 | D | 24 | 202 | 202 | 销售部 |
| 4 | D | 24 | 202 | 203 | 财政部 |
| 5 | E | 20 | 200 | 200 | 人事部 |
| 5 | E | 20 | 200 | 201 | 技术部 |
| 5 | E | 20 | 200 | 202 | 销售部 |
| 5 | E | 20 | 200 | 203 | 财政部 |
| 6 | F | 38 | 204 | 200 | 人事部 |
| 6 | F | 38 | 204 | 201 | 技术部 |
| 6 | F | 38 | 204 | 202 | 销售部 |
| 6 | F | 38 | 204 | 203 | 财政部 |
+--------+----------+-----+---------+---------+-----------+
24 rows in set

外连接之左外连接
mysql> select * from employee left join department on employee.dept_id = department.dept_id;
+--------+----------+-----+---------+---------+-----------+
| emp_id | emp_name | age | dept_id | dept_id | dept_name |
+--------+----------+-----+---------+---------+-----------+
| 1 | A | 19 | 200 | 200 | 人事部 |
| 5 | E | 20 | 200 | 200 | 人事部 |
| 2 | B | 26 | 201 | 201 | 技术部 |
| 3 | C | 30 | 201 | 201 | 技术部 |
| 4 | D | 24 | 202 | 202 | 销售部 |
| 6 | F | 38 | 204 | NULL | NULL |
+--------+----------+-----+---------+---------+-----------+
6 rows in set

mysql> select employee.emp_name,department.dept_name from employee,department where employee.dept_id = department.dept_id and employee.emp_name="A";
+----------+-----------+
| emp_name | dept_name |
+----------+-----------+
| A | 人事部 |
+----------+-----------+
1 row in set

#外连接之右外连接
mysql> select * from employee RIGHT JOIN department on employee.dept_id = department.dept_id;
+--------+----------+------+---------+---------+-----------+
| emp_id | emp_name | age | dept_id | dept_id | dept_name |
+--------+----------+------+---------+---------+-----------+
| 1 | A | 19 | 200 | 200 | 人事部 |
| 2 | B | 26 | 201 | 201 | 技术部 |
| 3 | C | 30 | 201 | 201 | 技术部 |
| 4 | D | 24 | 202 | 202 | 销售部 |
| 5 | E | 20 | 200 | 200 | 人事部 |
| NULL | NULL | NULL | NULL | 203 | 财政部 |
+--------+----------+------+---------+---------+-----------+
6 rows in set

#外连接之全外连接

全外连接:在内连接的基础上增加左边有右边没有的和右边有左边没有的结果

-- mysql不支持全外连接 full JOIN
-- mysql可以使用此种方式间接实现全外连接

mysql> select * from tableB full join tableA on tableB.tableA_id = table.id;
1054 - Unknown column 'tableB.tableA_id' in 'on clause'

mysql> select employee.emp_name,department.dept_name from employee,department where employee
-> .dept_id=department.dept_id and department.dept_name="技术部";
+----------+-----------+
| emp_name | dept_name |
+----------+-----------+
| B | 技术部 |
| C | 技术部 |
+----------+-----------+
2 rows in set

#显示大于25岁所有的部门
mysql> select distinct department.dept_name from employee,department where employee.dept_id=department.dept_id and employee.age>25;
+-----------+
| dept_name |
+-----------+
| 技术部 |
+-----------+
1 row in set

多表查询之子查询

#带IN关键字的子查询
mysql> select * from employee where dept_id in(200,201,202,203);
+--------+----------+-----+---------+
| emp_id | emp_name | age | dept_id |
+--------+----------+-----+---------+
| 1 | A | 19 | 200 |
| 2 | B | 26 | 201 |
| 3 | C | 30 | 201 |
| 4 | D | 24 | 202 |
| 5 | E | 20 | 200 |
+--------+----------+-----+---------+
5 rows in set

mysql> select dept_id from department
;
+---------+
| dept_id |
+---------+
| 200 |
| 201 |
| 202 |
| 203 |
+---------+
4 rows in set

#带IN关键字的子查询
mysql> select * from employee where dept_id in(select dept_id from department);
+--------+----------+-----+---------+
| emp_id | emp_name | age | dept_id |
+--------+----------+-----+---------+
| 1 | A | 19 | 200 |
| 2 | B | 26 | 201 |
| 3 | C | 30 | 201 |
| 4 | D | 24 | 202 |
| 5 | E | 20 | 200 |
+--------+----------+-----+---------+
5 rows in set

#用select查询语句,建表,会丢失主键信息
mysql> create table aa(select * from employee);
Query OK, 6 rows affected
Records: 6 Duplicates: 0 Warnings: 0

mysql> select * from aa;
+--------+----------+-----+---------+
| emp_id | emp_name | age | dept_id |
+--------+----------+-----+---------+
| 1 | A | 19 | 200 |
| 2 | B | 26 | 201 |
| 3 | C | 30 | 201 |
| 4 | D | 24 | 202 |
| 5 | E | 20 | 200 |
| 6 | F | 38 | 204 |
+--------+----------+-----+---------+
6 rows in set

mysql> desc aa;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| emp_id | int(11) | NO | | 0 | |
| emp_name | varchar(50) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| dept_id | int(11) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set

#带EXISTS关键字的子查询(结果存在就返回true,不存在就返回false)
mysql> select * from employee where exists (select dept_name from department where dept_id=203);
+--------+----------+-----+---------+
| emp_id | emp_name | age | dept_id |
+--------+----------+-----+---------+
| 1 | A | 19 | 200 |
| 2 | B | 26 | 201 |
| 3 | C | 30 | 201 |
| 4 | D | 24 | 202 |
| 5 | E | 20 | 200 |
| 6 | F | 38 | 204 |
+--------+----------+-----+---------+
6 rows in set

#不存在就返回false
mysql> select * from employee where exists (select dept_name from department where dept_id=205);
Empty set

mysql命令用法复习笔记的更多相关文章

  1. MySQL数据库设计复习笔记及项目实战

    最近手头上有3个项目开动,其他2个都是从底层开始的,一个已经开始了一段时间的了,在小城市小团队开发的条件下,都没有专门的DBA来做数据库的设计和维护,往往都是开发人员顶上,可是看了很多的数据库的设计, ...

  2. Mysql导入导出工具Mysqldump和Source命令用法详解

    Mysql本身提供了命令行导出工具Mysqldump和Mysql Source导入命令进行SQL数据导入导出工作,通过Mysql命令行导出工具Mysqldump命令能够将Mysql数据导出为文本格式( ...

  3. [转]Mysql导入导出工具Mysqldump和Source命令用法详解

    Mysql本身提供了命令行导出工具Mysqldump和Mysql Source导入命令进行SQL数据导入导出工作,通过Mysql命令行导出工具Mysqldump命令能够将Mysql数据导出为文本格式( ...

  4. mysql命令行的基本用法

    基础介绍:1.在linux下使用下列命令,请确认mysql的bin目录是否已经加入到PATH路径中,或者是已经进入到mysql安装路径下的bin目录查看PATHshell> echo $PATH ...

  5. mysql 常用命令用法总结积木学院整理版

    一.启动与退出 1.进入MySQL:启动MySQL Command Line Client(MySQL的DOS界面),直接输入安装时的密码即可.此时的提示符是:mysql> 2.退出MySQL: ...

  6. diff 命令用法--如何打补丁【原创--学习笔记】

    diff 命令用法 1.”-u”:表示在比较结果中输出上下文中一些相同的行,这有利于人工定位 2.“-r“:表示递归比较各个子目录下的文件 3.“-N“:将不存在的文件当作空文件 4.“-w“:忽略对 ...

  7. (笔记)Mysql命令mysql:连接Mysql数据库

    mysql命令用户连接数据库. mysql命令格式: mysql -h主机地址 -u用户名 -p用户密码 1) 连接到本机上的MYSQL首先打开DOS窗口,然后进入目录mysql\bin,再键入命令m ...

  8. MYSQL复习笔记2-自带工具介绍

    Date: 20140102Auth: Jin 一.mysql 命令行客户端1)base-h host-P port--socket=path,-S path用于连接的套接字文件替换使用IP PORT ...

  9. mysql 命令笔记

    添加密码 mysqladmin -uroot -p password 123456 创建用户只能在10.0.0.0网段下访问数据库grant select,create,insert,update o ...

随机推荐

  1. JQuery重定向

    window.location.href = "这里写页面的路径"; 如:window.location.href ="www.baidu.com";

  2. python3之Django内置模板标签和过滤器

    一.模板标签 内置标签: 1.autoescape 控制当前的自动转义行为,此标记采用on或者off作为参数,并确定自动转义是否在块内有效.该块以endautoescape结束标签关闭. views: ...

  3. CSUOJ 1973 给自己出题的小X DFS

    Description 小X学习了dfs,为了练习搜索,开始给自己出题玩. 玩着玩着,一会把自己难住了,一会又被自己难倒了,真是有趣诶! 小X出的题: 现在有N个不同的正整数,求它们可以组成多少个这样 ...

  4. springmvc遇见406错误的问题分析

    如果springmvc遇到406错误: 90%没有加入Jackson的包 10%因为后缀为.html 10%的情况,解决方案为加多一个映射,使用.action

  5. 模板优化 运用 function 及 外部模板

    我们都知道模板是泛型的,但是,它一旦被实例化就会产生一个实例化的副本. 好了,大家应该能够猜到,低效模板和高效模板的差异了 一般的低效模板: 1.泛型实参表达形式多样导致的低效模板 2.多文件引用同一 ...

  6. JAVA内存泄漏解决办法

    JVM调优工具 Jconsole,jProfile,VisualVM Jconsole : jdk自带,功能简单,但是可以在系统有一定负荷的情况下使用.对垃圾回收算法有很详细的跟踪.详细说明参考这里 ...

  7. Java 中的浮点数取精度方法

    Java 中的浮点数取精度方法 一.内容 一般在Java代码中取一个double类型的浮点数的精度,四舍五入或者直接舍去等的方式,使用了4种方法,推荐使用第一种,我已经封装成工具类了. 二.代码实现 ...

  8. 冒泡排序(高级版)之C++实现

    冒泡排序(高级版)之C++实现 一.源代码:BubbleSortHigh.cpp #include<iostream> using namespace std; /*定义输出一维数组的函数 ...

  9. 家谱(gen)

    家谱(gen) 时间限制  2S [问题描述]     现代的人对于本家族血统越来越感兴趣,现在给出充足的父子关系,请你编写程序找到某个人的最早的祖先. [输入格式]gen.in 输入文件由多行组成, ...

  10. 【Trie】【枚举约数】Codeforces Round #482 (Div. 2) D. Kuro and GCD and XOR and SUM

    题意: 给你一个空的可重集,支持以下操作: 向其中塞进一个数x(不超过100000), 询问(x,K,s):如果K不能整除x,直接输出-1.否则,问你可重集中所有是K的倍数的数之中,小于等于s-x,并 ...