zoukankan      html  css  js  c++  java
  • sql server 笔记(数据类型/新建、修改、删除数据表/)

    1、数据类型:

    Character 字符串 / Unicode 字符串 / Binary 类型 / Number 类型  /  Date 类型  / 其他数据类型

    详解:http://www.w3school.com.cn/sql/sql_datatypes.asp

    2、新建数据表

    (1)若要创建表,您必须提供该表的名称以及该表中每个列的名称和数据类型。指出每个列中是否运行空值,也是一种很好的做好。

    (2)大多数表有一个主键,主键由表的一列或者多列组成。主键始终是唯一的。数据库引擎将强制实施以下限制:表中的任何主键值都不能重复。

    过程:

    (1)在“对象资源管理器”中,连接到SQL Server数据库引擎的实例,然后展开该实例;

    (2)右键单击“表”,然后单击“新建”;

    (3)在弹出“新建表”的窗体中,录入表结构;

    (4)点击保存,在弹出对话框中输入表名称。

    备注:

    设置主键:点击列,右键选择设置主键。

     ==》        ==》 

    3. 修改表结构

     选中数据表==>右键,选择设计

     

    4.定义表主键/外键

    主键(primary key):

    是表中的一个或多个字段,他的值用于唯一的标识表中的某一条记录。一个表只有一个主关键字,主关键字又可以称为主键。主键可以由一个字段,也可以由多个字段组成,分别称为单字段主键或多字段主键,又称为主码,并且他可以唯一确定表中的一行数据,或者可以唯一确定一个实体。

    外键:

    (1)表示了两个关系之间的相关联系,以另一个关系的外键作主关键字的表被称为主表,具有此外键的表被称为主表的从表。外键又称作外关键字。

    (2)保持数据一致性,完整性,主要目的是控制存储在外键表中的数据。

    操作:选中列,右键==>关系

    备注:一般不设置外键,删除数据时不方便;

    5. 新增表记录

    (1)插入单行数据

    insert into '表格名' ('栏位1','栏位1',...) values ('值1','值2',...)
    insert into userInfo (id,name,age) values(2,'王五',13)

    (2)插入多行数据

    insert into "表格名" ("栏位1","栏位1",...) values ("值1","值2",...),("值1","值2",...);  

    (3)从其他表copy数据

    insert into "表格名1" ("栏位1","栏位1",...) select "栏位3","栏位4",... from "表格2"

    6.查询表记录  

    select select_list from table_source
    distinct //去重
    select distinct select_list from table_source
    top //查询 前n行
    select top 行数 select_list from table_source
    

    eg.

    select * from Vendor
    select top 20 code from Warehouse
    select distinct code from Warehouse
    

    7.修改表

    update table_name set 字段 = 值   

    eg.

    updata student set name="张三",age=12  where id=1 and phone = 133
    

    备注:图形界面也可以修改

    8.删除表

    delete from table_name
    

    eg.

    // from 可省略
    delete from userInfo where id =1
    

    9.条件限制 where

    //精确限制条件
    where 字段 = 值
    //模糊限制条件
    where 字段 like '%值%'

    eg.
    select * from userInfo where id = 2 and  age = 13
    select * from userInfo where role like '%工程师'  

    10. between 语法

    用法限制条件表达式,指定表达式范围值

    test_expression [NOT] BETWEEN begin_expression AND end_expression
    eg.
    select * from userInfo where (id between 2 and 5) and (phone = '133')

    11.IN 语法

    用于限制条件表达式,指定表达式范围值

    test_expression [NOT] IN (subquery | expression [,...n])
    eg.
    select * from Students where StudentNo in (1501,1503)
    select * from Students where StudentNo in (select StudentNo from Student_Lesson)

    12.子查询 EXISTS

    select a.No ,a.Name ,a.Age from Students as a where exists (select ID from Student_Lesson b where a.No = b.No)
    

    EXISTS 用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True 或 False

    EXISTS 指定一个子查询,检查行的存在。  

    13.返回记录排序

    默认升序,优先级:前面的优先

    ORDER BY order_by_expression [ASC | DESC] [,...n]
    eg.
    select * from Students order by ID ,NAME desc
    

    14.

    15.

  • 相关阅读:
    linux下压力测试工具ab的使用
    linux下nginx日常操作
    centos7编译安装nginx
    ssh登录locale报错:cannot change locale (zh_CN.UTF-8): No such file or directory
    linux重装rabbitmq的问题
    rabbitmq3.6.5镜像集群搭建以及haproxy负载均衡
    linux安装rabbitmq3.6.5
    centos6升级glibc-2.14没有报错,但是验证没有升级成功的问题解决
    linux卸载erlang
    asp web服务
  • 原文地址:https://www.cnblogs.com/kongge/p/9351482.html
Copyright © 2011-2022 走看看