zoukankan      html  css  js  c++  java
  • 数据库之四、数据的增删改查

    1、数据的增(Insert ):



    数据的增也就是数据的插入,基本语法如下:

    Insert Into Table_Name (column1, column2, column3, …) Values (值1, 值2, 值3, …);

    比如我们向Student表中插入一组数据

    insert into Student(SID,Sname,Sage,Ssex) values('01' , '赵雷' , '1990-01-01' , '');
    insert into Student(SID,Sname,Sage,Ssex) values('02' , '钱电' , '1990-12-21' , '');
    insert into Student(SID,Sname,Sage,Ssex) values('03' , '孙风' , '1990-05-20' , '');
    insert into Student(SID,Sname,Sage,Ssex) values('04' , '李云' , '1990-08-06' , '');
    insert into Student(SID,Sname,Sage,Ssex) values('05' , '周梅' , '1991-12-01' , '');
    insert into Student(SID,Sname,Sage,Ssex) values('06' , '吴兰' , '1992-03-01' , '');
    insert into Student(SID,Sname,Sage,Ssex) values('07' , '郑竹' , '1989-07-01' , '');
    insert into Student(SID,Sname,Sage,Ssex) values('08' , '王菊' , '1990-01-20' , '');

    还可以通过外部数据清单插入数据,比如讲Excel表格中的数据插入到数据库中,这里不做讨论。
    对表进行全列 Insert时,可以省略表名后的列清单,也可插入Null值和默认值。
    从其它表中复制数据,可以用下面这样的语法:

    Insert Into Table_Name1 (column1, column2, column3,…) Select 值1, 值2, 值3, … from Table_Name2;


    2、数据的删(Delete):



    数据的删除的语法如下:

    Delete from Table_Name Where condition;

    如果没有where字句则是清空表格中的数据,有where字句则会根据字句中的条件删除数据;
    删除数据表则用

    drop table table_name;


    3、数据的改(Update):



    数据修改的语法如下:

    Update Table_Name set column=表达式 where condition;

    将符合where字句中条件的的数据按set字句的要求进行修改

    Update    Student set Sname=’李明’ where sname=’王菊’;

    这样就将名字为王菊的数据修名字改为成了李明


    4、数据的查(select):



    4.1、数据查询的基本语法:

    Select column1,column2 ... 
    from table_name 
    Where condition;

    如果我们想查询数据表中所有数据可以使用如下语法:

    Select * from student;

    可以为查询结果列取别名:

    Select sid as “学号” from student;

    这里关键字as可以省略,中文名需要用英文双引号(“ ”)括起来,有些数据库中不用双引号一样可以
    常数查询,可以直接写出来,比如

    Select sid,12 from student

    可以使用distinct删除结果中的重复数据行,distinct只能使用在第一个列名之前

    Select distinct * from student 

    Select字句需要根据where字句选取数据,where字句可以有各种各样的条件

    Select * 
    from student
    Where Ssex=''

    4.2、算数运算符:
    算数运算符可以对数值列进行算数运算

    select SID,CID,SCORE*0.8 from SC

     

    select SID,CID,SCORE from SC
    WHERE SCORE>70

    4.:3、空值:

    关于空值Null,空值的判断一般用is not null 和is null的方式,且空值不能与数值进行算数运算,结果都会是Null

    Select * from student where ssex is not null 


    4.4、比较运算符(<,>,<=,>=,=):

    select SID,CID,SCORE from SC
    WHERE SCORE>70


    对数值或者时间数据使用比较运算符可以选取出符合条件的数据
    对于字符串使用比较预算符,只是从第一个单词开始比较,然后再比较第二个字符
    比如1,2,3,10,50,123,12546这些数据的排序是1,10,123,12546,2,3,50而不是和数值比较相同
    4.5、逻辑运算符(not,and,or)

    select SID,CID,SCORE from SC
    WHERE not SCORE>70
    select SID,CID,SCORE from SC
    WHERE SCORE>70 and SCORE<90
     
    select SID,CID,SCORE from SC
    WHERE SCORE>70 or SCORE<30
    尽管很渺小,但终究会变得伟大
  • 相关阅读:
    1105 Spiral Matrix (25分)(蛇形填数)
    1104 Sum of Number Segments (20分)(long double)
    1026 Table Tennis (30分)(模拟)
    1091 Acute Stroke (30分)(bfs,连通块个数统计)
    1095 Cars on Campus (30分)(排序)
    1098 Insertion or Heap Sort (25分)(堆排序和插入排序)
    堆以及堆排序详解
    1089 Insert or Merge (25分)
    1088 Rational Arithmetic (20分)(模拟)
    1086 Tree Traversals Again (25分)(树的重构与遍历)
  • 原文地址:https://www.cnblogs.com/chenbao1012/p/11819186.html
Copyright © 2011-2022 走看看