I don't make software; I make it better

sql基础复习

上一篇 / 下一篇  2008-12-17 17:35:39 / 个人分类:数据库相关

基本SQL语句
1.select prod_id,prod_name,prod_price form products order by prod_name,prod_price;
                   by 2,3;
                   by prod_price desc;降序
2.select prod_id,prod_name,prod_price form products oder by 3 desc,2;
3.select prod_id,prod_price form products where prod_price=3.3;
                  >0;
4.select wend_id,prod_name from products where wend_id<>'DLL01';等价于!=
5.select prod_name,prod_price form products where prod_price between 5 and 100;
6.select prod_name form products where prod_price is null;
7.select prod_id,prod_name,prod_price from products where vend_id="DLL01" and prod_price<=4;
8.select prod_id,prod_name,prod_price form products where vend="DLL01" or vend_id='DLL02'
9.select prod_id,prod_name,prod_price form products where vend_id='DLL01' or vend_id='DLL02' and prod_pric>=10;供应商DLL01所有的产品,DLL02价格为10美元以上的产品
              (vend_id='DLL01' or vend_id='DLL02' )and prod_pric>=10;供应商DLL01,02所有价格为10美元以上的产品 AND优先级高于or
10.select prod_name,prod_price form products where vend_id in ('DLL01'.'DLL02') oder by prod_name;等价于
               vend_id='DLL01' or vend_id='DLL02' order by prod_name;
11.select prod_nam,prod_price form products where not vend_id='DLL01' order by prod_name;
             vend_id<>'DLL01'
12.select prod_name,prod_id form products where prod_name like 'Fish%'; 通配符
                   '%bean bag%'
                   'F%y'                                     %匹配多个字符
                   '__ inch teddy bear '           _匹配单个字符
                   ‘[JM]%’                                  开头是J或M的
13.select vend_name+'('+RTRIM(vend_country)+')'  AS vend_title form Vendors order by vend_name;  RTRIMsh()函数是去掉右边的所有空格
14.select prod_id,quantity,item_price,quantity*item_price AS expanded_price form orderItems where order_num = 20008;
15.select vend_name,UPPER(vend_name) AS vend name_upcase from vendors order by vend_name; UPPER()函数是转化为大写
16.select order_num from orders where datepart(yy,order_date)=2004;
17.select AVG(prod_price) AS avg_price from Products where  vend_id = 'DLL01';
18.select count(*) as num_cust from Customers;
19.select count(*) as num_cust,MAX(prod_price) as max_price,MIN(prod_price) as min_price,AVG(prod_price) as avg(prod_price) from productors;
20.select  order_num,count(*) as items from OrderItems group by order_byn having count(*)>=3;
21.inser into customers values('10006','Tony Land','New York','1234'.'null');
22.inser into customers(cust_id,cust_name,cust_adress,cust_state,cust_email) values('10006','Tony Land','New York','1234'.'null'); 此句比上句更安全
23.inser into customers(cust_id,cust_name,cust_adress,cust_state,cust_email) select cust_id,cust_name,cust_adress,cust_state,cust_email from CustNew
24.select * into CustCopy from Customers;  从一个表复制到另一个表
25.update customers setcust_email='newemail@email.com'where cust_id='000001';
26.update customers set cust_contact='Sam robers',cust_email='newemail@email.com' where cust_id='10000000001';
27.delete from customers where cust_id='11101';
28.create table products
{
 prod_id  char(10)  not null,
 vend_id  char(10)  not null,
 prod_name char(254) not null,
 prod_price decimal(8,2) not null,
 prod_desc varchar(100) null
};
29.alter products drop clumn prod_desc;
30.delete table products;
31.create view vendorlocation as select rtrim(vend_name)+'('+rtrim(vend_country)+')' as vend_title from vendors; 创建视图,并参见格式 vend_title
32.create view customerEmailList as select cust_id,cust_name,cust_email from customers where cust_email is not null; 创建个包含 cust_id,cust_name,cust_email 并且email不为空的视图
      select  * from customerEmailList; 查询刚才建立的表内容
33.begin transaction;
 insert/update/delete ...;
 save transaction startorder;
 insert/ipdate/delete ...;
 if @@error<>0 rollback transaction startorder;
 commit transaction           commit和rollback来进行事务管理
34.DELARE CustCursor CURSOR
 FOR
 SELECT * FROM Customers
 WHERE cust_email IS NULL
