sql 基本语言要点

1.创建表

  • 属性在前,数据类型在后 例如:sname char(20),

  • 定义计算列 列名 as 表达式

  • 主码有多个属性构成是,必须作为表级完整性进行定义 例如:
    primary key(sno ,cno),
    foreign key (sno)references student (sno),

  • 定义有名称的约束方便后期修改
    int cno constraint cno_1 default 0

2.修改表

  • alter table 表名{
    alter column 列名
    add
    drop 删除列或者删除约束
    drop constrain 约束名 [cascade联级删除 | restrict 限制, 有关联的将不删除] }
    例如:
    alter table student
    alter column sname char(50) not null
    3.删除表
    drop table

操作表数据

1.插入

  • insert into 表名(列名)values() char的值用单引号
  • insert into 表名 select * 语句
  • 例如:insert into student values(1,1,‘陈伟斌’,‘you’,‘2015-09-14 23:59:59’);

2.修改update

  • update 表名 set 某列的数据的修改 where 某个元组的条件

3.删除记录

  • 删除符合条件的记录
    delete from 表名 where
  • 删除表中所有记录
    truncate table 表名

数据查询select

1.查询经过计算的值
select 2014-sage birthday from student where …
2.消除重复行(即投影之后的重复值)
select distinct sno from student (默认为all 不消除重复行)
3.where满足条件的元组

  • 确定范围 :between and ;not between and
  • 确定集合:属性指在指定集合中 sdept in (‘cs’,‘MA’)括号内也可以为select语句
  • 字符匹配:like (%任意字符长度,_单个字符,escape 定义转义符)例如where cname like ‘db \ _des_nnn’ escape ’ \ ’
  • 涉及空值的查询 where info is not null

4.oder by asc升序 desc 降序默认为升序
5.聚集函数(只能用在select后和group by 之中的having)

  • count(对元组进行计数值为null不影响结果),sum,avg,max,min([distinct | all ] 列名)
  • where筛选from指定的数据对象;group by用于对where结果进行分组,having对分组后的数据进行筛选
    例如:选修课程成绩超过80分且至少有两门的学生学号
    select sno
    from sc
    where score>80
    group by sno
    having count(*)>2

数据查询:连接查询

1.等值以及非等值连接 and
2.join

  • 内连接
发布了8 篇原创文章 · 获赞 2 · 访问量 489

猜你喜欢

转载自blog.csdn.net/chen2016/article/details/104039053
今日推荐