Home > Database > SQL > body text

Xiaobai's second level of learning Oracle: Creation of the first Oracle database table

little bottle
Release: 2019-05-24 15:16:38
Original
4508 people have browsed it

Nowadays, in actual work, creating tables in the database is often used. In this article, the editor mainly shares with you how to create tables in the database through SQL statements. First, use plsql to connect to the oracle database and ensure that the following services are enabled.

Xiaobais second level of learning Oracle: Creation of the first Oracle database table

Our requirements for creating a table this time are: create a class table, and a Zhang student table.

1. First, the class table serves as the main table, which is the so-called primary key. In the main table, the constraints we use here are primarykey and not null


create table classinfo(
       classid number(2) primary key,
       classname varchar(10) not null       
       );
Copy after login

sql analysis:

--create table The keyword to create the table

--classinfo is the name of the created table

--classid is the class table The id data type is number(2). We give it 2 lengths by default. We set the class id as the primary key to facilitate other foreign key associations.

--classname is the class name data type It is character type varchar(10). We have given a default length of 10 characters. The constraint of the class name is that it cannot be empty

Execute the sql statement:

# The classinfo table was created successfully.

2. Then we create a foreign key, that is A table associated with the primary key. Please see the following SQL statement for the data types and constraints used.


create table studentinfo(
       studentid number(2) primary key,
       studentname varchar(10) not null,
       studentsex char(2) check(studentsex='男' or studentsex='女'),
       studentage number(2) not null,
       studenttel number(11) unique,
       studentaddress varchar(50) default '上海',
       classid number(2) references classinfo(classid)
       );
Copy after login

SQL statement analysis:

--create table keyword to create a table

--studentinfo(); is the table name used to create the student information table

--studentid (student id) The constraint is the primary key primary key

--studentname (student name) The constraint is not null

--studentsex (student gender) The constraint is check

--studentage (student age) The constraint is not null

--studenttel (student phone number) The constraint is unique

- -studentaddress (student address) are the column names in the student table.

The student table studentinfo is created.

The complete sql statement is as follows:


create table classinfo(
       classid number(2) primary key,
       classname varchar(10) not null       
       );
       
create table studentinfo(
       studentid number(2) primary key,
       studentname varchar(10) not null,
       studentsex char(2) check(studentsex='男' or studentsex='女'),
       studentage number(2) not null,
       studenttel number(11) unique,
       studentaddress varchar(50) default '上海',
       classid number(2) references classinfo(classid)
       );
Copy after login

At this point, the class table and student table we created have been demonstrated. Isn’t it very simple?

[Recommended course: Oracle Video Tutorial]

The above is the detailed content of Xiaobai's second level of learning Oracle: Creation of the first Oracle database table. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template