创建模式
 create schema <schema_name> authorization <username> 没有指定schema_name时默认是用户名

 删除模式
 drop schema <schema_name> <cascade | restrict>

 创建表
 create table student.sc            ;定义表中的两个主码以及外码
 (sno ),
 cno ),
 grade smallint,
 primary key(sno, cno),
 foreign key sno references student(sno),
 foreign key sno references course(cno)
 );                    /*最后一行没有逗号,外码引用的必须是主码*/

 修改表
 alter table <table_name>
 [ add <新列名> <数据类型> [完整性约束] ]
 [ drop [完整性约束名] [列名] ]
 [ modify <列名> <数据类型> ];

 删除表

 drop table <表名>;

 创建索引
 create [unique] [cluster] index <索引名>
 on <表名>( <列名> [ <次序> ] [, <列名> [ <次序> ] ] ....);

 删除索引
 drop index <索引名>

 插入元组
 insert
 into <表名> [ ( <属性列1> [, <属性列2>...] ) ]
 values ( <常量1> [, <常量2>] ... )

 insert into sc(sno,cno)
 ')

 修改元组
 update <表名>
 set <列名> = <表达式> [, <列名> = <表达式> ]...
 [where <条件>];

 update student

 '

 删除元组
 delete
 from <表名>
 [where <条件> ];

 delete
 from student
 '

 使用视图创建语句建视图,通过视图查询数据:
 create view <视图名> [(<列名>[,<列名>]...)]            ;列名要么全部指定,要么全部不指定
 as
 <子查询>
 [with check option];

 drop view <视图名>;

 创建用户
 create user <username> [with] [DBA | RESOURCE | CONNECT];
 create user  zx_root   IDENTIFIED by 'xxxxx@localhost';

 删除用户
 drop user <username>;

 授权
 grant <权限> [,<权限> ]...                    ;all privileges, select, update, insert, delete
 on <对象类型> <对象名> [,<对象类型> <对象名>]...
 to <用户> [,<用户>]...                        ;public
 [with grant option];

 grant all privileges
 on table student, course
 to u2, u3;

 grant update(sno)
 on table student
 to u4;

 grant insert
 on table sc
 to u5
 with grant option

 回收授权
 revoke <权限>[,<权限>]...
 on <对象类型> <对象名> [,<对象类型> <对象名>]...
 from <用户> [,<用户>]...

 revoke select
 on table sc
 from public

 创建角色
 create role <rolename>

 给角色授权
 grant <权限> [,<权限>]...
 on <对象类型> <对象名>
 to <角色> [,<角色>]...

 grant <角色1> [,<角色2>]...
 to <角色3> [,<角色4>]...
 [with admin option]

 收回角色权限
 revoke <权限> [,<权限>]...
 on <对象类型> <对象名>
 from <角色> [,<角色>]...

 create role r1;

 grant select, update, insert
 on table student
 to r1;

 grant r1
 to 王平,张明

 revoke select
 on table student
 from r1;

 审计

 audit alert,update
 on sc;

 noaudit all
 on sc;

 实体完整性
 primary key(sno,cno);

 参照完整性
 foreign key sno reference student(sno);

 用户定义完整性
 create table sc
 (sno ) not null,
 cno ) not null,
 grade smallint not null,);

 create table dept
 (deptno number,
 dname ) unique,
 );

 create table student
 (sno ) primary key,
 ssex ) check (ssex in ('男','女')),);

 表级用户定义完整性
 check (ssex = '女' or sname not like 'ms.%' );            /*男性名字不能以ms.开头*/

 完整性约束命名子句
 constraint <完整性约束条件名> [primary key短语 | foreign key 短语 | check 短语]

 create stable student
 (sno )
      ),
 sname )
     constraint c2 not null,
 sage )
     ),
 ssex )
     constraint c4 check (ssex in ('男','女'),
 constraint studentKey primary key(sno),
 );

 alter table student
 drop constraint c4;

 alter table student
 add constraint c4 check (ssex in ('男','女'));

 域中完整性限制
 )
 constraint gd check (value in ('男','女'));

 alter domain genderdomain
 '));

 alter domain genderdomain
 drop constraint gd;

 查询
 seletct [ all | distinct ] <目标列表达式> [, <目标列表达式> ]...
 from <表名或视图名> [, <表名或视图名> ]...
 [ where <条件表达式> ]
 [ group by <列名1> [ having <条件表达式> ] ]
 [ order by <列名2> [ asc | desc ] ]

 表单查询

 -sage year
 from student

 select sname, sdept, sage
 from student
          /*not between and*/

 select sname, ssex
 from student
 where sdept in ('cs','ma','is')     /*not in*/

 select *
 from student
 where sno like '2002%21'            /*%任意多个字符,_单个字符, [ escape '\' ] 表示'\'为换码字符,not like */

 select sno,cno
 from sc
 where grade is null            /*is not*/

 select sno,grade
 from sc
 '
 order by grade desc,sno

 select cno, count( distinct sno )
 from sc
 group by cno

 连接查询,嵌套查询

 select sname
 from student
             /*or, not*/

 select first.cno, second.cpno
 from course first, course second
 where first.cpno = second.cno                /*<>是不等于*/

 select sname
 from student
 where sno in
     (select sno
     from sc
     ' );

 select sno,cno
 from sc x
 where grade >=
     (select avg(grade)
     from sc y
     where y.sno = x.sno);

 select sname,sage
 from student
 where sage < any
     (select sage
     from student
     where sdept = 'cs');            /*all*/

 select sname
 from student
 where not exists
     (select *
     from course
     where not exists
         (select *
         from sc
         where sno = student.sno
         and    cno = course.cno ));            /*not exists 没有*/

 集合查询:

 select *
 from student
 where sdept='cs'
 union
 select *
 from student
 ;

 select *
 from student
 where sdept='cs'
 intersect
 select *
 from student
 ;

 select *
 from student
 where sdept='cs'
 except
 select *
 from student
 ;

 数据类型
 char(n)                长度为n的定长字符串
 varchar(n)                最大长度为n的可变字符串
 int                    长整形,可写作integer
 smallint                短整形
 numberic(p,d)            定点数,有p位数字(不包括符号,小数点)组成,小数点后有d位小数
 real                    取决于机器精度的浮点数
 double precision            取决于机器精度的双精度浮点数
 float(n)                浮点数,精度至少为n为数字
 date                    日期,YYYY-MM-DD
 time                    时间,HH:MM:SS

 小问题

 "=" 两边可以没有空格

 实例:

 create DataBase SpjDB
 on (name=spj_dat,
 filename='D:\Sql_Server\spj_data.mdf',
 size=10mb)
 log
 on (name=spj_log,
 filename='D:\Sql_Server\spj_log.ldf',
 size=3mb)

 Create table S
 (SNO ) primary key,
 SNAME ),
 STATUS smallint,
 CITY ))

 insert into s
 ','天津');

 insert into s
 ','北京');

 Create table p
 (PNO ) primary key,
 PNAME ),
 COLOR ),
 WEIGHT smallint)

 insert into P
 ');
 insert into P
 ');
 insert into P

 Create table J
 (JNO ) primary key,
 PNAME ),
 CITY ))

 insert into J
 values('J1','三建','北京');
 insert into J
 values('J2','一汽','长春');
 insert into J

 Create table SPJ
 (SNO ),
 PNO ),
 JNO ),
 QTY smallint)

 insert into SPJ
 ');
 insert into SPJ
 ');
  

