More about maintaining a table Use speaker notes for additional information!

Slides:



Advertisements
Similar presentations
Creating an Oracle Database Table Additional information is available in speaker notes!
Advertisements

Group functions using SQL Additional information in speaker notes!
Quick-and-dirty.  Commands end in a semi-colon ◦ If you forget, another prompt line shows up  Either continue the command or…  End it with a semi-colon.
A Guide to SQL, Seventh Edition. Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
Queries and SQL in Access Please use speaker notes for additional information!
Table maintenance revisited (again) Please use speaker notes for additional information!
Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial Password: UWPstudent Password is case sensitive.
SQL Use of Functions Character functions Please use speaker notes for additional information!
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Working with Tables: Data Management and Retrieval Dr. Bernard Chen Ph.D. University of Central Arkansas.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Database Fred Durao What is a database? A database is any organized collection of data. Some examples of databases you may encounter in.
SQL (DDL & DML Commands)
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation.
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
Chapter 5 MYSQL Database. Introduction to MYSQL MySQL is the world's most popular open-source database. Open source means that the source code, the programming.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
More on views Please refer to speaker notes for additional information!
SQL and Conditions Speaker notes will provide additional information!
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
Manipulating data within PL/SQL Please use speaker notes for additional information!
Features of SQL SQL is an English-like language . It uses words such as select , insert , delete as part of its commend set. SQL is an a non-procedural.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
SQL Structured Query Language 1. Data Definition Language (DDL) is used to manage table and define data structure i.e. CREATE, ALTER, DROP Data Control.
Indexes in Oracle An Introduction Please check speaker notes for additional information!
# 1# 1 Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? CS 105.
1 SQL-2 Tarek El-Shishtawy Professor Ass. Of Computer Engineering.
SQL CREATING AND MANAGING TABLES lecture4 1. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
More on Primary and Foreign Keys Please see speaker notes for additional information!
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
SQL queries Data Maintenance. RHS – SOC 2 Data maintenance In addition to asking question to the database (queries), we can also maintain the data itself.
Introduction to PL/SQL As usual, use speaker notes for additional information!
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
Basic SQL*Plus edit and execute commands SQL*Plus buffer and built-in editor holds the last SQL statement Statements are created in free-flow style and.
Finish ing the logic flowc harts from week 3.. if invcd = “A” and (amtfst > 500 or amtsnd > 200) move “OKAY” to msg end if With the parenthesis, invcd.
A Guide to SQL, Sixth Edition 1 Chapter 5 Updating Data.
Understand Data Definition Language (DDL) Database Administration Fundamentals LESSON 1.4.
CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together.
Creating and Managing Tables. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically represents subsets.
3 A Guide to MySQL.
Fundamentals of DBMS Notes-1.
CS SQL.
CS 3630 Database Design and Implementation
Chapter 5 Introduction to SQL.
Insert, Update and the rest…
SQL Creating and Managing Tables
On new..
MySQL - Creating donorof database offline
SQL Creating and Managing Tables
Please use speaker notes for additional information!
SQL Creating and Managing Tables
“Manipulating Data” Lecture 6.
Introduction to Views and Reports
“Manipulating Data” Lecture 6.
This allows me to insert data for specified fields and not for other fields in the structure.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Updating Databases With Open SQL
Introduction to Oracle
SQL NOT NULL Constraint
Updating Databases With Open SQL
Presentation transcript:

More about maintaining a table Use speaker notes for additional information!

SQL> CREATE TABLE stu_maintain 2 (stuidno VARCHAR2(4), 3 name VARCHAR2(20), 4 major CHAR(2), 5 numcrs NUMBER(3), 6 gpa NUMBER(5,3), 7 stdate DATE); Table created. CREATE TABLE To review the create process, when you create a table you issue the command CREATE TABLE followed by the table name. Then, within parenthesis, you list the column/field names followed by the type and length where needed. When all fields have been listed, close the parenthesis and key in the semi-colon.

