• sql笔记(1)


    /*注意:SQL中不区分大小写的
    新增的:
    --备份数据库
    backup database database_name to disk='url\name.bak'--database_name 一定不能加单引号的‘’,另外为备份的数据库区别名.bak
    --回复还原数据库
    restore databse database_name from disk='url\name.bak'--还原的数据库名子可以自定义的
    --重命名数据库
    sp_renamedb 'database_oldName','database_newName' --更改数据库名称这里的单引号必须有的
    --重命名表
    sp_rename 'table_oldName','table_newName' --忘记两个表明之间的逗号
    --重命名列
    sp_rename 'table.字段名','new 字段名','column  '              
     --查看表的信息
    sp_help table_name  --1  
    sp_columns  table_name  --2
    sp_pkeys  table_name   --查看主键信息
    多列的主键约束
    alter table table_name add constraint name primary key(字段1,字段2) 
    删除主键
    alter table table_name drop constraint name 

    case
      when   then
      when   then
      else 
    end 
    数据转换
    1.cast cast(@a as varchar(10))
    2.conver (varchar(10),@a) 
    len('aaaa')--求字符的长度
    */


    --建库
    create database database_name
    --1建表(外键在允许为空的情况下是可以null的)
    create table table_name
    (
        stu_no int identity (1,1) ,
        stu_name char(10) not null,
        stu_age  int check(stu_age <60 and stu_age >20) not null,
        stu_sex  char(2)  check(stu_sex = '男' or stu_sex ='女') default'男'
                                                    
    )--显示 select *from stu_info
    --2【插入数据】
    --.1向所有的列插入数据:
     (identity 标识列自增的,不用插入数据,也不用null占位,否则会报错 在有标识列的表中插入数据时,即使插入数据错误,但是标识列也在自增;
     比较:在mysql中 自增列是 auto_increment 且必须是主键 primary key
    default 必须有数据 在允许为空时是null 也行,插入null 则值就是null 不会是默认值 ' ' 同理                           
    --.2向指定的列插入数据
     identity 标识列也是不能插入 总之标识列不能插入数据的 ;
    default  默认值 不向其插入值时是默认值
    --3【改数据库名】
              --语法:alter database 原数据库名 modify name= 新库名
         alter database 笔记 modify name = 同学信息表   但分离是还是原数据库名
    --4【改表增/删列】
    --a.增加新列 语法:alter table  表名 add 列名 列名的类型 约束
    alter table  stu_info add  stu_adress  varchar(10) not null default '郑州' --非空时一定要有一个默认值
    alter table  stu_info add stu_tel int
    --b.删除列 语法:alter table  表名 drop  column 列名  [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
    alter table stu_info  drop column stu_no --没有约束能够直接删除列(这里的约束不包括 null , not null)
    alter table stu_info drop column stu_adress--有约束不能直接删除,则必须先删除约束 再删除列
    alter table stu_info drop DF__stu_info__stu_ad__79A81403 -- DF__stu_info__stu_ad__79A81403 删除列时的出现的约束的地址
    -c.修改
     alter table table_name alter column_name 约束  《一般是null 在有数据时不能为not null》
    alter table stu_info alter  column stu_name char(10) not null
    --【删除一条数据】
      --语法:delete from 表名 where 条件1--这里不加where条件 会删除全部数据
     --删除‘张军’中的数据
    delete from stu_info where  stu_name='张军'--张军的所有信息都删掉了
    --------------------------------------------------------------------------------------------显示 select *from stu_info
    --删除具有 统一特性 的数据
    --删除性别是男的数据
    delete from stu_info where stu_sex='男'--男士的所有信息全都删除
    delete from stu_info where stu_no IN (3,4)--通过使用IN关键字
    --【查询数据】
     --语法1:select *from 表名                     《查询的是 (这个表中) 所有列的数据》
     --语法2:select 列名1 from 表名1 where 条件1    《查询的是表名1中符合条件1的列名1的数据》
                    --语法3:select *from 表名1 where 条件1         《查询的是表名1中符合条件1的  所有列 的数据》
    select stu_name from stu_info  --没有where条件查询的是表中所有的 stu_name 数据
    select * from stu_info where stu_adress='金水路11号'
    select stu_no AS 学号 ,stu_name  姓名,stu_age 年龄  from stu_info where stu_age <60 and stu_age > 50--通过AS ,空格 ,= ,为列取别名
    select   distinct stu_name AS 姓名 from stu_info where stu_age <60 and stu_age > 40 --通过使用关键字distinct 把不同的屏蔽掉
    select  stu_no AS 学号,stu_name 姓名,stu_age 年龄 from stu_info  where stu_no IN (4,8)--通过使用IN关键字查询 有其一就成立
    --查询结果排序 order by        desc
    select stu_name AS 姓名,stu_age 年龄 from stu_info  order by stu_age  desc--使用 order by stu_age desc按年龄从大到小排列
    select top 3 stu_name as 姓名 from stu_info order by stu_age desc         --使用top关键字  top n 或 百分比    查询前 n行 数据


    ------------------------------------另一个数据库的表中
    --查询结果分组group by
    create database chshi
    alter database chshi modify name= 超市

    create table huo
    (
      title char(20) not null,
      type char(20) not null,
      price float not null,
      priduct_id int not null
    )
    insert into huo values ('洗衣粉','日用品',4.8,2000)
    insert into huo values  ('牙刷','日用品',2.0,2500)
    insert into huo values ('apple糖','食品',5.0,1000)
     ---------------------------------------select *from huo
    select title as 名称, sum(priduct_id)as 数量 from huo group by title <order by title> --查寻每种商品的数量
       --sum函数      --可以不写

              --avg函数
    --select type as 类型,avg(priduct_id*price)as 平均价 from huo group by type order by type
    select title as 名称,type 类型 ,avg(priduct_id*price)as 平均价 from huo group by title,type order by title,type
                           --使用count函数查询每种类型中有几种产品
    select type as 类型,count(title) as 种类数 from huo group by type order by type

    select type as 类型,title 名称 , max(price)as 最高价格 from huo group by type ,title order by type ,title/**/
    select type as 类型,title 名称, sum(priduct_id*price)as 订单价格 from huo group by type ,title having sum(priduct_id*price)>7000
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    /***************************************************************************************************************************************************************************/***/
    -- 创建数据库
    create  database  stu_info --创建  创造  ddl create database 数据库名
      go
    --drop database stu_info--删除数据库  ddl create drop alter
    create database student2
    use stu_info  --以后每一步操作 都存到这个数据库中
    create table student1--创建表  在stu_info中  表名
    (
      --表中应该有相应的字段   列名
      stu_num int constraint stu_num_union unique,
      stu_name varchar(8) not null,
      stu_age int check(stu_age<100 and stu_age>0),--and 相当于逻辑与  与符
      stu_sex char(4)
    )
    create database 胡书然
    /*  unique 唯一约束
        语法:constraint 字段名后面_union  unique
        强迫给某一字段和unique约束建立关系
        unique和primary key 不允许有相同值 重复值
                            unique允许有一个是空值
        default约束:防止空值 给他一个默认值
        default ‘男’ 有效防止空值 防止无效值出现 
    在企业管理其中怎样设置默认值:打开表界面 =》 找到操作表右键
    => 设计表 =》 下方默认值中,设置相应默认值
        */
    --修改表 alter DDL database define language
    ---------------------------ddl 对象是表,库---------------------------------------
    --create 创建   drop删除    alter修改
    /*修改表的操作
       alter语法有几种
         1.add方式    增加列 
           语法:alter table 表名 add 列定义、完整性定义
    例:在stu_info中添加两个字段 班级号列  地址列 列名 */
    alter table student1 add cla_id int
     
    alter table  student1 add address varchar(10)

     /*  2、alter方式          修改列的属性
          语法:   alter teble 表名 alter  column 列名 类型    
    例:把address列宽增加到20
        alter table   student1
        alter column  address varchar(20)
    注意问题:1、不能修改列名 字段名称
          2、不能将已定义可以有空值的列定义修改成 not null
        3、若已有数据不能减少数据列,也不能修改数据类型
         4、只能修改null或者not null约束,其他类型约束放在修改前
                 必须删除,然后重新创建修改过的列的约束。*/
    -- 3.drop  删除表    约束
    alter table 表名 drop column 列名                     ----删除列
      drop database 数据库名                              ----删除数据库
      drop table 表名                                     ----删除表
      drop constraint 约束名                              ----删除约束
    -----------------dml-----对象是数据---------------
     1.insert   into 表名 values 数值  -----插入数据
     2. select *from 表名1        ----查询表1中所有列的数据
        select 列名1 from 表名1         ----查询表1中列名1的数据
     3. update 表名 set  列名=更新的值 where 条件      ----更新数据
     4.delete from 表名 where 条件  ----删除的是一条记录数据
    1.语法:insert into 表名 [列名1,列名2.... ] values 【值1,值2....】
         列名数量须和值的数相对应,如果不写列名,直接插入值时,空值应该‘
         空出(在所在的列允许为空的前提下)
    例:在student1表中插入一学生数据
        学号   007
        姓名   007
        性别   男
        年龄   20
        班级好 1
    insert into student1 values(007,'007',20,'男',1,'')
     
    注意:1.1、必须使用逗号将各个数据分开,字符型,时间类型 用单引号 '' 分隔
          1.2、每条记录必须在每个属性上都有values值,且值在自居中的 ,排列应和表中值的顺序一致。
          1.3、对于into子句,没有出现的列,保留空值位置
          1.4、在表定义时,有not null约束的属性列,将不能去空值
    --2.select 查询 查询语言 select where
    --select用于查询数据,也可以用来调用函数或者局部变量
    语法:select 选择列表(* 代表表中所有的列) from  表名 where 检索条件
      注意:2.1、选择列表中,可以包含几个列名或者表达式,用逗号隔开
                 用来指示返回那些数据,
             from子句,包含提供数据表的名称   从那里查询
            2.2、当选择列表中含有列名时,每一个select子句必须有一个from子句
        where用于写出检索条件。
    --查询全体学生所有信息
      select * from student1    
    select stu_age
    from student1
    where stu_name='胡书然'
    --3.update修改,更新
      语法:update 表名 set  列名=更新的值 where 条件 
      
      例:update用于修改表中已经存在的数据
          可以一次修改多行,也可以修改一行
      例:修改一行:把007 的 姓名换成  胡书然
      update student1
      set stu_name='胡书然'
      where stu_age=20
      例:将所有学生年龄全部加1
      update student1
      set stu_age=stu_age+1
      where stu_age<22
      insert into student1 values(321,'0012',20,'男',1,'')
    --5.delete 删除
      where 要删除记录的条件
      可以删除一行 也可以删除所有数据
      没有where就是删除所有数据 
    delete from student1
    go
    drop table student1
    select * from student1
    delete from student1
    where stu_name='胡书然'


    今天:语言  ddl  create drop alter
                dml  update  delete  insert  select
                dcl  grant revoke

    时间函数:
    select getdate()
    select year('2000-10-10')
    select year(getdate())
    select datediff(hour,'1988-10-01',getdate())

  • 相关阅读:
    大数据记录2
    大数据记录1
    Spark 1.5 to 2.1.X
    存储过程定期删除冗余数据
    spark error Caused by: java.io.NotSerializableException: org.apache.hadoop.hdfs.DistributedFileSystem
    spark sql error mismatched input 'union' expecting { <EOF>,''................................
    spark rdd元素println
    spark模型运行时无法连接摸个excutors异常org.apache.spark.shuffle.FetchFailedException: Failed to connect to xxxx/xx.xx.xx.xx:xxxx
    spark模型error java.lang.IllegalArgumentException: Row length is 0
    被简书恶心到了
  • 原文地址:https://www.cnblogs.com/top100/p/2092736.html
Copyright © 2020-2023  润新知