Go on to the advanced level !! (I personally think this post is very helpful for beginners. please read it carefully. Due to a rush of time, please correct me if you have any errors. in the SQL * PLUS post, we have mastered some basic oracle operations, such as creating, authorizing users, and creating databases. In the visual window environment of the OEM (OracleEnterpriseManager ),
Go on continues to the advanced level !! (I personally think this post is very helpful for beginners. please read it carefully. Due to time Rush, please correct me if there is any error)
SQL * PLUS basics
In the previous post, we learned some basic oracle operations, such as creating, authorizing users, and creating databases. In the visual window environment of the OEM (Oracle Enterprise Manager), although we can do these things very conveniently, in fact, it is more efficient to write in the SQL language in development !! SQL * Plus provided by oracle is a good tool. if you like the development environment of windows, use SQLPlus Worksheet! Let's talk about the basic West! The Structure Query Language (SQL) is a structured Query Language and the core Language of a database. it is a set-oriented descriptive non-procedural Language.
There are four types of SQL languages: DQL, DML, DDL, and DCL. 1. the basic structure of the data query language DQL is a query block consisting of the select clause, from clause, and where clause:
Select <字段名表> From <表或视图名> Where <查询条件> 2. Data Manipulation Language (DML ):
Insert to add rows to the table
Delete a data row from a table
Update and change the data in the table 3. Data Definition Language DDL defines the structure of the database, including the database itself, data tables, directories, views, and other database elements (below are some common DDL statements)
Create table
Create index
Create view
Alter table: add a table column, redefine the table column, and change the storage allocation.
Drop table delete table
Drop index delete index 4. the database control language DCL is used to grant or revoke some privileges to access the database, control the time and effect of database operations, and monitor the database. For example:
Grant permissions or roles to users or other roles
Revoke revokes user permissions
Roll rollback is a protection operation performed by oracle when a dialog changes the data in the database and the user does not want to submit the change for some reason. This is a state that restores information to the last submitted state before update, insert, and delete.
Commit. When you insert, delete, and modify a database, it is only after the transaction is committed to the database. only the database owner can see the transaction before it is committed, only after the final submission is complete can others see it. Next, let's take a look at SQL * Plus to lay a solid foundation for what we will do below.
After logging on to SQL * Plus using system, we will perform the following operations (this time, no details are provided)
SQL> create user maxuan identified by max; # create a maxuan user with the max password
SQL> grant connect, resource to maxuan; # authorize maxuan
SQL> conn maxuan/max; # connect with your maxuan
L> create table test (a number); # create A table named test. only one column with the field name a is created, and the data type is numeric.
SQL> insert into test values (1); # insert a record
SQL> select * from test; # Query record. the first row of column A is 1.
SQL> update test set a = 2; # Change the Record. at this time, the first row of column A has been changed to 2.
SQL> commit; # submit
SQL> delete from test; # delete all records in the test table. at this time, there are no records in the test table.
SQL> roll; # query the test table before rolling back to submit. The value of the first row of column A is returned to the data type of 2oracle.
When creating a data table in a database, we need to define the types of all fields in the table. The data types include character, numberic, date, lob, and raw, these are the most basic data types. Of course, you can also customize data types in oracle! Character data types provided in oracle:
Char ( ): A fixed-length string with a maximum length of 2000 bytes. if the length is not specified, the default length is 1 byte.
Varchar2 ( ): A variable-length string with a maximum length of 4000 bytes. The maximum length is specified in the definition, this type can contain all characters in numbers, letters, and ASCII character sets (or character set standards accepted by database systems such as EBCDIC. If the data length does not reach the maximum value, oracle automatically adjusts the field length based on the data size. Is the longest data type.
Nchar ( ): A fixed-length string based on the character set. it can contain a maximum of 2000 bytes.
Nvarchar2 ( ): A variable-length string based on the character set. it can contain a maximum of 4000 bytes.
Long: variable long character column. The maximum length is 2 GB. it is used for long string data that does not need to be searched by string. This type is a legacy data type that will not be supported in the future. it is gradually replaced by BLOB, CLOB, NCLOB, and other big data types. The numberic data type is used to store negative and positive integers, scores, and floating point data. the numberic data type provided in oracle:
Number ( , ): A variable length value column, which can be 0, positive, or negative. m is the number of digits of all valid numbers, and n is the number of digits after the decimal point. Date data types provided in oracle:
Date: the default format is dd-mon-yy (day-month-year). The lob data type provided in oracle is as follows:
Blob, clob, and nclob: three large objects (lob) are used to save large image files or formatted text files, such as Word documents and non-text files such as audio and video files, the maximum length is 4 GB. Dizzy data is stored in the database.
Bfile: a large binary object file stored outside the database. The maximum length is 4 GB. This type of LOB is used to record changes in the database, however, data is stored outside the database. Raw data types provided in oracle:
Raw ( ): Variable-length binary data. The maximum length must be specified when defining a field. this format is used to save a small image file or a text file with a format, it is also an old data type that will be replaced by the lob data type.
Long raw: Variable-length binary data with a maximum length of 2 GB. it can be used to save large graphics or text files with formats, as well as non-text files such as audio and video files, this is also an old data type, which will be replaced by the lob data type. Other data types:
Rowid: this is a pseudo-sample in the oracle data table. it is the unique identifier of each row of data in the data table.
Integer: integer type
After creating the shopping website background database, we now go back to the theme of developing a shopping website with J2EE system and start to build the background database of the shopping website.
To implement the basic functions of a shopping website, we need to create four tables: product list, item, orders, and admin ). The table structure is as follows: item table structure (item type table)
Field name data type allow null primary key/foreign key remarks
Type_id INTEGER (automatic number) no primary key item category ID tag
Type varchar2 (30) no product category name product table structure (item list)
Field name data type allow null primary key/foreign key remarks
Product_id INTEGER (automatic number) no primary key item ID mark
Title varchar2 (30) no product name
Type_id INTEGER no foreign key product category tag
Info varchar2 (80) is product introduction
Price number () no product price orders table structure (order list)
Field name data type allow null primary key/foreign key remarks
Order_id INTEGER (automatic number) no primary key order ID mark
Name varchar2 (20) no customer name
Address varchar2 (100) is the shipping address
Tel number (16) is the contact number
Email varchar2 (30) no contact email
Btime date is the order date.
Product_id INTEGER no foreign key product tag
Uword varchar2 (100) is the admin table structure of the customer message (administrator list)
Field name data type allow null primary key/foreign key remarks
Admin_id INTEGER (automatic number) no primary key administrator ID mark
Adminname varchar2 (20) no administrator name
Password varchar2 (20) No. after the administrator password is designed, the table structure is created.
It is no longer difficult to create a table. Note the association between the three tables "product", "item", and "orders", as well as the automatic number. The following is a complete SQL statement. I will give a detailed description later. you can compare it with the input in SQL * Plus or save it as an SQL script file, run the command in SQL * Plus or SQLPlus Worksheet. Of course, you can also copy the code directly to SQL * Plus for execution! Code copy box
Rem // by maxuan start // create table item (type_id integer not null, type varchar2 (30), constraint item_pk primary key (type_id )); create table product (product_id integer not null, title varchar2 (30) not null, type_id integer not null, info varchar2 (80), price number (16, 2) not null, constraint product_pk primary key (product_id), constraint product_fk foreign key (type_id) references item (type_id); create table orders (order_id integer not null, name varchar2 (20) not null, address varchar2 (100), tel number (16), email varchar2 (30) not null, btime date, product_id integer not null, uword varchar2 (100 ), constraint orders_pk primary key (order_id), constraint orders_fk foreign key (product_id) references product (product_id); create table admin (admin_id integer not null, adminname varchar2 (20) not null, password varchar2 (20) not null, constraint admin_pk primary key (admin_id); create sequence type_id increment by 1 start with 1; create sequence product_id increment by 1 start with 1; create sequence order_id increment by 1 start with 1; create sequence admin_id increment by 1 start with 1; rem // by maxuan end ///
[Ctrl + A Select all and then copy] description 1: Create association between tables
The product, item, and orders tables are associated through the public domain, usually called the Key Field. There are two types of keys: Primary key) and the external key (Foreign key ). The primary key keeps the data rows in the table unique. in the product Table, product_id is the primary key, and the table orders also contains product_id. in this case, product_id is the external key. The external key of a table obtains information from other tables. Check the preceding SQL statement! Note 2: automatic numbering
There are automatically numbered data types in access, and both MSSQL and MYSQL have auto-increasing data types. when you insert a record, you do not need to operate on this field, and the data value is automatically obtained, oracle does not have an auto-increasing data type. we need to create an auto-increasing serial number. when inserting a record, we need to assign the next value of the serial number to this field. it is foreseeable that this function is available, we can migrate data from ACCESS, MSSQL, or MYSQL to oracle!
Create sequence type_id increment by 1 start with 1;
In this sentence, type_id is the name of the serial number. each time it increases to 1, the starting sequence number is 1. Well, our database has been built and some basic knowledge has been learned from it. this is the end of oracle, part 2 of my J2EE-based shopping website, if you have any questions, please leave a message !!
In the third weblogic article, I will continue to share my painstaking efforts and hope you will get some benefits !! Thank you for your support!
PS: I am so tired that I am catching up with my book !!