create database sql create database school mastersys databases




建数据库(create database) sql语句 create database school; 查看master数据库的系统视图sys. databases

建表(create table) 创建“学生选课表”SCT sql语句: use school; create table SCT (ID int not null primary key identity(1, 1), Sno varchar(5) not null, Cno varchar(3) not null, Tno varchar(4) not null, Grade decimal(5, 1) null );

建表(create table) 查看school数据库的系统视图 sys. tables sys. columns sys. indexes

建表(create table) 查看school数据库的系统视图 sys. key_constraints sys. identity_columns sys. objects

添加约束(add constraint) 为表SCT的属性列Sno, Cno, Tno分别添加外键约束 sql语句: use school; alter table SCT add constraint sno_foreignkey foreign key (Sno) references Student(Sno); alter table SCT add constraint cno_foreignkey foreign key (Cno) references Course(Cno); alter table SCT add constraint tno_foreignkey foreign key (Tno) references Teacher(Tno);

添加约束(add constraint) 为表SCT的属性列Sno, Cno, Tno分别添加外键约束 sql语句: use school; alter table SCT add constraint sno_foreignkey foreign key (Sno) references Student(Sno); alter table SCT add constraint cno_foreignkey foreign key (Cno) references Course(Cno); alter table SCT add constraint tno_foreignkey foreign key (Tno) references Teacher(Tno);

添加约束(add constraint) 为表SCT的属性列Sno, Cno, Tno分别添加外键约束 sql语句: use school; alter table SCT add constraint sno_foreignkey foreign key (Sno) references Student(Sno); alter table SCT add constraint cno_foreignkey foreign key (Cno) references Course(Cno); alter table SCT add constraint tno_foreignkey foreign key (Tno) references Teacher(Tno);

添加约束(add constraint) 查看school数据库的系统视图 sys. foreign_keys

删除表 (delete table) 删除表Course 可能的sql语句: use school; drop table Course; 执行结果:

删除表 (delete table) 删除表Course 可能的sql语句: use school; drop table Course; 执行结果: 正确的sql语句: use school; alter table SCT drop constraint cno_foreignkey; drop table Course;

删除数据库(delete database) 删除数据库school 可能的sql语句: drop database school; 执行结果:



sql语句执行流程(伪码) sql语句: CREATE TABLE test (id int , name varchar(10) ); DDL处理流程(DDL解释器)

sql语句执行流程(伪码) DDL处理流程(DDL解释器): interpret(sql) { t_name = extract_table_name(sql); if(validate_table_name(t_name)) { insert_into_dict(sys. objects, t_name); insert_into_dict(sys. tables, t_name); col_name_and_datatype = extract_col_info(sql); while(col_name_and_datatype != NULL) { insert_into_dict(sys. columns, col_name_and_datatype); col_name_and_datatype = extract_col_info(sql); } } }

sql语句执行流程(伪码) DDL处理流程(DDL解释器): interpret(sql) { t_name = extract_table_name(sql); if(validate_table_name(t_name)) { insert_into_dict(sys. objects, t_name); insert_into_dict(sys. tables, t_name); col_name_and_datatype = extract_col_info(sql); while(col_name_and_datatype != NULL) { insert_into_dict(sys. columns, col_name_and_datatype); col_name_and_datatype = extract_col_info(sql); } } }

sql语句执行流程(伪码) DDL处理流程(DDL解释器): interpret(sql) { t_name = extract_table_name(sql); if(validate_table_name(t_name)) { insert_into_dict(sys. objects, t_name); insert_into_dict(sys. tables, t_name); col_name_and_datatype = extract_col_info(sql); while(col_name_and_datatype != NULL) { insert_into_dict(sys. columns, col_name_and_datatype); col_name_and_datatype = extract_col_info(sql); } } }

sql语句执行流程(伪码) DDL处理流程(DDL解释器): interpret(sql) { t_name = extract_table_name(sql); if(validate_table_name(t_name)) { insert_into_dict(sys. objects, t_name); insert_into_dict(sys. tables, t_name); col_name_and_datatype = extract_col_info(sql); while(col_name_and_datatype != NULL) { insert_into_dict(sys. columns, col_name_and_datatype); col_name_and_datatype = extract_col_info(sql); } } }


THANK YOU !
- Slides: 23