select和insert用法

本文介绍了SQL中的基本查询操作,包括使用`SELECT`查询特定列或所有信息,结合`WHERE`子句设置条件,处理空值,以及使用`INTO`创建新表。还提到了`IN`和`NOT IN`操作符以及`OR`和`AND`逻辑运算符的用法。此外,`INSERT`语句用于向表中插入新记录,支持指定字段插入和全字段插入。文章还涉及了特殊约束和更改表内容的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                                                        select 和 insert用法
1、select语法:select column1,column2 from table_name where condition1 or/and condition2

  • select    *    from    Information                                           --查询Information表中的信息    *   代表所有列
  • select    *    from    Information    where mem_major = '物联网工程'         --where 后面表示条件
  • select    *    from    Information    where mem_major = '物联网工程' or mem_major = '电子信息工程'   --条件的或,只满足一个就能查询出来,还有与 and ,同时满足才能查询到,   
  • select    *    from     Information    where isnull(mem_major,'') = ''       --表示mem_major这一类是否是空字符串,如果是,则视为null,查询的话可以当成null查询出来
  • select    mem_major,mem_dir,mem_name    from Information                     --查询一些列在Information表中
  • select    mem_major,mem_dir,mem_name    decration into Sim_Info    from Information    --将查询到的这些列放到新的表Sim_Info中
  • select    mem_major,mem_dir,mem_name,cast('' as varchar(200) )    decration into Sim_Info    from Information    --cast('' as varchar(200)) 将''空字符串作为varchar(200)的说明字符
  • select    object_id('Information')                              --查询表达id号
  • select * from syscolumns where id =  object_id('Information')   --查询id的表
  • select * from Information where mem_grade = 1602 or mem_grade = 1601
  • select * from Information where mem_grade in (1601,1602)
  • select * from Information where mem_grade not in (1601,1602)
  • select    *    from    sysobjects    where xtype = 'PK'         --查询数据库中哪些表   
    U 是查询表
    P 是查询存储过程
    V 是查询视图
    T 是查询触发器
    F 是查询函数
    PK 是查询主键
    FK 是查询外键
  • select 还有更改的用途
declare @i int
select  @i = 22
insert Information select '小明',@i,'男'    --直接插入所有列,可用变量
2、insert语法: insert     table_name(column1,column2) values(value1,value2)
  • insert    Information(mem_name,mem_age,mem_sex) values('小明',22,'男')
  • insert    Information    values('...','...')    --写入全部列的信息
  • insert    Information    select '小明',22,'男'    --直接插入所有列


3、其他一些知识
  • 添加特殊约束
alter    table    Information
add     mem_phone    nvarchar(11)    not null default('')    --添加mem_phone 列,类型nvarchar , 非空,默认值''
constraint    UQ_Phone    unique(mem_phone)                        --特殊约束,不能重复出现
  • 更改一个表中的内容
alter    table    Information
update    Information    set    mem_major = '物联网工程' where    mem_major = '物联网'        --更改表中某一列的值,将'物联网'改为'物联网工程',set为设置


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值