Create table [ schema. ] table_name ( ------------方案 、表名
Column_name data_type [dafault expression] ---------字段名、数据类型、或者默认语句
[ [constatraint constraint_name ] constraint_def ] ---------约束名、约束
[,... ] ) ---------------更多的字段
[tablespace tablespace_name ]; ---------------数据存放的表空间,默认表空间为users表空间
2、创建表,并查看表结构:
1》创建学生表:主键student_id
SQL> create table students(
2 student_id number(5)
3 constraint student_pk primary key,
4 name varchar2(15) not null,
5 dob date,
6 sex varchar2(6));
Table created.
SQL> desc students;
Name Null? Type
----------------------------------------- -------- ----------------------------
STUDENT_ID NOT NULL NUMBER(5)
NAME NOT NULL VARCHAR2(15)
DOB DATE
SEX VARCHAR2(6)
2》创建课程表:主键course_id
SQL> create table courses(
2 course_id number(5)
3 constraint course_pk primary key,
4 course_nmae varchar2(30) not null,
5 credit_hour number(2),
6 credit_hours number(3),
7 term varchar2(4));
Table created.
SQL> desc courses;
Name Null? Type
----------------------------------------- -------- ----------------------------
COURSE_ID NOT NULL NUMBER(5)
COURSE_NMAE NOT NULL VARCHAR2(30)
CREDIT_HOUR NUMBER(2)
CREDIT_HOURS NUMBER(3)
TERM VARCHAR2(4)
3》创建选课表:外键 student_id与course_id
SQL> create table s_grade(
2 student_id number(5)
3 constraint grade_fk_students
4 references students(student_id),
5 course_id number(5)
6 constraint grade_fk_courses
7 references courses(course_id),
8 score number(4,1));
Table created.
SQL> desc s_grade;
Name Null? Type
----------------------------------------- -------- ----------------------------
STUDENT_ID NUMBER(5)
COURSE_ID NUMBER(5)
SCORE NUMBER(4,1)
4》创建简单的表:没有约束
SQL> create table total(
2 t1 number(4),
3 t2 number(4));
Table created.
SQL> desc total;
Name Null? Type
----------------------------------------- -------- ----------------------------
T1 NUMBER(4)
T2 NUMBER(4)
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31392094/viewspace-2125878/ ,如需转载,请注明出处,否则将追究法律责任。