DESCRIBE SQL> DESCRIBE stu_maintain; Name Null? Type STUIDNO VARCHAR2(4) NAME VARCHAR2(20) MAJOR CHAR(2) NUMCRS NUMBER(3) GPA NUMBER(5,3) STDATE DATE The DESCRIBE command followed by the table name is used to show the structure of the table.

SQL> INSERT INTO stu_maintain 2 VALUES('1111','John Randolph', 'CI',12,3.333,'10-SEP-98'); 1 row created. SQL> INSERT INTO stu_maintain 2 VALUES ('2222','Susan Quincy', 'CI', 15, 3.250, '11-SEP-99'); 1 row created. SQL> SELECT * 2 FROM stu_maintain; STUI NAME MA NUMCRS GPA STDATE John Randolph CI SEP Susan Quincy CI SEP-99 INSERT INTO To review, the INSERT INTO command followed by the name of the table and then the word VALUES followed by the values needed will create a row/record in the table. Note that the VALUES have to be in the same order as the columns/fields on the file. When I did the CREATE TABLE, stuidno was first so the data for that field is also entered first. Name was second so the data for name is entered second.

SQL> INSERT INTO stu_maintain (stuidno, name, stdate) 2 VALUES('3333','Ann Tanner','23-JAN-00'); 1 row created. SQL> SELECT * 2 FROM stu_maintain; STUI NAME MA NUMCRS GPA STDATE John Randolph CI SEP Susan Quincy CI SEP Ann Tanner 23-JAN-00 INSERT INTO If you only want to put data in specific fields, you can list the fields after the table name and then only enter data for the names specified.

SQL> UPDATE stu_maintain 2 SET major = 'BU' 3 WHERE stuidno = '3333'; 1 row updated. SQL> SELECT * 2 FROM stu_maintain; STUI NAME MA NUMCRS GPA STDATE John Randolph CI SEP Susan Quincy CI SEP Ann Tanner BU 23-JAN-00 UPDATE In this example, I want to update an existing row/record by changing the major which has no data to BU. I want to make this change only for the record where the stuidno is If you eliminate the WHERE clause, all records would be changed. STUI NAME MA NUMCRS GPA STDATE John Randolph CI SEP Susan Quincy CI SEP Ann Tanner 23-JAN-00

STUI NAME MA NUMCRS GPA STDATE John Randolph CI SEP Susan Quincy CI SEP Ann Tanner BU 23-JAN-00 SQL> UPDATE stu_maintain 2 SET numcrs = 4, gpa = WHERE stuidno = '3333'; 1 row updated. SQL> SELECT * 2 FROM stu_maintain; STUI NAME MA NUMCRS GPA STDATE John Randolph CI SEP Susan Quincy CI SEP Ann Tanner BU JAN-00 UPDATE

SQL> INSERT INTO stu_maintain (stuidno, name) 2 VALUES ('3434','TO DELETE'); 1 row created. SQL> SELECT * 2 FROM stu_maintain; STUI NAME MA NUMCRS GPA STDATE John Randolph CI SEP Susan Quincy CI SEP Ann Tanner BU JAN TO DELETE SQL> DELETE FROM stu_maintain 2 WHERE stuidno = '3434'; 1 row deleted. SQL> SELECT * 2 FROM stu_maintain; STUI NAME MA NUMCRS GPA STDATE John Randolph CI SEP Susan Quincy CI SEP Ann Tanner BU JAN-00 DELETE FROM DELETE FROM DELETE FROM removes the specified row/record from the table.

SQL> ALTER TABLE stu_maintain 2 ADD (stu_opt VARCHAR2(5)); Table altered. SQL> DESCRIBE stu_maintain; Name Null? Type STUIDNO VARCHAR2(4) NAME VARCHAR2(20) MAJOR CHAR(2) NUMCRS NUMBER(3) GPA NUMBER(5,3) STDATE DATE STU_OPT VARCHAR2(5) ALTER TABLE SQL> SELECT * 2 FROM stu_maintain; STUI NAME MA NUMCRS GPA STDATE STU_O John Randolph CI SEP Susan Quincy CI SEP Ann Tanner BU JAN-00

SQL> INSERT INTO stu_maintain (stu_opt) 2 SET stu_ 3 SQL> UPDATE stu_maintain 2 SET stu_opt = 'prog' 3 WHERE stuidno = '2222'; UPDATE SQL> UPDATE stu_maintain 2 SET stu_opt = 'trans' 3 WHERE stuidno = '1111' OR stuidno = '3333'; 2 rows updated. SQL> SELECT * 2 FROM stu_maintain; STUI NAME MA NUMCRS GPA STDATE STU_O John Randolph CI SEP-98 trans 2222 Susan Quincy CI SEP-99 prog 3333 Ann Tanner BU JAN-00 trans Above, I updated stu_opt for stuidno 2222 and below I updated stu_opt for stuidno 1111 and The results are shown in the SELECT.

SQL> DESC stu_maintain; Name Null? Type STUIDNO VARCHAR2(4) NAME VARCHAR2(20) MAJOR CHAR(2) NUMCRS NUMBER(3) GPA NUMBER(5,3) STDATE DATE STU_OPT VARCHAR2(5) SQL> ALTER TABLE stu_maintain 2 MODIFY (stu_opt VARCHAR2(10)); Table altered. SQL> DESC stu_maintain; Name Null? Type STUIDNO VARCHAR2(4) NAME VARCHAR2(20) MAJOR CHAR(2) NUMCRS NUMBER(3) GPA NUMBER(5,3) STDATE DATE STU_OPT VARCHAR2(10) SQL> SELECT * 2 FROM stu_maintain; STUI NAME MA NUMCRS GPA STDATE STU_OPT John Randolph CI SEP-98 trans 2222 Susan Quincy CI SEP-99 prog 3333 Ann Tanner BU JAN-00 trans ALTER MODIFY ALTER MODIFY Original table structure had stu_opt 5 characters. After modification, stu_opt is 10 characters. MODIFY changes the structure of the table.

UPDATE STUI NAME MA NUMCRS GPA STDATE STU_OPT John Randolph CI SEP-98 trans 2222 Susan Quincy CI SEP-99 prog 3333 Ann Tanner BU JAN-00 trans SQL> UPDATE stu_maintain 2 SET stu_opt = 'transfer' 3 WHERE stuidno = '1111' OR stuidno = '3333'; 2 rows updated. SQL> SELECT * 2 FROM stu_maintain; STUI NAME MA NUMCRS GPA STDATE STU_OPT John Randolph CI SEP-98 transfer 2222 Susan Quincy CI SEP-99 prog 3333 Ann Tanner BU JAN-00 transfer

SQL> ALTER TABLE stu_maintain 2 MODIFY (stu_opt VARCHAR2(5)); MODIFY (stu_opt VARCHAR2(5)) * ERROR at line 2: ORA-01441: column to be modified must be empty to decrease column length ALTER MODIFY ALTER MODIFY In this example, I tried to decrease the size of the stu_opt field. The field contained data that was more than 5 characters so data would have been lost with this decrease. Note the error message that resulted. STUI NAME MA NUMCRS GPA STDATE STU_OPT John Randolph CI SEP-98 transfer 2222 Susan Quincy CI SEP-99 prog 3333 Ann Tanner BU JAN-00 transfer

UPDATE SQL> UPDATE stu_maintain 2 SET GPA = 4.000; 3 rows updated. SQL> SELECT * 2 FROM stu_maintain; STUI NAME MA NUMCRS GPA STDATE STU_OPT John Randolph CI SEP-98 transfer 2222 Susan Quincy CI SEP-99 prog 3333 Ann Tanner BU JAN-00 transfer When the WHERE clause is omitted, the UPDATE effects all records.