SQL 基础的更多相关文章

  1. <三> SQL 基础

    SQL查询的一般形式,以及被逻辑处理的顺序 (8) select (9) distinct (11) <TOP_specification> <select_list> (1) ...

  2. [SQL] SQL 基础知识梳理(三) - 聚合和排序

    SQL 基础知识梳理(三) - 聚合和排序 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5926689.html 序 这是<SQL 基础知识梳理 ...

  3. Oracle知识梳理(三)操作篇:SQL基础操作汇总

    Oracle知识梳理(三)操作篇:SQL基础操作汇总 一.表操作 1.表的创建(CREATE TABLE): 基本语句格式:       CREATE TABLE  table_name ( col_ ...

  4. ASP.NET实现二维码 ASP.Net上传文件 SQL基础语法 C# 动态创建数据库三(MySQL) Net Core 实现谷歌翻译ApI 免费版 C#发布和调试WebService ajax调用WebService实现数据库操作 C# 实体类转json数据过滤掉字段为null的字段

    ASP.NET实现二维码 using System;using System.Collections.Generic;using System.Drawing;using System.Linq;us ...

  5. 数据库整理(三) SQL基础

    数据库整理(三) SQL基础 SQL语言的特点 集数据定义语言(DDL),数据操纵语言(DML),数据控制语言(DCL)功能于一体. 可以独立完成数据库生命周期中的全部活动: ​ ●定义和修改.删除关 ...

  6. 第三章 - SQL基础及元数据获取

    SQL的介绍 SQL的定义:结构化查询语句 SQL的作用:对库和表进行操作 SQL的常用分类 DDL 数据定义语言(Data Definition Language) DCL 数据控制语言(Data ...

  7. [SQL] SQL 基础知识梳理(一)- 数据库与 SQL

    SQL 基础知识梳理(一)- 数据库与 SQL [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5902856.html 目录 What's 数据库 ...

  8. [SQL] SQL 基础知识梳理(二) - 查询基础

    SQL 基础知识梳理(二) - 查询基础 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5904824.html 序 这是<SQL 基础知识梳理( ...

  9. [SQL] SQL 基础知识梳理(四) - 数据更新

    SQL 基础知识梳理(四) - 数据更新 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5929786.html 序 这是<SQL 基础知识梳理( ...

  10. [SQL] SQL 基础知识梳理(五) - 复杂查询

    SQL 基础知识梳理(五) - 复杂查询 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5939796.html 序 这是<SQL 基础知识梳理( ...

随机推荐

  1. 以.net core重构原有.net framework过程中的一些API变更记录(持续更新)

    1)Type.IsGenericType类似属性变更 以下是.net framework 4.5中Type抽象类中泛型类型的几个个属性,用于泛型类型的相关信息判断: 以下是.net core(nets ...

  2. java版简易socket客户端

    android项目需要使用到心跳, 于是编写了一个简易的socket客户端程序 主要功能是给服务端发送心跳包,保持在线状态 没有使用框架,这样避免了需要引入包,直接使用的阻塞Socket通信. 主要逻 ...

  3. css-列表或标题的多级计数

    利用css实现多级计数,比如1/1.1/1.1.1这种层层嵌套的计数,主要利用到counter-reset/counter-increment/counter/content/:before. 一.标 ...

  4. 【poj2096】 Collecting Bugs

    http://poj.org/problem?id=2096 (题目链接) 题意 有一个程序,其中有s个子结构,每个子结构出bug的概率相等.bug总共分成n类,每种bug出现的概率相等.每天找出一个 ...

  5. java String类

    String 是一个类 所以他不是基本数据类型 而是引用数据类型 String 两种实例化方式:1.直接赋值 String name = "xxxxxx";2. String na ...

  6. windows下python的web环境搭建使用(观看Backbone的教程有感)

    pip安装a 下载 get-pip.py (https://pip.pypa.io/en/latest/installing/#python-os-support b python get-pip.p ...

  7. Unity Animator动画状态机 深入理解(一)

    接触Unity以来就已经有了Animator,Animation用的少,不过也大概理解他俩之间的一个区别于联系. 图中其实就是Animator和Animation之间的区别于联系了,啊!你肯定会告诉我 ...

  8. centos7下使用yum安装mysql

    CentOS7的yum源中默认好像是没有mysql的.为了解决这个问题,我们要先下载mysql的repo源. 1. 下载mysql的repo源 wget http://repo.mysql.com/m ...

  9. 数字与字母混合生成流水号规则--ASP实现

    最近遇到一个比较奇葩的事情,自己所负责的一个系统出现一个流水号用完的问题:正常情况下,流水号用完应该增加多位来解决这个问题.鉴于各种因素,最后决定:位数不变,增加字母进去,当数字用完后,会出现字母,而 ...

  10. 调用Interop.zkemkeeper.dll无法使用解决方案

    调用Interop.zkemkeeper.dll无法使用 已经注册dll成功但是还是报错 检索 COM 类工厂中 CLSID 为 {00853A19-BD51-419B--2DABE57EB61F} ...