已阅读5页,还剩89页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验1 ORACLE数据库SQL语句练习实验实验类别:验证性 实验级别:必做开课单位:软件学院软件工程系 实验时数:12学时一、实验目的:1、 了解DBMS系统的功能、软件组成;2、掌握利用SQL语句定义、操纵数据库的方法。二、实验要求:1、在课外安装相关软件并浏览软件自带的帮助文件和功能菜单,了解DBMS的功能、结构; 2、创建一个有两个关系表的数据库;(建议采用ORACLE ISQLPLUS)3、数据库、关系表定义;4、学习定义关系表的约束(主键、外键、自定义);5、了解SQL的数据定义功能;6、了解SQL的操纵功能;7、 掌握典型的SQL语句的功能;8、 了解视图的概念; 三、实验设备:计算机、数据库管理系统如DB2,Oracle 等软件。四、建议的实验步骤:0、关闭drcom软件,或深大登陆客户机。登陆oracle见附件11、使用SQL语句建立关系数据库模式及数据如下;(注:若为日期,因为该oracle服务器采用中文内核,所以日期必须写成17-12月-90)EMP:EMPNO ENAME JOB MGRHIREDATESAL COMM DEPTNO7369SMITHCLERK790217-Dec-9013750 207499ALLENSALESMAN 7698 20-FEB-89 190006400307521WARD SALESMAN 7698 22-FEB-93 185004250307566JONES MANAGER 7839 02-APR-89 26850207654MARTIN SALESMAN 7698 28-SEP-97 156753500307698BLAKE MANAGER 7839 01-MAY-90 24000 307782CLARK MANAGER7839 09-JUN-88 27500 107788SCOTT ANALYST 7566 19-APR-87 19500207839KING PRESIDENT 17-NOV-83 82500107844TURNER SALESMAN 7698 08-SEP-92 185006250307876ADAMS CLERK 7788 23-MAY-96 11900207900JAMES CLERK 7698 03-DEC-95 12500307902FORD ANALYST 7566 03-DEC-91 21500207934MILLER CLERK 7782 23-JAN-95REENSALESMAN442224-Jul-95185002750504422STEVENSMANAGER783914-Jan-9424750506548BARNESCLERK442216-Jan-951195050DEPT:DEPTNO DNAME LOC10ACCOUNTING LONDON20RESEARCH PRESTON30 SALES LIVERPOOL40 OPERATIONS STAFFORD50MARKETING LUTON2、用SQL定义数据库的关系表;注:(每位同学在各自创建的图表名字后面添加自己学号末三位以示区分,如EMP112等)3、定义各个关系的字段和自定义的数据完整性约束;4、确定关系表的主键、外键;5、对照帮助文件和教材理解主键和外键的约束规则;6、分别为关系表添加记录;7、保存数据库以便进行后续实验。(运用commit命令)8、理解SQL语句和关系运算的关系;9、练习典型的SQL语句,对第6步实验中已建立的表做查询、插入、更新、删除等操作;完成练习题。10、检查同学作业的思考题是否存在问题。注:以上具体步骤可参见帮助文件SQL handbook 或相关书籍。五、 实验1实验报告要求格式:1、实验目的:2、实验时间:3、完成的实验内容:4、实验设备和实验环境:5、实验结果和结论(运行的结果):6、思考题:SQL Handbook中 EX1,任选3题,Ex2, 任选2题 Ex3,第6以及第9题Ex4,第8题以及其他任选两题Ex5, 5,6Ex6,任选2题必须写出sql语句以及运行结果。附件一、Oracle ISQLPLUS 登录地址1)登录48/isqlplus输入用户名: scott密码:tigerSID: CIE在出现的窗体内输入SQL语句。并按“执行”。创建了数据库以及更改数据之后,请输入COMMIT;命令保存数据。91附件二、SQLhanbookCONTENTSp 2-3Introduction - PLEASE READ4Logging in and out5-6Editing SQL statements6SAVE commands7Running files. The SPOOL command8SELECT data9Creating the tables you need10The Data for the examples11-16The Basic SELECT statement17-18Exercise 119JOINing tables20Joining a table to itself21Outer Joins22Exercise 223-7SQL functions28-30Group Functions31Exercise 332-4Date Functions35Exercise 436-7GROUP BY38-9The HAVING clause40Exercise 541-4Subqueries45Exercise 646-8Adding, Updating and Deleting data49The COMMIT statement50-1CREATE tables52Data types53ALTER/MODIFY54DROP tables55-8VIEWS59Exercise 760-1Set Operators62-71Output Formatting72-3Procedures /Parameters74-5SQL Dictionary76-7SQL*PLUS dictionary78Logical OperatorsThis book has been designed to help you learn SQL as it has to be learnt by doing, not by teaching.It is therefore in your best interest to work your way through it (10-15 hours work) systematically.You may be asked to submit some of the answers to the exercises, for an assignment and may be asked specific details in an exam.If you have Oracle 8 at home then you will find that the EMP and DEPT tables come with Oracle, but with old data. It is better to extract the createempdept2.sql file from WebCT, copy it to a floppy and take it home:Login to Oracle SQL and type ay the SQL prompta:createempdept2when it has finished, typeCOMMITThis will save the data tables to your hard disk permanently. You can then run through the exercises/examples.Note that because Oracle stores tables in its own file system, your tables are invisible to Windows and cannot be copied. You need to recreate them on each system.WARNINGThere are dozens of different versions of Oracle and many (sometimes all) will not install on PCs because of Windows versions and other software running. If this happens, you are advised to do all the Oracle work at the UNI.SQL (Structured Query Language) is a relational database language. Amongst other things the language consists of statements to insert, update, query and protect data. Although SQL is not a DBMS, for simplicity in this manual SQL will be considered as a DBMS as well as a language. Of course, in the places where it is necessary, a distinction will be drawn.There are a few things to note about SQL as a database language, because it is a relational database language, SQL may be grouped with the non-procedural database languages. By non-procedural it is meant that users (with the help of the various statements) have only to specify which data they want and not how this data must be found. C+, Java and VB are examples of Procedural languages. It also means that there are no variables, IF statements or loop constructs. Because it is non procedural, it is very difficult to teach, and the only way to learn it is by working through this book and picking up how certain results can be achieved.SQL can be used in two ways. First, interpretively: an SQL statement is entered at a terminal or PC and immediately processed or interpreted. The result is also visible immediately. This is known as interactive SQL. The second way is known as embedded SQL. The SQL statements are embedded in a program written in another, procedural language. Results of these statements are not immediately visible to the user, but are processed by the enveloping program. In this module we shall be assuming the interpretive use of SQL.SQL has already been implemented by many manufacturers as the database language for their DBMS. It is not the case, therefore, that SQL is the name of a particular manufacturers product available on the market today. However, it is the market standard and you will find many career opportunities within the general SQL field. Currently it is number one in the Jobs market.Some manufacturers are now providing SQL-server machines. These machines can be connected to a DBMS, and they carry out all the database functions defined in SQL. Thus SQL is now a data interchange language between any systems that can speak SQL. Typically, an SQL-server is placed on a LAN where it processes all database operations for clients on the LAN.Please note that, although SQL is an ISO standard, each manufacturer have their own add-ons. For Oracle, these are the SQL*PLUS commands. They will not work in a non-Oracle environment. Specifically, the date formatting commands are very different.LOGGING IN TO ORACLEThis example shows how to start SQL*PLUSN.B. This information will be confirmed by your lecturer when you start the course, because the first page or two relate to Windows NT or XP. 1. You first need to get SQL on your desktop. It can be found under Applications, Programming Tools and Oracle. 2.Make sure that you have a user ID and Password for the database. These will be created by your Module leader once he/she knows you exist. Your user_id will normally be your surname followed by an underscore, followed by your first intial. E.g. JONES_J (not case sensitive). Your password will initially be set to CHANGEME, but can be changed byGRANT CONNECT TO youruserid IDENTIFIED BY newpassword;3.Within WINDOWS double click on the SQL iconSQL*PLUS asks for 3 pieces of information. YOU MUST ENTER ALL 3 BEFORE YOU HIT RETURN, otherwise it wont let you in and you will have to start all over again. This becomes tedious. (At home you do NOT need a database name)4.Enter your USER ID, Password and STUD2 in the 3 boxes and click on OKSQL is displayed on the screen and indicates a successful log on.If you get an error it may be caused by one of 3 things:a) your user-id or password were wrongb) you didnt enter all 3 pieces of information before the returnc) Oracle is being backed up (this will normally occur for a few hours each week when no Oracle lab classes are scheduled. Your module tutor will tell you which time (same each week) is affected.TO EXIT SQL*PLUSAt the SQL prompt, type EXIT, this will disconnect you from the database. Do this rather than clicking the x icon in the top right corner to prevent possible loss of data.ENTERING AND EXECUTING COMMANDS.Commands can be entered at the SQL prompt and are terminated with a semicolon ;with a slash / on a line by itself, or with a blank line.A semicolon tells SQL that you want to run the command, when you press return the command will be processed. The majority of statements in this book will require a ; to execute. Because SQL will not execute until it sees a ;, you can build up multi-line statements. A at the end of the first line will simply go onto the next with a 2 instead of the SQL prompt to show you are on line 2. If you were expecting a response and get a 2 it means you have forgotten the ;, this can then be put on line 2 (on its own) to execute.A blank line tells SQL*PLUS that you have finished entering the command but you do not want to run it yet. The command will be stored in a part of the memory called the SQL Buffer. You can edit the current command stored in the buffer if you wish, or you can run it again by entering the RUN or / command. This will list the SQL command currently in the buffer and then run it. Once you start entering another command, it will overwrite the buffer. So if you want to keep the previous command, you must SAVE it (page 4).IN TERMS OF LANGUAGE, ORACLE IS NOT CASE SENSITIVE, BUT IN TERMS OF THE DATA STORED, IT IS.i.e. a name of BROWN is different to Brown is different to brownAs with any other language, character strings and dates must be in quotes. EDITING COMMANDS.Unless you enjoy typing, it is often easier to edit multi-line statements. This can be done in one of several ways.a) You can edit a command entered directly to the command prompt by using Backspace key or by re-entering the command which overwrites the original.b) By editing the buffer using a line editor. WARNING - if you invoke the system editor, when you return to SQL the Paging is often messed up resulting in half lines coming out, lines overwriting each other etc. However, you can cut and paste into/from a package like NOTEPAD.c) You can use the mouse to copy and paste any preceding text. This is a useful trick requiring practice. Click and highlight the target text with the left mouse button. With the left button still down, click the right button. This pastes the text into the current buffer.After all these, you need to re-run the current buffer. Use:RUNRlists and runs the SQL statement/runs the SQL statement without first listing itWhen you list the buffer contents the semicolon terminating the command is not shown as it is not stored in the buffer, this makes it more convenient to edit commands. To run the command after editing, use RUN or / at the SQL promptNote that the mentioned earlier is used to execute a command file containing multiple SQL statements. If you had a saved file called a.sql which contained 3 sql statements, thenRUN Awould only execute the first.a will run all 3 consecutively.USING AND SAVING COMMANDS FOR LATER USE.List the current command and follow it at the SQL prompt with SAVE file-name.a .SQL extension is automatically added to the filename, and it is stored in your WINDOWS directory. (NOTE - the tables you use/create are not stored in your area but on the ORACLE file server). NOTE the default directory for oracle is on r drive to which you have no write access. You need to change your directory by doing an OPEN or SAVE AS, finding your windows directory and then click CANCEL. Oracle will thereafter look in that directory for your .SQL files. Unfortunately, this needs doing every time you use SQL.SQLSAVE When saving files for later use DO NOT terminate with a semicolon, if you do SQL*PLUS will attempt to run the buffer contents and an error will occur when the SAVE command is encountered. (SAVE is not an SQL command).RETRIEVING COMMAND FILESTo use a file that is stored you first need to retrieve it by using the GET command.SQL GET file-nameagain you do not need the SQL extension. See above note for directories.RUNNING COMMAND FILESThe START command retrieves a command file and runs the command(s) it containsSQL START file-nameagain you do not need the .SQL extension.You can only run a single SQL statement with a START. When loading data this is a nuisance as you require lots of statements. Fortunately you can also use the command to run a command file. A normal editor (e.g. NOTEPAD) can be used to create a file of any number of SQL statements (each terminated by a ;). This can then be executed by:SQL file-nameA useful query statement is DESCRIBE or DESC this will list the column names and types of the specified table, and give information on data types, sizes etc.SQLSPOOL copies all subsequent terminal output to the specified file (useful for your second assignment). The actual file created will be filename.LST and will be saved by default to your WINDOWS directory. Print it from NOTEPAD as WORD messes up the formatting.SQLSPOOL OFFterminates spooling of output and closes the file.SELECTING DATA FROM TABLES.The SELECT Command is the basis of all queries on tables, therefore its full description is given to show its power. Examples of the various formats are provided after the description.SELECT ALL / DISTINCT TABLE * expr alias , expr alias. FROM table alias, table alias, . WHERE condition set condition CONNECT BY condition START WITH condition GROUP BY expr, expr,. HAVING condition UNION / INTERSECT / MINUS SELECT. ORDER BY expr/posn ASC/DESC, expr/posn ASCDESC,. FOR UPDATE OF column, column. NOWAIT ;NOTE - All SELECT statements must include a FROMDescription: Selects rows and columns from one or more tables. May be used as a command, or (with certain restrictions on Clauses) as a subquery in another SELECT, and UPDATE, or other SQL command.Dont worry too much about this generic syntax list as you will see all kinds of examples throughout this book.PARAMETERS AND CLAUSES.ALL makes SELECT display all rows produced by the query. Since this is the default, it is generally not neededDISTINCT makes it omit duplicate rows.* makes SELECT display all columns of the table(s) specified by FROM, in the order they were defined when the table(s) were created.i.e. SELECT * FROM EMP; Alternatively, each expression becomes one column in the display.i.e. SELECT EMPNO, ENAME FROM EMP;displays only the named columns in the expression.Each alias, if specified, is used to label the preceding expression in the displayed table.e.g. SELECT EMPNAME “Name”, SAL “Salary” from EMP;Note the use of double quotes here. character strings are delimited by single quotes.FROM table specifies the table or view to be drawn on. More than one table implies a join. Alias, if specified, may be used as an alias for the preceding table through the rest of the SELECT command.SELECTED EXAMPLES AND WORKSHEETS.The examples in this book should be worked through carefully to ensure that you understand what the commands are doing. Your assignment work will assume that knowledge.You will need the following tables, EMP and DEPT.These can be created with the following commands:CREATE TABLE EMP AS SELECT * FROM COMPUTING.EMP;CREATE TABLE DEPT AS SELECT * FROM COMPUTING.DEPT;The data in them is currently as shown on the next page:Note - if the table contents become corrupted (particularly after the Update example in Exercise 7), you can always delete the tables and start again. This can be achieved by:DROP TABLE EMP;DROP TABLE DEPT;To list all the tables in your Oracle area:SELECT * FROM TAB;THE DATA USED IN THESE EXERCISES:EMPNO ENAME JOB MGR HIREDATESAL COMM DEPTNO- 7369 SMITHCLERK 7902 17-DEC-9013750 20 7499 ALLENSALESMAN 7698 20-FEB-89 19000 6400 30 7521 WARD SALESMA
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2027年4月电工证考试理论知识易错题卷(高清版)及答案
- 高中物理力学实验观察动量守恒定律验证测试技巧训练试题及答案
- 初中数学几何图形圆的内接四边形性质应用技巧训练试题及答案
- 2027届贵州省六盘水市高三上学期第一次统测历史试题(含答案)
- 高中地理南亚主要国家农业发展区域特征试题及答案
- 2026年度学校领导班子述职述廉述综报告
- 2026年标准贵州省水利厅安全员b证考试题库及答案
- 特种设备作业人员考试题库及答案
- 2025年二级建造师二建水利水电工程管理与实务真题及解析
- 河北省廊坊市管理执法辅助人员招聘考试《法律基础知识》国考真题
- (2026版)《建筑公司经营部年终工作总结》
- 2026地球小博士知识竞赛试题附答案
- 2026新教材语文 2《啊我们踯躅于黑暗的丛林里!》教学课件
- 浙江台州市2026届高三第二次教学质量评估英语+答案
- 《应用文写作实践教程》中职全套教学课件
- 土方平整施工方案
- 贵州省2025年12月普通高中学业水平合格性考试英语试卷
- 国开《人文英语2》综合测试任务答案
- 《机械制图》电子教材
- 中医护理操作技术热奄包
- 西安美食广场租赁协议
评论
0/150
提交评论