The Language of SQL- P42 ppsx

5 216 0
The Language of SQL- P42 ppsx

Đang tải... (xem toàn văn)

Thông tin tài liệu

■ The third column in the table is named ColumnThree and defined as a VARCHAR datatype with a length of 25. This column will allow NULL values. ■ The fourth column in the table is named ColumnFour and defined as a FLOAT datatype and will allow NULL values. It will be given a default value of 10. Here is the CREATE TABLE statement in Microsoft SQL Server that accom- plishes this: CREATE TABLE MyTable (ColumnOne INT IDENTITY (1,1) PRIMARY KEY NOT NULL, ColumnTwo INT NOT NULL REFERENCES RelatedTable (FirstColumn), ColumnThree VARCHAR (25) NULL, ColumnFour FLOAT NULL DEFAULT (10) ) DATABASE DIFFERENCES: MySQL and Oracle The same CREATE TABLE statement in MySQL looks like: CREATE TABLE MyTable ColumnOne INT AUTO_INCREMENT PRIMARY KEY NOT NULL ColumnTwo INT NOT NULL, ColumnThree VARCHAR (25) NULL, ColumnFour FLOAT NULL DEFAULT 10, CONSTRAINT FOREIGN KEY (ColumnTwo) REFERENCES 'RelatedTable' (FirstColumn) ); The same statement in Oracle is: CREATE TABLE MyTable (ColumnOne INT PRIMARY KEY NOT NULL ColumnTwo INT NOT NULL, ColumnThree VARCHAR2 (25) NULL, ColumnFour FLOAT DEFAULT 10 NULL, CONSTRAINT "ForeignKey" FOREIGN KEY (ColumnTwo) REFERENCES RelatedTable (FirstColumn) ); As previously mentioned, Oracle doesn’t allow for auto-increment columns. After a table is created, an ALTER TABLE statement can be used to modify spe- cific attributes of the table. Due to its complexity and to the vast differences Creating Tables 191 between databases, the syntax for the ALTER TABLE won’t be covered in this book. As one example, if you wanted to modify MyTable to eliminate the Column- Three column from the table, you would need to issue this ALTER TABLE statement: ALTER TABLE MyTable DROP COLUMN ColumnThree The syntax for deleting a table is simple. To delete MyTable, issue this statement: DROP TABLE MyTable Creating Indexes SQL provides a CREATE INDEX statement to create indexes after the table is created. You can also use the ALTER TABLE statement to add or modify indexes. If you would like to add a new index on the ColumnFour column in MyTable table, the syntax in Microsoft SQL Server is: CREATE INDEX Index2 ON MyTable (ColumnFour) This creates a new index named index2. To delete an index, simply issue a DROP INDEX statement such as: DROP INDEX Index2 ON MyTable DATABASE DIFFERENCES: Oracle In Oracle, the equivalent DROP INDEX statement is: DROP INDEX Index2; Looking Ahead The SQL statements that add or modify tables and indexes are complex but relatively unimportant to learn in detail. Database software generally provides graphical tools to modify the structure of tables without having to resort to is- suing SQL statements. The important concepts to take from this chapter are a Chapter 18 ■ Maintaining Tables192 knowledge of the various table attributes, including an understanding of how indexes and primary and foreign keys are related to each other. In our next chapter, ‘‘Principles of Databa se Design,’’ we move from the rela- tively mundane task of creating tables to the much broader topic of database design. Just as tables are normally created before their data is accessed, it’s also true that databases are normally designed before tables are created in those databases. So in a sense, we’re moving in reverse through topics that are normally introduced before data retrieval is ever attempted. The specific design of your database is, of course, an essential compo nent of your ability to deliver quality results via SQL. If a database is poorly designed, you’re going to be hindered in your subsequent attempts to retrieve data. Basic knowledge of the database design principles discussed in the next chapter can go a long way toward ensuring a quality database environment. Looking Ahead 193 This page intentionally left blank chapter 19 Principles of Database Design In our first chapter, we introduced the notion that relational databases are a collection of data, stored in any number of tables. The tables are assumed to be related to each other in some fashion. In the prior chapter on maintaining tables, we made clear that database designers can, if they want, assign foreign keys to ensure that certain relationships between tables are maintained properly. However, even with our knowledge of primary and foreign keys, we still have not yet addressed the basic issue of how to design a database in the first place. The main questions to address are the following: ■ How shou ld data be organized into a set of related tables? ■ What data elements should be placed in each table? Once tables and their data elements are defined, then a database administrator can go about the business of creating foreign keys, indexes, appropriate data- types, and so on. There will never be a single correct answer to the above two questions. Besides the fact that every organization or business is unique, it is also true that there is no definitive solution for any given business. Much depends on how flexible a business wants its data design to be. Another obvious factor is the existence of current data. Very few organizations have the luxury of designing their databases in a vacuum, apart from what already exists. 195 . with our knowledge of primary and foreign keys, we still have not yet addressed the basic issue of how to design a database in the first place. The main questions to address are the following: ■. a business wants its data design to be. Another obvious factor is the existence of current data. Very few organizations have the luxury of designing their databases in a vacuum, apart from what. Tables192 knowledge of the various table attributes, including an understanding of how indexes and primary and foreign keys are related to each other. In our next chapter, ‘‘Principles of Databa se

Ngày đăng: 05/07/2014, 05:20

Mục lục

    Chapter 1 Relational Databases and SQL

    Microsoft SQL Server, Oracle, and MySQL

    Primary and Foreign Keys

    The Significance of SQL

    Chapter 2 Basic Data Retrieval

    Column Names with Embedded Spaces

    Chapter 3 Calculations and Aliases

    The Function of Functions

    Sorting in Ascending Order

    Sorting in Descending Order

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan