zoukankan      html  css  js  c++  java
  • 数据库面试

    常用指令

    (1) 数据记录筛选:
    sql="select * from 数据表 where 字段名=字段值 order by 字段名 [desc]"
    sql="select * from 数据表 where 字段名 like '%字段值%' order by 字段名 [desc]"
    sql="select top 10 * from 数据表 where 字段名 order by 字段名 [desc]"
    sql="select * from 数据表 where 字段名 in ('值1','值2','值3')"
    sql="select * from 数据表 where 字段名 between 值1 and 值2"
    
    (2) 更新数据记录:
    sql="update 数据表 set 字段名=字段值 where 条件表达式"
    sql="update 数据表 set 字段1=值1,字段2=值2 …… 字段n=值n where 条件表达式"
    
    (3) 删除数据记录:
    sql="delete from 数据表 where 条件表达式"
    sql="delete from 数据表" (将数据表所有记录删除)
    
    (4) 添加数据记录:
    sql="insert into 数据表 (字段1,字段2,字段3 …) values (值1,值2,值3 …)"
    sql="insert into 目标数据表 select * from 源数据表" (把源数据表的记录添加到目标数据表)
    
    (5) 数据记录统计函数:
    AVG(字段名) 得出一个表格栏平均值
    COUNT(*|字段名) 对数据行数的统计或对某一栏有值的数据行数统计
    MAX(字段名) 取得一个表格栏最大的值
    MIN(字段名) 取得一个表格栏最小的值
    SUM(字段名) 把数据栏的值相加
    

    创建数据库:

       命令:create database 数据库名;

      示例:create database student;

    删除数据库:

      命令:drop database 数据库名;

      示例:drop database  student;

    新建表格:

      命令:create table 表名

                  (列名  数据类型,列名2.....)

      示例:create table student

                 (sname  char(20),sid  int)

    删除表格:

      命令:drop table 表名

      示例:drop table student

    修改表结构:

        (插入(新增)列)

        命令:alter table 表名

                     add 新列名  数据类型

        示例:alter table student

                     add  sage  int

        (删除列)

        命令:alter table 表名

                       drop column 列名

        示例:alter table student

                       drop column sid

        (修改列类型)

        命令:alter table 表名

                      alter  column 列名  数据类型

        示例:alter table student

                      alter  column  sid  float(浮点型)

      (新增约束)

         命令:alter table 表名

                      alter column 列名  新数据类型

         示例:alter table student

                       alter column PK_sid  primary  key(sid)(新增的约束类型是主键约束)

      (删除约束)

        命令:alter table 表名

                      drop  列名

        示例:alter table student

                      drop PK_sid

    查询表内容:

      命令:select  要查询的数据列名

                  from 表名

                    where  筛选条件(无法对分组后的数据进行筛选)

    (高级搜索)【group  by 列名(分组)

                             having  筛选条件(只能对分组后的数据进行筛选)

                                order by  排序方式(控制数据最后输出的排列方式有正序:asc、倒叙:desc)】

      示例:select  sid

                 from student

                   where  sid=2

                        【group by sid

                                  having  sid=1

                                       order by desc】

    在表中插入数据:(值与列必须一一对应)

     命令:insert  into  表名

                    (列名 ,列名)

                values

                    (值,值)

      示例:insert  into  表名

                    (sname,sid,sage)

                values

                   (‘张三’,12,15)

    修改表中数据值:

      命令:update from 表名

                   set 列名=新值

      示例:update from student

                  set sname='李四'

    超键、候选键、主键、外键分别是什么?

    超键:在关系中能唯一标识元组的属性集称为关系模式的超键。一个属性可以为作为一个超键,多个属性组合在一起也可以作为一个超键。超键包含候选键和主键。

    候选键:是最小超键,即没有冗余元素的超键。

    主键:数据库表中对储存数据对象予以唯一和完整标识的数据列或属性的组合。一个数据列只能有一个主键,且主键的取值不能缺失,即不能为空值(Null)。

    外键:在一个表中存在的另一个表的主键称此表的外键。

     
  • 相关阅读:
    Spring Aware源码
    Spring 后置处理器源码
    Java8 Optional
    几种自定义Spring生命周期的初始化和销毁方法
    Spring通过@Autowired获取组件
    Spring的组件扫描注解
    Spring通过注解注入外部配置文件
    [CSP-S模拟测试92]题解
    [笔记乱写]关于数论函数(关于卷积的一些证明+杜教筛)
    我觉得我就是[数据删除]
  • 原文地址:https://www.cnblogs.com/Aaron12/p/9201180.html
Copyright © 2011-2022 走看看