MySQL(零基础)详解之DQL

目录

一、DQL

1.1、语法结构

1.2、着重号

1.3、SQL简单使用

二、别名查询

2.1、给字段名取别名

2.2、给表取别名

三、去重查询(distinct)

四、单表查询

4.1、算术运算符

4.2、比较运算符

4.3、逻辑运算符

4.4、范围和集合

4.5、模糊查询

4.6、统计查询

4.7、分页查询(limit)

4.8、分组查询

4.9、排序查询

 五、多表查询

5.1、笛卡尔积

5.2、关联查询

5.3、内连接

5.4、外连接

5.4.1、左外连接

5.4.2、右外连接

5.4.3、全外连接(union)

 5.5、自连接

六、子查询

6.1、where 型子查询

6.2、from 型子查询

6.3、exists 型子查询

6.4、复制表子查询

6.4.1、复制表结构

6.4.2、复制表结构和数据

6.4.3、复制表结构+数据

6.4.4、跨数据库复制

6.4.5、复制表中部分字段和数据

6.4.6、在创建表的同时定义表的字段


一、DQL

DQL 是数据查询语言,用于查询数据,它是我们在真正的开发中使用最多的一种。而我们项目用 得最多的也是查询。而在 SQL 中, DQL 所占比例也是最大的,结构也是最复杂的。

1.1、语法结构

select 字段列表
from 表名或视图
[where 条件]
[group by 字段列表
[having 分组条件]]
[order by 字段 [asc | desc]] 
[limit m, n]

语法说明:

(1)使用 select 关键字来做查询;

(2)字段列表是使用英文逗号来进行分割

(3)from关键字后跟表的,可以是多个表名称或视图名称;

(4)group by:用于分组查询;

(5)having:用于结分组查询时进行过滤——条件的筛选;

(6)order by:用于数据显示时的排序;

(7)asc:表示长序,它是默认值;

(8)desc:表示降序;

(9)limit:用于分页显示,它有两个参数,第一个参数是起始值,第二个参数是显示的数量。

1.2、着重号

有些字段或表名可以使用了数据库的关键字或保留,这时我们就可以使用着重号(也叫反引号``)来进行着重标明

示例:

select `name` from t_stu;

1.3、SQL简单使用

示例一:查询员工表中所有行所有列

select * from t_employee;
使用 * 号来表示表中所有的字段,即查询所有行所有列的数据。

 注意:在实际开发中,不建议在SQL中使用 *

示例二:查询部分字段

select eid,ename,gender from t_employee;

示例三:查询员工表中性别为男的数据

select eid,ename,gender from t_employee where gender='男';

 

二、别名查询

在查询某个字段或某张表时,我们可以给这个字段或这张表取一个别名,这种查询就叫别名查询。使用别名查询时,需要用到一个关键字叫 as ,但这个关键字在书写时是可以省略。

2.1、给字段名取别名

示例: 查询员工表中姓名以及手机号,要求字段名取别名
select ename as '员工姓名',tel '手机号' from t_employee;

2.2、给表取别名

select eid, ename as name, gender, tel from t_employee as e;
为了验证表的别名已经起作用了,我们可以把别名加到字段的前面,作为字段的前缀。
select t_employee.eid, ename as name, gender, tel from t_employee as e;
上面的语句会报错,因为 t_employee 表已经被改为了 e 了,所以要修改为如下语句:
select e.eid, ename as name, gender, tel from t_employee as e;
在实际使用中,我们不推荐使用这种简短的别名,因为这种名称没有意义,最后是取一个见名知意的别名。

三、去重查询(distinct)

有时在查询时,我们希望查询出来的数据没有重复的数据,这时就可以使用 distinct 关键字来实 现。
示例: 查询员工表中的性别数据
select gender from t_employee;

执行完上面的语句后,我们的需求是已经实现了,但是有问题,性别只需要男或女即可,不需要这些重复的数据。这时就需要去重查询。

