数据库基本知识

来源:互联网 发布:非诚勿扰遗憾退场软件 编辑:程序博客网 时间:2024/04/29 22:24

一、数据库语言

1、数据定义语言(DDL):定义数据库模式;

create table Students(    student_id    char(10) not null,    student_name  char(20),    student_sex   char(5),    student_class char(10)primary key(student_id),foreign key(student_class)references Class  )

2、数据操纵语言(DML):数据的的查询和更新;

select student_idfrom Studentwhere student_sex = 'woman'

二、数据类型
常见的数据类型有:
1、char(n),固定长度字符串,用户指定长度n。
2、varchar(n),可变长度字符串,用户指定最大长度n。
3、int/smallint,整数类型和小整数类型。
4、numeric(p,d),定点数。
5、real,double,precision,浮点数和双精度浮点数。
6、float(n),精度至少为n位的浮点数。
7、date ‘2016 -03 -09’
8、time ‘10:27:00’
9、timestamp ‘2016 -03 -09 10:27:00’

三、SQL常见命令

1、插入

insert into Student (student_id,student_name,student_sex,student_class)values('2016211333','张三','woman','2016211320');

2、删除
删除Student中所有元组,但是保留关系Student

delete from Student

删除关系Student

drop table Student

3、更新 update

update instructorset salary = salary * 1.5where salary < 7000

4、操作关系的属性
增加属性

alert table Student add student_register;alert table Student add student_register Beijing;

删除属性

alert table Student drop student_register;

5、更名运算 as

select S.student_namefrom Student as S , Class=Cwhere S.sex = 'woman';

6、去重 distinct

select distinct student_namefrom Studentwhere Student.student_sex='women';

7、排列元组显示次序 asc升序,desc降序

select *from Studentorder by Student.student_id asc;

8、between比较运算符

select *from instructorwhere salary between 9000 and 10000;

等价于

select *from instructorwhere salary >= 9000 and salary <= 10000;

9、集合运算 union/intersect/except 并、交、差

10、空值 is null is not null

11、聚集函数
平均值:avg
最小值:min
最大值:max
总和:sum
计数:count

12、分组聚集 group by
任何没有出现在group by 字句当中的属性如果出现在select字句中,则该属性只能出现在聚集函数的内部否则为错误的。
一个错误的例子:

select dept_name,ID,avg(salary)from instructorgroup by dept_name

13、having字句
类似where字句,但是对group by才起作用

14、重复元组存在性测试 unique

15、嵌套子查询

with字句

with max_budget(value) as(    select max(budget)    from department)select budgetfrom department,max_budgetwhere department.budget = max_budget.value;

from语句中的子查询

select dept_name,avg_salaryfrom (    select dept_name,avg(salary) as avg_salary    from instructor    group by dept_name    )where avg_salary>4200

标量子查询(可出现在返回单个值的表达式能够出现的任何地方)

select dept_name,    (    select count(*)    from instructor    where department.dept_name = instructor.dept_name    ) as num_instructorfrom department

16、创建索引

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 索尼z5无限重启怎么办 苹果8基带坏了怎么办 oppo手机忘记图案密码怎么办 电池充不进去电怎么办 电脑充不进去电怎么办 苹果5c白苹果怎么办 港行不支持电信卡怎么办 安卓导航不开机怎么办 鞭炮放一半不响怎么办 禁止鸣笛的地方鸣笛了怎么办 手被炮仗炸了怎么办 手被猴子抓伤了怎么办 炸东西剩的油怎么办 炸臭豆腐剩的油怎么办 油炸久了油发黑怎么办 炸鱼的时候粘锅怎么办 吃了葱蒜有味怎么办 哺乳期喝了抹茶怎么办 干炸小黄鱼凉了怎么办 烧鱼酱油放多了怎么办 夏天腿干燥起皮怎么办 螃蟹柿子同时吃了怎么办 柿子和螃蟹一起吃怎么办 螃蟹和柿子吃了怎么办 今天为什么很多网站打不开怎么办 网上报名人太多服务器卡怎么办 网站换了电脑打不开怎么办 感冒时后背发凉怎么办? 脚扭了脚背疼怎么办 五妙水仙膏干了怎么办 红苹果接不到任务了怎么办 我判刑了家里老母亲怎么办 离婚之前对方把财产转移怎么办 有人侵犯我的名誉权怎么办 耳朵被咬红肿了怎么办 孕29周呼吸困难怎么办 痔疮出血一个星期了怎么办 怀孕长了副乳该怎么办 备孕期间长痔疮怎么办 15年凌度智能钥匙全丢怎么办 西水开发商跑了怎么办