35.create index prod_name_ind on products(prod_name);
常见SQL问题
1. SQL Server存储过程,与触发器的作用,与优缺点
存储过程就是为了以后使用而保存的一条或多条SQL语句的集合。优点:简化复杂的操作,保证数据的一致性,简化对变动的改动,一定程度提高性能,方便扩展更强大的功能;缺点:不同的DBMS的存储过程的语法有所不同,编写比较复杂,需要更高的技能。
触发器是特定的存储过程,一般与特定表上的insert,update,delete相关联,在其操作执行之前或之后执行。优点:保证数据一致,对于某个表的变动在其他表上执行活动,进行额外的验证并根据需要汇退所有数据,计算计算列的值或更新时间戳。
2.标准的SQL与T-SQL的区别?写出4个SQL命令和3个T-SQL命令。
Transact-SQL 是 Microsoft SQL Server 中对标准 SQL 语言的增强,它是用来让应用程序与 SQL Server 沟通的主要语言。T-SQL 是使用 SQL Server 的核心。与 SQL Server 实例通信的所有应用程序都通过将 Transact-SQL 语句发送到服务器(不考虑应用程序的用户界面)来实现这一点。T-SQL 提供标准 SQL 的 DDL 和 DML 功能,加上延伸的函数、系统内置程序以及程序设计结构(例如 IF 和 WHILE)让程式设计更有弹性。 在 Oracle 数据库中则是 PL/SQL 语言
3.数据库的第三范式
第一范式(1NF):在关系模式R中的每一个具体关系r中,如果每个属性值 都是不可再分的最小数据单位,则称R是第一范式的关系。
第二范式(2NF):如果关系模式R(U,F)中的所有非主属性都完全依赖于任意一个候选关键字,则称关系R 是属于第二范式的。例:如S1(SNO,SNAME,DNO,DNAME,LOCATION) 各属性分别代表学号,
姓名,所在系,系名称,系地址。关键字SNO决定各个属性。由于是单个关键字,没有部分依赖的问题,肯定是2NF。
1NF:一个table中的列是不可再分的(即列的原子性)
2NF:一个table中的行是可以唯一标示的,(即table中的行是不可以有重复的)
3NF:一个table中列不依赖以另一个table中的非主键的列。举个例子吧:有一个部门的table,我们叫它tbl_department, 它有这么几列(dept_id(pk),dept_name,dept_memo...) 有一个员工table,我们叫它tbl_employee,在这个table中有一列dept_id(fk)描述关于部门的信息,若tbl_employee要满足3NF,则在tbl_employee中就不得再有除dept_id列的其它有关部门信息的列!
5NF:将一个table尽可能的分割成小的块,以排除在table中所有冗余的数据
4.数据库表user pk ID int Name (char) Age(int) Sex(bit)
  (1) sex=0,age=20的name降序
  (2) 当sex=1时,age+1
  (3) 删除所有奇数据
  (4) 统计age>20的个数
  (5) 年龄最大的人的名字
(1).Select user_ID,user_name from users where user_sex=0 and user_age=20 order by user_name;
(2).Update users set user_age=user_age+1 where user_sex=1;
(3).Delete from users where user_id ???
(4).Select count(*) as num_age from users where user_age>20;
(5).Select user_name from users where user_age=MAX(user_age);
5.什么是基本表?什么是视图?两者的区别和联系是什么?
表示数据库最重要的容器,存储各类数据。视图是虚拟的表。只包含使用时动态检索数据的查询。视图可以嵌套,不能索引
6.所有的视图是否都可以更新?为什么?
理论上不能,只能察看
视图是一个虚拟表,其内容由查询定义。同真实的表一样,视图包含一系列带有名称的列和行数据。但是,视图并不在数据库中以存储的数据值集形式存在。行和列数据来自由定义视图的查询所引用的表,并且在引用视图时动态生成。
但在Oral数据库里有一种视图可以操作
好像叫“实例化视图”
7.ASCII和Unicode有什么区别?
Unicode是2000以上系统,多平台交流就使用Unicode,ascii是98以前系统的编码方式,单字节编码方式
8.什么是mvc模式
MVC(Model/View/Controller)模式是国外用得比较多的一种设计模式,好象最早是在Smaltalk中出现。MVC包括三类对象。Model是应用对象,View是它在屏幕上的表示,Controller定义用户界面对用户输入的响应方式。
模型-视图-控制器(MVC)是80年代Smalltalk-80出现的一种软件设计模式,现在已经被广泛的使用。

 


TAG: 数据库相关

 

评分:0

我来说两句

Open Toolbar