SQL Server—表格详解

SQL Server---表格详解

    表格的创建

    复制代码
    ##### 代码操作
    
    *
    
      ```sql
      -- StudentTwo 库名
      use StudentTwo
      go
    
      -- table 表-- database 数据库 存放表-- 先判断表是否存在,如果存在先删除再创建
      -- sysobjects 表-- 判断所有系统表有没有一个表名字为number,如果有删除掉
      if exists(select * from sysobjects where name = 'nunber')
      drop table number --删除掉
      go
    
      create table XueShengTable(
      	-- 学生ID 整形谁 identity 标识符作用从1000开始 每次递增1 ,以后添加学生信息时候不要标识列添加
      	StudentId int identity(1000,1),
    
      	-- 姓名
      	StudentName varchar(20) not null,
    
      	-- 性别
      	Gender char(2) not null,
      )
      注意: 列后面不加 not null,加null是当前列可为空,加not null是当前列不可为空
      ```
    复制代码
    ##### 界面操作
    
    *
      > 点击数据库 --\> 点开使用的数据看 --\> 右键击表 --\> 新建表

    增删改查

    复制代码
    ##### 增
    
    * 解释:
    
    *
      > -- 插入、增加学生
      >
      > -- insert into 表名(列名) values(值)
      >
      > -- 向哪个表中插入那一列对应值是什么
      >
      > -- 注意: 列名之间使用逗号隔开,值和列名一一对应,类型也得匹配
    * 写法1:
    
    *
      >
      > ```sql
      > insert into XueShengTable(StudentName,Gender,Binrthday,Age,Phone,StudentAddress,ClassId,StudentCard) values('迪迦','男','2000-09-18',14,'15377300008','河南省南阳市邓州市',2341,12345678910987)
      > ```
    
    * 写法2:
    
    *
      > -- 也可以将列名省略
      >
      > ```sql
      > insert into XueShengTable values('凹凸曼','男','2000-09-18',12345678910987,23,'15377300008','河南省南阳市邓州市',1)
      > ```
    复制代码
    ##### 删
    
    * -- 语法
    
    *
      > delete from 表名 where 条件
    * 方法1delete:
    
    *
      > -- 把学号1001数据删除
      >
      > delete from XueShengTable where StudentId = 1001
    * -- 第二种删除方案 truncate:
    
    *
      > truncate table XueShengTable -- 删除整个表格
    * -- 小提示: delete删除的时候比truncate删除的更快 -- delete删除的时候 不允许这条记录被外键引用,可以先把外键的关系删除掉,再进行删除这条数据 -- truncate 要求删除的表不能有外键的约束
    复制代码
    ##### 改
    
    * -- 语法:
    
    *
      > updaste 表名 set 列名 = 值, 列名 = 值 where 条件
    * --修改学号为1000学生的姓名改为李白,出生年月
    
    *
      > update XueShengTable set StudentName = '李白',Binrthday='1975-01-01' where StudentId=1000
    复制代码
    ##### 查
    
    * -- 查询所有信息 类似于数组遍历
    
    *
      > select \* from XueShengTable
    * -- 查询具体列的数据查询姓名这一列 select 列名, from 表名 select StudentName,Gender from XueShengTable
    
    * -- 条件查询 : 查询年龄等于23的 select 列名, 列名 from 表名 where Age = 23
    
    *
      > select StudentName from XueShengTable where Age = 23
    * -- 查询满足条件的所有列
    
    *
      > select \* from XueShengTable where Age \<= 23
    * --逻辑运算符号 and 并且关系 相当于\&\&,or或者条件 相当于\|\|;
    
    *
      > select \* from XueShengTable where Age\>14 and Age\<23
相关推荐
web3.08889993 分钟前
tb关键词API接口——解锁独一无二的商品
java·数据库·https
黄昏晓x6 分钟前
数据库 ---- 表的约束
android·数据库
Elastic 中国社区官方博客12 分钟前
使用 Elastic Observability 和 MCP 的 Agentic 驱动 Kubernetes 调查
数据库·elasticsearch·搜索引擎·云原生·容器·kubernetes·全文检索
阿正的梦工坊14 分钟前
DOCKER_DATABASE_URL 逐段解析:部署时候的信息解析
数据库·docker·容器
倒流时光三十年17 分钟前
PostgreSQL 大表字段扩长度 -- 会不会锁表?
数据库·postgresql
Irene199119 分钟前
(AI总结版)完整操作流程:从零配置 Oracle 21c XE 开发环境(安装 CO 示例、安装 SCOTT 教学示例)
数据库·oracle
Han.miracle22 分钟前
Spring Cloud + Nacos 环境切换与配置管理最佳实践
数据库·spring boot·spring cloud·maven
p@nd@24 分钟前
DM删除用户后的不完全恢复测试
数据库·达梦数据库·备份还原·备份恢复
在屏幕前出油26 分钟前
08. ORM——快速开始
数据库·后端·python·sql·pycharm·orm
lzhdim28 分钟前
SQL 入门 11:日期时间格式化、IF、CASE的使用
数据库·sql