Sei sulla pagina 1di 4

CS5483 Data Warehousing and Data Mining

Exercise 1: Database Practice: Microsoft SQL Server


Accessing MS SQL Server
1. 2. 3. 4. Login Windows XP by entering your CS LAB account and password Click Start => All Programs and select Microsoft SQL Server Click the Query Analyzer item Under Connect to SQL Server window, under SQL Server: type / select w2ksc from the browse button, under Use SQL Server Authentication, Login name: text box, type your group number, for example grp## (## is your group number, e.g. grp01), under Password text box, type abc123, click the OK button to connect to the server Under the grp##_db Database, type different commands within the query area, then the result will be shown in Result area. If you want to change your password, you may type this statement in Query area: sp_password old_password, new_password For example sp_password 'abc123', 'new123' Click the Play button Reminder: You have the sole responsibility of keeping and remembering your new password After every execution of the statements, you should clear the previous statement(s) 7. Type then execute the following statement: create table course ( course_no integer not null, primary key(course_no) ) 8. Type then execute the statement create table person ( id_no integer not null, name varchar(50), birth_date datetime, height float, weight float, primary key(id_no) ) 9. Type then execute create table student ( id_no integer not null, student_id integer not null, dept_name varchar(100), course_taken integer, primary key(id_no) ) 10. Type then execute Page 1 of 4

5. 6.

create table ft_student ( id_no integer not null, grade integer, primary key(id_no) ) 11. Type then execute create table pt_student ( id_no integer not null, primary key(id_no) ) 12. Type then execute alter table course add course_name varchar (50) null 13. Type then execute alter table course add credit integer null 14. Type then execute alter table course add teacher_name varchar (50) null 15. Type then execute create table previous_course_required ( course_no integer not null, previous_course_no varchar(100) not null, primary key (previous_course_no), foreign key (course_no) references course(course_no) ) 16. Type then execute create table students_registered ( course_no integer not null, id_no integer not null, primary key (course_no, id_no), foreign key (course_no) references course(course_no), foreign key (id_no) references student(id_no) ) 17. Type then execute insert into person (id_no, name, birth_date, height, weight) values (1, 'John Doe', 'mar 1, 1997 0:0:0.0', 1.11, 1.11) 18. Type then execute insert into person (id_no, name, birth_date, height, weight) values (2, 'Able Chitson', 'mar 2, 1997', 2.22, 2.22) 19. Type then execute Page 2 of 4

insert into person (id_no, name, birth_date, height, weight) values (3, 'Chris Bloor', 'mar 3, 1997', 3.33, 3.33) 20. Type then execute insert into student (id_no, student_id, dept_name, course_taken) values (3, 3, 'Computer Science', 4) 21. Type then execute insert into course (course_no, course_name, credit, teacher_name) values (4, 'Database', 3, 'Peter Smith') 22. Type then execute insert into course (course_no, course_name, credit, teacher_name) values (5, 'Data Engineering', 5, 'Mike Denny') 23. Type then execute insert into person (id_no, name, birth_date, height, weight) values (5, 'Keith Reagon', 'mar 5, 1997', 5.55, 5.55) 24. Type then execute insert into student (id_no, student_id, dept_name, course_taken) values (5, 5, 'Computer Science', 0) 25. Type then execute insert into ft_student (id_no, grade) values (5, 5) 26. Type then execute insert into previous_course_required (course_no, previous_course_no) values (4, 'CS101') 27. Type then execute insert into previous_course_required (course_no, previous_course_no) values (4, 'CS102') 28. Type then execute insert into students_registered (course_no, id_no) values (4, 3) 29. Type then execute insert into students_registered (course_no, id_no) values (5, 3) 30. Type then execute select * from course 31. Type then execute Page 3 of 4

select * from person 32. Type then execute select * from ft_student 33. Type then execute create proc show_person as select * from person return 34. 28 35. Type then execute show_person Type then execute create proc show_course @incourse_no integer as select * from course where course_no = @incourse_no return 36. 29 Type then execute execute show_course 4

System Tables
In schema translation design, the database schema from source database is extracted using MS SQL server's system tables. System tables are tables used to store information about the system (the database catalog) or an individual database (the database catalog). MS SQL Server system tables used by our schema translation are shown in following table. Metadata of source database is extracted from system tables.

Important System Tables used in Project


Tables Name syscolumns sysindexes Function Contains one row for every column in every table and view, and a row for each parameter in a stored procedure. Contains one row for each each clustered index and one row for each nonclustered index. These indexes are the result of a CREAT INDEX statement or the CREATE TABLE statement with a PRIMARY KEY or UNIQUE constraint. Additionally, sysindexes contains one row for each table that has no clustered index and one row for each table that contains text or image columns. Contains one row for each each database object and temporary object. Contains one row for each FOREIGN KEY constraint.

sysobjects sysreferences

Sample SQL Statements Get table information


To create a tables, we should look at the tables syscolumns , systypes , syscomments and sysobjects. These tables contains the tables name, column name, column type, column length, default value, null value and identity. This SQL statement is order by the object ID and the Column ID (syscolumns.colid). cold is the order of the columns in a table. The first column in a table is 1 and the second is 2 and so on.

Page 4 of 4

Potrebbero piacerti anche