select distinct gender from t_employee;

 这个关键字好用,但是要慎用。因为它会做全表扫描,然后再把数据进行去重后再展示出来。比 较耗性能。

注意:这个关键字尽量少用或不用。

四、单表查询

4.1、算术运算符

算术运算符: + - * / (除也可以写成 div div 取整 ) % (取模可以写成 mod
示例一: 筛选出 eid 是奇数的员工
select eid,ename,gender,tel from t_employee where eid%2=1;

 示例二:筛选出eid除以2后等于1的数据

select eid,ename,gender,tel from t_employee where eid div 2=1;

4.2、比较运算符

比较运算符: = > >= < <= != (不等于还可以写成 <> <=> (安全等于)
示例一: 查询基本工资不等于 10000
select * from t_salary where basic_salary != 10000;

示例二: 查询 commission_pct 等于 null 数据

select * from t_salary where commission_pct = null;
从查询结果发现没有任何数据被查询出来,但是我们知道是有数据的值是 null 。那为什么不能正常的查询出来呢?
原因在于做 null 查询时,不能使用 = 来匹配 null 值,而是要使用 is 来进行匹配。
(1) is :匹配等于
(2)is not :匹配不等于
select * from t_salary where commission_pct is null;

以后在对 null 进行查询时,不要使用等号(=)或不等号( != ),要使用 is is not 来进 行筛选。

4.3、逻辑运算符

逻辑运算符包括与( && )、或( || )、非( not )操作。
示例一: 查询性别为男,并且在  1990 年1月2日 以前出生的员工
select eid,ename,gender,birthday from t_employee where gender='男' and birthday < '1990-01-02';

 上面的 SQL 语句也可以写成如下形式:

select eid,ename,gender,birthday from t_employee where gender='男' && birthday < '1990-01-02';

示例二:查询职位编号 job_id 1 2 的员工

select job_id, job_name, description from t_job where job_id=1 or job_id=2;

 上面的SQL语句也可以写成如下形式:

select job_id, job_name, description from t_job where job_id=1 || job_id=2;

4.4、范围和集合

范围查询需要使用到 between...and... 来查询,而集合就需要使用 in not in 来查询。
示例一: 查询基本薪资在 9000~12000 之间的员工编号和基本薪资。
select eid, basic_salary from t_salary where basic_salary between 9000 and 12000;

 注意:between...and... 查询时,条件是一个闭区间查询。并且后一个值要大于前一个值。

上面的SQL语句和一面这条SQL语句执行结果是一样的。

select eid,basic_salary from t_salary where basic_salary>=9000 and basic_salary<=12000;

示例二:查询 eid 的值为 135的基本薪资。

select eid,basic_salary from t_salary where eid=1 or eid=3 or eid=5;

上面的SQL语句是可以正确的查询出我们想要的结果。但是这种查询效率很低,我们不推荐SQL 使用 or 查询,因为它会做全表扫描

一般来说,使用 or 查询的地方都可以使用 in not in 来代替,这也是 SQL 优化中的一种方
式。
select eid,basic_salary from t_salary where eid in (1,3,5);
以上的 SQL 执行后,效果相同,但是它性能要比 or 查询高。
注意:使用 in 查询有局限性,它只支持数值类型和字符串类型。

4.5、模糊查询

模糊查询需要使用 like 关键字,也可使用 not like 来进行查询。要模糊查询中,我们还可以使用
通配符:
(1)% :表示 0 ~ n 个字符
(2)_ :表示一个字符
示例一:查询员工姓名中包含有 ' ' 的员工信息
select eid,ename,gender,tel from t_employee where ename like '%孙%';

上面 SQL 语句能够正常的查询出我们想要的数据,但是性能不好,它会进行全表扫描。我们可以查询以 ' ' 开头的员工。
select eid,ename,gender,tel from t_employee where ename like '孙%';
上面的 SQL 语句表示姓名是以 孙 开头的员工信息。
我们还可以使用下划线来进行匹配,一个下划线代表一个字符。
select eid,ename,gender,tel from t_employee where ename like '孙_';

 上面的SQL语句表示姓名是以孙开头,并且名字中只有两个字。

4.6、统计查询

SQL 中,统计查询一般会用到如下几种:
(1)sum() :用于求和统计
(2)avg() :用于求平均值
(3)count() :用于获取总记录数
(4) max() :用于获取最大值
(5)min() :用于获取最小值
示例一:查询员工的平均工资。
select avg(basic_salary) as '平均工资' from t_salary;

它的内部实际是把基本工资总和起来后再除以员工数得到值。
示例二:获取工资表中最高的工资。
select max(basic_salary) as '最高工资' from t_salary;

示例三:获取工资表中最低的工资

select min(basic_salary) as '最低工资' from t_salary;

 示例四:获取所有员工的工资总和

select sum(basic_salary) as '工资总和' from t_salary;

 示例五:获取员工总数

select count(*) as '员工总数' from t_salary;

使用 count() 来进行查询时,参数不能为空。参数可以是数字、字符、星号、某个字段,结果都一样。

4.7、分页查询(limit)

MySQL 查询中,要想实现分页查询,我们只需要提供 limit 关键字即可。这个关键字在使用时需要提供两个参数,语法格式如下:
limit offset,pagecount;
参数说明:

(1)offset:它是分页的起始偏移值,它需要计算;

(2)pagecount:显示的记录数

示例一:分页显示员工信息,每页显示 2 名员工。

第一页的数据
select eid,ename,gender,tel,birthday from t_employee limit 0, 2;

第二页的数据:
select eid,ename,gender,tel,birthday from t_employee limit 2, 2;

 

 第三页数据:

select eid,ename,gender,tel,birthday from t_employee limit 4, 2;

 从上面的三条SQL语句可以发现,唯一变化的是 offset 这个参数,而这个参数的计算是根据页码和每页显示的记录数来计算而得。它的计算公式为:

offset = (当前页码 - 1) * 每页显示记录数
offset = (page - 1) * pagecount

4.8、分组查询

分组查询需要使用 group by 关键字来进行分组,还可以能使用 having 关键字来进行条件筛选。

示例一:根据员工的职位来分组查询员工信息。

select eid,ename,gender,job_id from t_employee group by job_id;
执行上面的语句时,报了如下错误:
[42000][1055] Expression #1 of SELECT list is not in GROUP BY clause and
contains nonaggregated column 'mydb.t_employee.eid' which is not
functionally dependent on columns in GROUP BY clause; this is incompatible
with sql_mode=only_full_group_by
解决以上的错误的方式是把 select 中查询的字段放到 group by 的字段列表中即可。
select eid,ename,gender,job_id from t_employee group by eid,ename,gender,job_id;
这时再次查询时就不会报错误信息,并且能够查询出数据。

上面查询出来的数据并不是我们希望得到数据,我们是希望根据员工的职位来进行分组。
select job_id from t_employee group by job_id;

 这次的结果确实是按员工的职位来进行分组,但是我们看不到每职位的员工有多少个。

从上面分析过程可以发现:我们的分组字段是可以写在 select 列表中的,还有统计查询的 一些函数也是可以写在分组查询中的 select 列表中的,其它的字段不能写在 select 列表 中。

示例二: 根据部门编号统计员工人数
select dept_id, count(*) from t_employee group by dept_id;

示例三:根据部门编号统计员工人数,要求统计的部门编号大于1

select dept_id, count(*) from t_employee group by dept_id having dept_id > 1;
注意:当 SQL 语句中既有 where 条件,又有 having 条件时,先执行 where 条再执行 having条件。
select dept_id, count(*)
 from t_employee
 where gender = '男'
 group by dept_id
 having dept_id in (1,2);

 从 SQL 优化角度,我们最好是定义 where 条件来过滤大量的数据后,再使用 having 来进行过滤,这样的话性能就会高很多。

4.9、排序查询

排序查询会使用到 order by 关键字,还会使用到 asc desc 关键字。
示例一: 查询所有员工信息,并按员工的编号的升序进行显示
select eid,ename,gender,tel,birthday from t_employee;
select eid,ename,gender,tel,birthday 
from t_employee 
order by eid desc ;

 题目的要求是按员工的编号进行升序显示,因此在 SQL 就应该使用 order by 语句。

select eid,ename,gender,tel,birthday from t_employee 
order by eid asc;

 从上面两条 SQL 语句执行的结果可以发现,结果是一样的,也就是说,我们在查询时,默认就是按照主键的升序进行显示的。

如果是按主键的降序进行显示,那么就必须要使用 order by 语句了,并且指定为 desc
select eid,ename,gender,tel,birthday 
from t_employee 
order by eid desc ;

 示例二:按员工的基本工资的高低来进行显示

select eid, basic_salary 
from t_salary 
order by basic_salary desc;

对于排序的字段也是可以有多个的,当有多个时,会优先对第一个进行排序,只有当第一个的值相同时,才会对第二个值进行排序,依次类推。

示例三:按员工的职位高低进行排序,如果职位相同再按员工的编号升序进行排序。

select eid,ename,gender,job_id
from t_employee
order by job_id
desc,eid asc;

 五、多表查询

5.1、笛卡尔积

什么是笛卡尔积,它其实是数学中的一个概念。当有多张表进行组合查询时,如果条件不准确,那么查询的结果就是这几张表中数据的乘积数。这种方式就是笛卡尔积。
笛卡尔积示例:查询员工姓名和所在部门名称
select ename,dname from t_employee,t_department;

 从上面的查询结果可以发现,我们在做关联查询时, 要尽量避免出现笛卡尔积的情况出现。

5.2、关联查询

表的连接查询的约束条件有三种方式:

(1)where:适用于所有的关联查询。

(2)on只能和 join一起使用,只能写在关联条件中,但是它可以和 where 一起使用。

(3)using只能和 join 一起使用,并且要求两个关联的字段在关联表中名称一致

示例一:把关联条件写在 where 语句中

select ename,dname 
from t_employee,t_department 
where dept_id=did;

 示例二:把关联条件写在 on 语句中

select ename,dname
from t_employee join t_department on dept_id=did;

 两条查询的结果一样。

示例三:查询员工姓名和它的基本工资信息

select ename,basic_salary 
from t_employee as e,t_salary as s 
where e.eid=s.eid;

select ename,basic_salary 
from t_employee e join t_salary ts on e.eid = ts.eid;

 

select ename,basic_salary 
from t_employee join t_salary using (eid);

 

注意:要想使用 using,那么必须要关联查询相关表中被关联的条件字段名称必须相同。

示例四:查询员工姓名、基本工资和所在部门名称

这个示例涉及到三张表的关联查询:

(1)员工姓名:在 t_employee 表中

(2)基本工资:在 t_salary 表中

(3)部门名称:在 t_deparment 表中

员工表和基本工资表的关联条件是 eid。
员工表和部门表的关系条件是员工表中的 dept_id 字段指向的是部门表中的 did 字段。
工资表和部门表没有关联条件。
select ename,basic_salary,dname 
from t_employee e join t_salary using(eid) 
                  join t_department td on td.did = e.dept_id;

5.3、内连接

MySQL 中内连接( inner join )有两种:显示的内连接和隐式的内连接,返回的结果是符合条件的所有数据。
隐式的内连接语法为:
select [columns] 
from 表1,表2,... 
where [condition];
显式的内连接语法为:
select [columns] from 表1 inner join 表2 on [condition] where [其它条件]; 

select [columns] from 表1 cross join 表2 on [condition] where [其它条件];

select [columns] from 表1 join 表2 on [condition] where [其它条件];

 执行关联查询后的结果为如上图所示的红色区域内的数据。也就是两张表中共同的部分。

示例一: 查询员工姓名和部门名称
select ename,dname
from t_employee e inner join t_department td on td.did = e.dept_id;

select ename,dname
from t_employee e join t_department td on td.did = e.dept_id;
在使用 inner join 时,我们可以把 inner 关键字省略,只写 join 即可。
select ename,dname
from t_employee e cross join t_department td on td.did = e.dept_id;

5.4、外连接

外连接( outer join )在 MySQL 有以下几种外连接:
(1) 左外连接( left outer join ):简称左连接( left join
(2) 右外连接( right outer join ):简称右连接( right join
(3)全外连接( full outer join ):简称全外连接( full join

5.4.1、左外连接

左外连接是以左表要基准表,如果右表中没有数据和左表匹配,则以空值( null 值)为填充。
为了演示上面的效果,我们去添加一个新的部门。
insert into t_department(dname, description) values('财务部', '发工资部门');

 示例一:查询所有部门信息以及部门员工信息

select did,dname,description, ename,gender,tel 
from t_department td left join t_employee te on td.did=te.dept_id;

 如果以员工表为左表,那么查询的结果就不一样:

select ename,gender,tel,did,dname,description
from t_employee te left join t_department td on te.dept_id=td.did;

上面的示例是返回左表中所有行,如果左表中的行在右表中没有匹配的数据,则会以空值来填充。

接下来我们来演示左表中行在右表中没有匹配的的记录。

示例二:查询部门信息,仅保留没有员工的部门信息
select did,dname,description,eid,ename,tel,dept_id 
from t_department td left outer join t_employee te on td.did = te.dept_id 
where te.dept_id is null;

 示例三:查询所有员工信息,以及员工所在的部门信息

为了演示这个示例,我们去添加一名新员工。
insert into t_employee(ename) values('刘备');

5.4.2、右外连接

(1)返回右表中所有行,如果右表中的行在左表中没有匹配的行,则左表中以空值来填充。
示例一: 查询所有部门以及该部门的员工信息。
select eid,ename,gender,did,dname,description 
from t_employee e right join t_department d on e.dept_id=d.did;

(2)查询右表在左表中没有的数据。

  示例二:查询没有员工的部门信息。

select eid,ename,gender,did,dname,description 
from t_employee e right join t_department d on e.dept_id=d.did 
where dept_id is null;

5.4.3、全外连接(union)

MySQL 中不支持全外连接( full join ),但是我们可以使用 union 关键字来代替,它代表联合查询的意思。
可以使用如下语法来代替:
select column_list 
from 表1 left join 表2 
on 条件 
union 
select column_list 
from 表1 right join 表2 
on 条件
把左外连接和右外连接联合起来就形成了全外连接了。

(1) 查询所有部门和员工信息
select did,dname,eid,ename,gender,tel 
from t_department d left join t_employee te on d.did = te.dept_id 
union 
select did,dname,eid,ename,gender,tel 
from t_department d right join t_employee te on d.did = te.dept_id;

 (2)查询所有没有员工的部门和没有部门的员工信息

select did,dname,eid,ename,gender,tel 
from t_department d left join t_employee te on d.did = te.dept_id
where te.dept_id is null 
union
select did,dname,eid,ename,gender,tel 
from t_department d right join t_employee te on d.did = te.dept_id 
where te.dept_id is null;

 5.5、自连接

当表 1 和表 2 是同一张表时,只是用了别名的方式来虚拟两张表进行关联查询时,就形成了自连接。在自连接中也是可以使用内连接和外连接的。
(1) 查询员工姓名和它的领导的姓名,仅显示有领导的员工。
示例:修改员工的领导
update t_employee set mid=1 where eid=7;

此时孙红雷就是刘备的上级。
select e1.ename, e2.ename 
from t_employee e1, t_employee e2 
where e1.mid=e2.eid;

 上面的方式我们使用隐式的内连接来实现的自连接查询。

还可以有如下的写法:
select e1.ename, e2.ename 
from t_employee e1 join t_employee e2 on e1.mid=e2.eid;
上面的方式我们使用显式的内连接来实现的自连接查询。

六、子查询

在某些情况下,我们在做查询时,需要的条件或数据是另一个查询的结果,这时就用到子查询。
MySQL 中,子查询有以下几种:

(1)where 型子查询:把子查询作为 where 的条件(条件)

(2)from 型子查询:把子查询作为 from 的临时表(数据)

(3) exists 型子查询:把子查询作为判断是否存在的条件(条件)
(4) copy 型子查询:用于复制表数据或结构

6.1、where 型子查询

where 型子查询即把内层 sql 语句的查询结果作为外层 sql 的查询条件。
注意以下几点:
(1)子查询要包含在括号内
(2) 建议将子查询放在比较条件的右侧
(3) 单行操作符对应单行子查询,多行操作符就对应多行子查询
示例一:查询比孙红雷的工资高的员工信息
第一步:查询孙红雷的工资
select e.eid,ename,basic_salary 
from t_employee e, t_salary s 
where e.ename='孙红雷' and e.eid=s.eid;

 第二步:查询工资比 12000 高的员工编号

select eid,basic_salary from t_salary 
where basic_salary > 12000;

 第三步:查询员工编号为 4 6 的员工信息

select eid,ename,gender,tel 
from t_employee 
where eid in (4,6);

 根据上面三步的分析可以发现,我们所需要的条件都是另一个 SQL 语句的返回结果中,因此,我们可以使用子查询来实现上面三步的操作。

select eid,ename,gender,tel
from t_employee
where eid in (
    select eid
    from t_salary
    where basic_salary > (
        select basic_salary
        from t_employee e, t_salary s
        where e.ename='孙红雷' and e.eid=s.eid
        )
);
上面的 SQL 语句执行的结果是:

示例二: 查询和孙红雷、鹿晗在同一个部门的员工
第一步:查询孙红雷和鹿晗所在的部门编号
select eid,ename, dept_id 
from t_employee 
where ename in ('孙红雷','鹿晗');

第二步:查询部门编号为 1 的员工信息
select eid,ename,gender,tel,dept_id 
from t_employee 
where dept_id in (1,1);

 根据上面的分析,我们把第一步查询到的数据作为第二步查询所使用的条件即可。

select eid,ename,gender,tel,dept_id
from t_employee
where dept_id in (
    select dept_id
    from t_employee
    where ename in ('孙红雷','鹿晗')
);

执行结果一样

6.2、from 型子查询

from 型子查询是把内层 SQL 语句查询的结果作为外层 SQL 查询的数据。
示例:查询出比部门平均工资高的员工编号和基本工资。
第一步:查询出部门的平均工资
select dept_id, avg(basic_salary) 
from t_employee e, t_salary s 
where e.eid=s.eid 
group by dept_id;

通过上面的SQL语句,我们可以正确得到部门的平均工资。但是有一个小的问题:我们从查询的结果可以看到,平均工资的字段名称为 avg(basic_salary) 。这个名称不便于我们使用,为了便于后续使用,我们经这个字段取一个别名,例如叫 avg_salary

select dept_id, avg(basic_salary) as avg_salary 
from t_employee e, t_salary s 
where e.eid=s.eid group by dept_id;

 通过这样调整后,后续使用就比较方便了。

第二步:查询员工信息和基本工资。
select e1.eid, dept_id,basic_salary 
from t_employee e1 join t_salary s1 
on e1.eid=s1.eid 
and dept_id in (1, 2);

 第三步:查询比部门平均工资高的员工和基本工资(需要把前面两步进行结合)

select te.eid, te.ename, basic_salary 
from t_salary ts join t_employee te
    on ts.eid=te.eid 
join (
      select dept_id, avg(basic_salary) as avg_salary 
      from t_employee e, t_salary s 
      where e.eid=s.eid 
      group by dept_id 
      ) as temp 
on te.dept_id=temp.dept_id 
where ts.basic_salary > temp.avg_salary;

6.3、exists 型子查询

exists 子查询基本都是出现在 where 语句中,用于判断某些条件是否成立。
示例:查询必须有员工的部门信息
第一步:查询有员工的部门
select eid,ename,gender,dept_id 
from t_employee 
where dept_id is not null;

 第二步:在第一步的基础上来实现最终的功能

(1) 测试子查询中有数据
select * from t_department 
where exists(select * from t_employee);

(2)测试子查询中没有数据

select * from t_department 
where exists(select * from t_employee where eid > 10);

 根据上面的测试情况可以发现,我们子查询的结果不能为空,如果为空则外层查询就没有数据了;如果子查询不为空,则外层查询就会有数据。

最终的查询语句为:
select did,dname,description 
from t_department 
where exists( 
    select eid,ename,gender,dept_id 
    from t_employee 
    where dept_id is not null 
);

上面的结果中包含了没有员工的部门——财务部,这显然不符合我们的题要求,原因在于我们子查询中条件给的不对,因为没有我外部表进行关联,导致条件不符合。

select did,dname,description 
from t_department td 
where exists( 
    select eid,ename,gender,dept_id 
    from t_employee te 
    where td.did=te.dept_id 
);

 此时就得到了最终的结果了。

6.4、复制表子查询

复制表子查询分为两种情况:一种是复制表;另一种是复制数据。

6.4.1、复制表结构

语法格式:
create table 表名 like 被复制的表名;
示例:复制 t_stu 表结构。
t_stu 表的结构如下: desc t_stu
 create table t_stu_new like t_stu; 
执行完上面语句后,再执行:
desc t_stu_new;

6.4.2、复制表结构和数据

语法格式:
create table 表名 as (select * from 被复制的表名);
示例:复制 t_stu 表结构和数据
create table t_stu_new_2 as (select * from t_stu);

desc t_stu_new_2;

select * from t_stu_new_2;

注意:这种语法可以复制表结构和数据,但是复制出来的表中不包含原表中的约束索引

6.4.3、复制表结构+数据

这种方式是先使用  6.4.1  节的语法来复制表结构,然后再使用如下语法来添加数据。
语法格式:
insert into 复制后的表名 select * from 被复制的表名;
示例:复制 t_stu 表结构和数据
create table t_stu_new_3 like t_stu;

 再向这个表添加数据

insert into t_stu_new_3 select * from t_stu; 

select * from t_stu_new_3;

6.4.4、跨数据库复制

语法格式为:

create table 表名 like 被复制数据库名.被复制的表名; 

create table 新数据库名.表名 like 被复制数据库名.被复制的表名;

6.4.5、复制表中部分字段和数据

语法格式为:
create table 表名 as ( 
select 要复制的字段列表 
from 被复制的表名 
where 条件 
)
示例:复制 t_stu 表中 id,name 字段到 t_stu_new_4 表中
create table t_stu_new_4 as ( 
select id,name 
from t_stu 
where id=1 
)
使用如下语句来查看表结构:
desc t_stu_new_4;

 再查看数据

lect * from t_stu_new_4;

6.4.6、在创建表的同时定义表的字段

 语法格式为:

create table 表名(
 字段名 类型(长度) [主键][自增][非空][默认值][注释] 
) as (
select 被复制的字段 from 被复制的表名 
)
示例:
create table t_stu_new_5(
id int(11) primary key auto_increment
) as (
select id,name from t_stu );



desc t_stu_new_5;

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值