Creating Tables
To create a table you can either execute a SQL query containing the schema for the new table, or you can use the built-in table editor in SSMS.
SQL Query
CREATE TABLE sws.people (
id INT PRIMARY KEY IDENTITY (1, 1),
name_last NCHAR (25) NOT NULL,
name_last NCHAR (15) NOT NULL,
name_middle NCHAR (15),
start_date DATETIME NOT NULL,
phone_home CHAR (10),
phone_mobile CHAR (10),
agency_id INT NOT NULL,
FOREIGN KEY (agency_id) REFERENCES sws.agencies (agancy_id)
);
SSMS Table Editor
- Browse to your database
- Click the ’+’ sign to expand it
- R-click on Tables and choose New > Table
- Add the fields for the table, and define their data types, etc.
- Don’t forget the identity and primary key
- When complete, R-Click the tab of the designer and choose Save Table (name it here)
- Add any additional tables
Step 3
Step 4
Set Identity, will cause SQL Server to increment the counter in this field automatically.
Now Right-Click on the little triangle, and choose “Set Primary Key”
Step 5
Finally, right-click on the tab and choose Save Table. It will ask you to name it at this point.
Back to Class 2 Contents