zoukankan      html  css  js  c++  java
  • MySQL 基础语句

    一、增加数据:

    insert 语句可以用来将一行或多行数据插到数据表中,使用的一般形式如下:

    insert into 表名(字段列表)values(值列表)

    insert[into]表名[(列名1,列名2,列名3,列名4,...)]values(值1,值2,值3,值4,...)

    insert into students values(NULL, "张三", "男", 20, "18889009876");

    有时我们只需要插入部分数据, 或者不按照列的顺序进行插入, 可以使用这样的形式进行插入:

    insert into students(name,sex,age)values("李四","女",21);

    1.添加数据:

    insert into stu(name,sex,age) values('张学友','男',18);
    insert into stu(name,sex,age) values('张娜拉','女',73);
    insert into stu(name,sex,age) values('张家辉','男',23);
    insert into stu(name,sex,age) values('张汇美','女',85);
    insert into stu(name,sex,age) values('张铁林','男',35);

    2.查询数据

    2.1、查询所有学生

    select * from stu;

    2.2、查询年龄大于80岁的女生

    select * from stu where age >80 and sex = "女";

    where 关键词用于指定查询条件, 用法形式为: select 列名称 from 表名称 where 条件;

    以查询所有性别为女的信息为例, 输入查询语句: select * from students where sex="女";

    eg:查询年龄在21岁以上的所有人信息:select * from students where age > 21;

    查询名字中带‘王’字的所有人信息:select * from students where name like "%王%";

    查询id 小于5并且年龄大于20的所有人信息:select * from students where id <5 and age>20;

    2.3、聚合函数:

    获得学生总人数:select count(*) from students

    获得学生平均分:select avg(mark)from students

    获得最高成绩:select max(mark)from  students

    获得最低成绩:select min(mark) from students

    获得学生总成绩:select sum(mark) from students

    3.删除数据

    delete from 表名[删除条件]

    删除表中所有数据:selete from students;

    删除id 为10的行:delete from students where id=10;

    删除所有年龄小于88岁的数据:delete from students where age<88;

    4.修改数据

     4.1、将编号为1 的学生年龄加1岁

    updata stu set age=age+1 where id =1;

    4.2、将80岁以上的女学生年龄修改成90岁并将姓名添加“老人”

    #CONCAT(str1,str2,...)连接字符串

    update stu set age = 90,name = CONCAT(name,'(老人)')where age >=80 and sex='女';

    4.3、将编号4的学生名字修改为‘张美丽’

    update stu set name = '张美丽' where id =4;

    4.4、删除数据

    删除年龄大于70岁的学生

    delete from stu where age >70;

    4.5、删除所有学生

    delete from stu;

    5.更新数据

    update 语句可用来修改表中的数据, 基本的使用形式为:

    update 表名称 set 列名称=新值 where 更新条件;

    Update 表名 set 字段=值 列表 更新条件

    将id为5的手机号改为默认的"-": update students set tel=default where id=5;

    将所有人的年龄增加1: update students set age=age+1;

    将手机号为 13723887766 的姓名改为 "张果", 年龄改为 19: update students set name="张果", age=19 where tel="13723887766";

    6.修改表

    alter table 语句用于创建后对表的修改,基本用法如下:

    6.1、添加列

    基本形式:alter table 表名add 列名 列数据类型[after 插入位置]

    eg: 在表的最后追加列 address: alter table students add address char(60);

    在名为 age 的列后插入列 birthday: alter table students add birthday date after age;

    6.2、删除列

    删除 age 列:alter table students drop age;

    6.3、重命名

    基本形式:alter table 表名 rename 新表名;

    eg: 重命名 students 表为temp: alter table students rename temp;

    6.4、删除表

    基本形式:drop table 表名;

    eg:删除students表:drop table students;

    6.5、删除数据库

    基本形式: drop database 数据库名;

    eg:删除lcoa数据库:drop database lcoa;

  • 相关阅读:
    第五周总结
    第四周总结
    第三周总结
    开课博客
    学习进度
    个人作业1-数组
    数组
    第一周考试总结
    团队个人冲刺第六天
    团队个人冲刺第五天
  • 原文地址:https://www.cnblogs.com/Adalia-Ting/p/11041328.html
Copyright © 2011-2022 走看看