non-Unicode, 16 bytes pointer on row, preferred over text data type 0-2 GB varcharmax non-Unicode 0-8000 varchar[n] non-Unicode 0-8000 char[n] Comments Bytes Name... Example 1CREATE TABL
Trang 1Lab 01b
Trang 3Data types (1)
n Integer
0 to 255 1
tinyint
-215 (-32,768) to 215 - 1 (32,767) 2
smallint
-231 (-2,147,483,648) to
231 - 1 (2,147,483,647)
4 int
-263 (-9,223,372,036,854,775,808) to
263 - 1 (9,223,372,036,854,775,807)
8 bigint
Range Bytes
Name
Trang 4Data types (2)
n Exact numeric
- 1038 +1 to 1038 - 1
5 – 17 numeric[p[,s]]
- 1038 +1 to 1038 - 1
5 – 17 decimal[p[,s]]
Range Bytes
Name
p (precision)
The maximum total number of decimal digits that can be stored, both to the left and
to the right of the decimal point The precision must be a value from 1 through the maximum precision of 38 The default precision is 18.
s (scale)
Trang 5- 1.79E+308 to -2.23E-308,
0 and 2.23E-308 to 1.79E+308
n Float[(n)]
Range Bytes
Name
Trang 6Data types (4)
n Monetary
- 214,748.3648 to 214,748.3647
4 smallmoney
-922,337,203,685,477.5808 to 922,337,203,685,477.5807
8 Money
Range Bytes
Name
Trang 7Data types (5)
n Date and Time
January 1, 1900, to June 6, 2079
4 smalldatetime
January 1, 1753, to December 31, 9999
8 datetime
Range Bytes
Name
Trang 8non-Unicode, 16 bytes pointer on row, preferred over text data type
0-2 GB varchar(max)
non-Unicode 0-8000
varchar[(n)]
non-Unicode 0-8000
char[(n)]
Comments Bytes
Name
Trang 916 bytes pointer or in row, preferred over ntext data type
0-2 GB nvarchar(max)
max 4000 unicode characters 0-8000
Name
Trang 100-8000 varbinary[(n)]
0-8000 binary[(n)]
Comments Bytes
Name
Trang 11Comments Bytes
Name
n Global identifier
16 uniqueidentifier
Comments Bytes
Name
16 bytes pointer 0-2GB
xml
Comments Bytes
Name
Trang 12Data types (10)
n Special
0-8016 sql_variant
table
-256 sysname
one column per table 8
timestamp
0-8 cursor
1 byte for every 8 bit columns 1
bit
Range Bytes
Name
Trang 14n Create table
n Alter table
n Drop table
Trang 15[ NULL | NOT NULL ]
[ [ CONSTRAINT constraint_name ] DEFAULT constant_expression ]
| [ IDENTITY [ ( seed ,increment ) ] [ NOT FOR REPLICATION ] ]
Trang 16Create Table (1)
< column_constraint > ::=
[ CONSTRAINT constraint_name ]
{ { PRIMARY KEY | UNIQUE } [ CLUSTERED | NONCLUSTERED ] |
[ FOREIGN KEY ] REFERENCES referenced_table_name [ ( ref_column ) ]
[ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ] [ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
| CHECK ( logical_expression )
}
Trang 17Create table (3)
< table_constraint > ::=
[ CONSTRAINT constraint_name ]
{ { PRIMARY KEY | UNIQUE } [ CLUSTERED | NONCLUSTERED ]
(column [ ASC | DESC ] [ , n ] )
| FOREIGN KEY ( column [ , n ] )
REFERENCES referenced_table_name [ ( ref_column [ , n ] ) ]
[ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ] [ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
| CHECK ( logical_expression )
}
Trang 18Example (1)
CREATE TABLE Department
(
DNumber numeric(4,0) Constraint pk_dep Primary Key,
DName varchar(15) not null,
MgrSSN char(9) Constraint fk_dep Foreign Key References
Employee(SSN),
MgrStartdate datetime,
);
Trang 19Example (2)
Or:
CREATE TABLE Department
(
DNumber numeric(4,0) not null,
DName varchar(15) not null,
MgrSSN char(9),
MgrStartdate datetime,
Constraint pk_dep Primary Key (DNumber),
Constraint fk_dep Foreign Key (MgrSSN) References Employee(SSN)
);
Trang 20Alter Table
ALTER TABLE table_name
{ ALTER COLUMN column_name
{ type_name [ ( { precision [ , scale ] } ) ]
[ NULL | NOT NULL ] | {ADD | DROP }
Trang 21Example (1)
Column
ALTER TABLE Department ADD column_b
VARCHAR(20) NULL ;
ALTER TABLE Department DROP column_b;
ALTER TABLE Department ALTER COLUMN
Trang 22Example (2)
Table
n Primary key
ALTER TABLE Department
ADD Constraint pk_dept PRIMARY KEY (DNumber);
n Foreign key
ALTER TABLE Department
ADD Constraint fk_dept Foreign Key (MgrSSN) References
Employee(SSN);
Drop Constraint
ALTER TABLE Department
Trang 24n Insert
n Delete
Trang 25Insert into table_name [(column_list)]
Values (value_list)
Example
INSERT INTO Department
VALUES (5, 'Research', '333445555', '22-MAY-78');
Or
INSERT INTO Department (DNumber, DName)
VALUES (5, 'Research');
Trang 28n Create Database company (13th slide)
n Insert Data
Trang 29Practice: Create table (1)
n Department
CREATE TABLE Department(
DName varchar(15) NOT NULL, DNumber numeric(4, 0) NOT NULL, MgrSsn char(9) NULL,
MgrStartdate datetime NULL) GO
Trang 30Practice: Create table (2)
n Employee
CREATE TABLE Employee(
FName varchar(15) NOT NULL, MInit varchar(1) NULL,
LName varchar(15) NOT NULL, SSN char(9) NOT NULL,
BDate datetime NULL, Address varchar(30) NULL, Sex char(1) NULL,
Salary numeric(10, 2) NULL,
Trang 31Practice: Create Primary Key
ALTER TABLE Department
ADD Constraint pk_Dept PRIMARY KEY (DNumber) GO
ALTER TABLE Employee
ADD Constraint pk_Emp PRIMARY KEY (SSN) GO
Trang 32Practice: Create Foreign Key
n Department
ALTER TABLE Department
ADD constraint fk_DeptMgrssn FOREIGN KEY(Mgrssn) REFERENCES Employee(SSN)
GO
n Employee
ALTER TABLE Employee
ADD Constraint fk_EmpDNo FOREIGN KEY(DNo) REFERENCES Department(DNumber)
GO
Trang 33Practice: Insert Data (1)
n Department
INSERT INTO Department
VALUES ('Research', 5, Null, '22-MAY-1978') GO
INSERT INTO Department
VALUES ('Administration', 4, Null, '01-JAN-1985') GO
INSERT INTO Department
VALUES ('Headquarters', 1, Null, '19-JUN-1971') GO
Trang 34Practice: Insert Data (2)
n Employee
INSERT INTO Employee
VALUES ('James', 'E', 'Borg', '888665555', '10-NOV-1927', 'Houston,TX', 'M', 55000, null, 1)
GO
INSERT INTO Employee
VALUES ('Franklin', 'T', 'Wong', '333445555', '08-DEC-1945', 'Houston,TX', 'M', 40000, '888665555', null, 5)
GO
INSERT INTO Employee
VALUES ('Jennifer', 'S', 'Wallace', '987654321', '20-JUN-1931', 'Bellaire,TX', 'F', 43000, '888665555', null, 4)
Trang 35Practice: Insert Data (3)
n Employee
INSERT INTO Employee
VALUES ('John', 'B', 'Smith', '123456789', '09-Jan-1955', 'Houston,TX', 'M', 30000, '333445555', 5)
GO
INSERT INTO Employee
VALUES ('Alicia', 'J', 'Zelaya', '999887777', '19-JUL-1958', 'Spring,TX', 'F', 25000, '987654321', 4)
GO
INSERT INTO Employee
VALUES ('Ramesh', 'K', 'Narayan', '666884444', '15-SEP-1952', 'Humble,TX', 'M', 38000, '333445555', 5)
GO
INSERT INTO Employee
VALUES ('Joyce', 'A', 'English', '453453453', '31-JUL-1962', 'Houston, TX', 'F', 25000, '333445555', 5)
GO
INSERT INTO Employee
VALUES ('Ahmad', 'V', 'Jabbar', '987987987', '29-MAR-1959', 'Houston,TX', 'M', 25000, '987654321', 4)
GO
Trang 36Practice: Update date
UPDATE Department
SET MgrSsn = '333445555' WHERE DNumber = 5
GO
UPDATE Employee
SET MgrSsn = '987654321' WHERE DNumber = 4
Trang 37Practice: Query Data
Trang 38Practice: Delete Table
n Delete Foreign Key:
ALTER TABLE Employee Drop Constraint FK_EmpDNo
Trang 39Practice: Delete Table (2)