当前位置:Gxlcms > 数据库问题 > MySQL常用语法

MySQL常用语法

时间:2021-07-01 10:21:17 帮助过:16人阅读

insert into classes(class_no,class_name,department_name) values(null,2017自动化1班,机电工程); --如果只给部分字段的值(括号内要指明需要给值的字段) insert into classes(class_name,department_name) values(2017自动化2班,机电工程); --如果添加的字段与表的字段相同则不用写特定的字段 insert into classes values(null,2017自动化3班,机电工程);

# 插入多行数据

create table stu like student;
--以一个表的数据复制到另一个表()
insert into stu(student_no,student_name,student_contact)
select student_no,student_name,student_contact 
from student;

 6.2 update语句

 使用update语句可以对表的一行或多行进行修改,

语法:

update 表名 set 字段名1=值1,字段名2=值2,…,字段名n=值n [where 条件表达式];

  --更新对子表,主表都有影响
update score set c_score=c_score + 5;
update score set c_score=100 where c_score>100;
update stu set student_name=张三丰,class_no=2 where student_no=2017001;

--班号(class_no)影响以下修改会出错
update student set student_name=‘test‘,student_contact=‘1111‘,class_no=10
where class_no=1;
update classes set class_no=4 where class_no=1;

6.3 delete语句 

如果表中的某条(某几条)记录不再使用,可以使用 delete语句删除,

语法格式为: delete from 表名 [where 条件表达式]; 删除表记录时,需要注意表之间的外键约束关系

delete from stu where student_no=2017001;

6.4 truncate语句 

truncate table用于完全清空一个表,语法格式如下:

truncate [table] 表名; truncate语句和delete语句的区别:

使用truncate语句清空父表,将会失败

使用truncate语句清空表记录,会重置自增型字段的计数器 

 -- 创建测试表
  create table test(
    id int auto_increment primary key,
    name varchar(10));
  -- 插入测试数据
  insert into test(name) values(张三);
  insert into test(name) values(李四);
  insert into test(name) values(王五);
  -- 使用delete 删除数据时,自增长字段继续编号
  delete from test;
  insert into test(name) values(张三); -- id为4
  -- 使用truncate截断表,自增长字段重新编号
  -- truncate语句(字段没有外键约束的情况下才能使用)
  truncate table test;
  insert into test(name) values(张三); -- id为1

truncate语句不支持事务的回滚

7. DQL语句

7.1  select 字段列表 from 数据源 [ where 条件表达式] 

 select * from student;
 select student_no,student_name from student;
 select student_no as 学号,student_name 姓名 from student;

[ group by 分组字段[ having 条件表达式]] 

[ order by 排序字段 [asc | desc]] 

可以使用关键字as为字段或表达式命名别名 

语法: 字段(或表达式)[as] 别名

7.2  使用distinct过滤重复记录 

distinct 字段名

  select department_name 院系 from classes;
  select distinct department_name 院系 from classes;
  select distinct class_no,department_name 院系 from classes;

7.3   使用limit实现分页,语法格式如下:

select 字段列表 from 数据源 limit [start, ]length; 其中,

start表示从第几行记录开始检索 length表示检索多少行记录

以系统数据库information_schema中的tables为例:
  desc information_schema.tables;
  # 列出第一页(每页10行)
  select table_schema,table_name,table_rows from information_schema.tables limit 10;
  # 列出第二页
  select table_schema,table_name,table_rows from information_schema.tables limit 10,10; 

7.4 表连接

 使用join...on实现表连接

from 表名1 [连接类型] join 表名2 on 连接条件   

inner关键字可省略

select student.student_no 学号,student.student_name 姓名,classes.class_name 班级,classes.department_name 学院
  from classes inner join student 
     on student.class_no = classes.class_no;

可以给表名指定别名 如果两张表中有同名字段,字段名前需要用表名作前缀 

   # 表的别名
    select s.student_no 学号,s.student_name 姓名,
       c.class_name 班级,c.department_name 学院
     from classes c inner join student s
       on s.class_no = c.class_no;

 

 

 

 

 

MySQL常用语法

标签:win   创建   系统数据   commit   arc   font   ref   root   order   

人气教程排行