第22讲

来源:互联网 发布:旺角揸fit人 知乎 编辑:程序博客网 时间:2024/06/05 00:55
今日结果:
     1  sqlite 数据库中一个学生表中 例如学号不能重复:
            create table stu ( id int unique , name text);   // create table stu(id int primary key , name text);
            create table stu( id int primary key , check (id > 100));//  当插入是id必须是大于100的。
            create  table stu (id int , name text, memo text default 'aa' );  //  default 为memo赋默认值'aa'    ....(  collation '姓名');
           insert into  stu[(id , name)] values (2 , 'stu2');          
      2  not null  表示不为空,(必填) ;  
           create table stu(id int , name text no null); 
           create table  stu (id int  identity(1,1) ,name text);  // identity(1,1) 从1 开始 加 1 自动编号
           select rowid form stu ;   // rowid  另加新的字段,为每天标记编号
           sqlite 是动态的数据类型存储。
       3  修改表(框架):
           alter  table stu add age int ;   
       4  视图 是查看数据表中的数据的一种方式, 增加安全性和保密性。
           sqlite> create view [database-name] v-stu as  select * from stu ; 
                                    那一个数据库(默认为当前)||视图的数据来源
       5  drop view v_stu ; // 不会删除来源数据stu  
            select name,memo  from stu ;  // 查询具体的字段
       6  索引INDEX :  提供指针以指向存储在表中指定列的数据值,之后可以排序这些指针
            优点: 快速访问数据, 加强数据行的唯一性
           缺点: 占用硬盘的存储空间,会产生其他开销(插入,修改,等)
           创建表时,有默认的数据类型是任意类型。
           create unique index stu_id on stu(id);  // 虽然stu表id是不唯一的,但经过所以之后他的id就是唯一的
           SQLite  不支持 :right out join / full out join  .// 左连接等      grant /revoke  授权(对用多用户)
        7  整理数据库(相当于电脑中的整理碎片)
                 VACUUM [ index -or table -name ]  
               一下情况下不能执行此操作:  不能对附加数据库文件进行操作; 若当前有活动事务,该命令无效; 对in - momery数据库,该命令无效。
        8   create table stu_bak as select * from stu where 1<0;  // 把stu表中的结构复制到stu_bak中(字段名,字段个数等)
             insert into stu_bak  select * from stu;  // 复制stu中的数据到stu_bak中
             insert into stu_bak(name) select name from stu;   // 复制指定的字段
             insert into sut_bak(name)  select 'aaa'  from stu;  //   为指定字段赋值常字符串
        9  更新记录(数据) :  update  <表名>  set <列 = 更新值>     [ where <更新条件>]   //  '<>' 表示必须有的,'[]' 可无有
                  update  stu set  id = 2 where  name = 'stu2';   //修改指定的一个字段
                  update stu set id =2 , name = 'stu4' where name = 'stu2';  // 多字段进行修改
        10   删除记录: delete  from  <表名>  [where <删除条件>]  ; 
                     delete  from stu where id = 2;   
                     select * from stu where  id>2  and id <4;    // and  | or | not  三中与 或 非
        11  通配符: '-'  一个字符的模糊通配操作  
                   select * from  stu wherer name like 'stu-'  ;//   like '--u-' 等
                  %  任意长度的字符串通配    B like ' co%'   |  '%2007%'  不知道2007 的具体位置查询
                  [ ]   括号中所指定范围内的一个字符 //  like ' stu[1-2] '     
                  [ ^]      不属于指定范围 ([a-f]) 或集合 ([abcdef]) 的任何单个字符。与 [ ]  相反
     between  2  and 4; //  相当于  >=2  and  <=4;
         12  排序:  order by  <desc>/ <asc>   默认为asc 升序排序(从上到下,由小到大)
                      select * from stu order by  id ;   //   order by id ,name ; 多字段排序(由左向右);(当有id相等时,name小的排在前面)
         13  distinst   将重复的行合并  :   select  classid  from stu;   
         14  复合语句操作: select a.id , a.name , a.core , b.name from stu a  ,  class  b  where  a.classid = b.id;//  多张表的关联
           select a.* from stu a , (select  max( classid) mymax from stu ) b where a.classid = b.mymax;   
          15    也可以自己连接自己;    select  max(classid) from stu ;   
                  //  查询最大值的函数max()
      //  substr (X ,Y,Z)  截取字符串  从Y开始,截取 Z个字符在X字符串中
                  // length ( X)  计算字符串长度
                 //  date  返回日期   ,  其他格式 datetime('now')  , time('now')  
                //  random (*)  返回随机数    
                // round (X [,Y])   //     round (123.4455,  2)   对第一个参数的保留2位小数  
                //  ifnull( X, Y)     X表达式为null 时 , 返回值为 Y
           16  select * from stu  where name  is null;   // 不能写为: name = null  或  name =''   
           17  分页  (数据库)
                  select * from  table name  limit  X  offset  Y
                    Y  :  从查询结果集开始偏移Y 位        X  : 限制显示的记录数
       select * from stu  limit  100  offset 2 ;


明日计划:首先,认真做好先前点餐的项目答辩 ,再 整体复习 C语言的知识 ,完成考试;  之后 完成老师布置的作业,然后,对C++的类进行进行复习。
感想:感觉今天是知识点最多的一次, 当然,可能这些只是一些数据库的语句吧 ,虽然有点不好记,但最终要通过不断的编写和使用,慢慢的就理解记住了。先前 学校有学过 SQL SERVER 2008  现在看起来 两者的语法几乎完全一样, 这次的学习使得自己能更加深刻的理解这些使用。今天,才明白一卡通 只要是大学城的大学都是可以消费的。真好,现在已经到了大学联盟的世纪哈~~  
0 0
原创粉丝点击