Okay in this chapter we will discuss how to change table structure using sql command. fisrt open your SQL Sever. In this case I will use sql server 2005, Choose root of database->Choose Tools menu and click at new query. after that query analyzer will display. Query Analyzer is place to write sql command.
For Example I will create some table:
Create table dataStudent(
NRP varchar(100), Name varchar(100)
)
Create table dataSubject() {
Number varchar(10) primary key not null, Name_Subject varchar(100), NRP varchar(100)
}
How to Add Column at Table(I will add column Address at dataStudent)
Format:Alter table [Table Name] add [Add Column] [Data Type]
Example: Alter table dataStudent add Address char(100)
How to change column data type at table (I will change data type column Address at dataStudent)
Format:Alter table [Table Name] alter column [Add Column] [Data Type]
Example:Alter table dataStudent alter column Adress varchar(100)
How to delete column at table (I will delete column Adress at dataStudent)
Format:Alter table [Table Name] drop column [Column Name]
Example:Alter table dataStudent drop column Address
How to add constraint primary key (I will add primary key to NRP column)
1. If you want make column as primary key you must add not null at column.
Example :Alter table dataStudent alter column NRP varchar(100) not null
2. Add Constraint to column
Format: Alter table [Table Name] add contraint [Name Primary Key] primary key [Column Name]
Example:Alter table dataStudent add contraint pk_Student primary key(NRP)
How to add contraint foreign key at column in Table(I will make foreign key between table dataSubject->dataStudent(Primary Key))
Format:Alter table[table candidate foreign key] add constraint [Contraint Name]([Column Name]) reference [table primary key](Column Name)
Example:Alter table dataSubject add contraint fk_Subject_ref_Student foreign key (NRP) reference dataStudent(NRP)
How to delete constraint (I will delete fk_subject_ref_Student)
Format:Alter table [Table Name] drop contraint [Constraint Name]
Example:Alter table dataSubject drop contraint fk_subject_ref_Student
How to drop table (I will drop table dataSubject)
Format:Drop table [Table Name]
Example:Drop table dataSubject
Okay that's is it. Congratulation you have learn a little sql command. I hope this tutorial can help you. Thank a lot..^^
No comments:
Post a Comment