Creating and Working with Database and Tables Marisol D. Payra SQL stands for Structured Query Language. So, SQL is a language, specifically, it's a language for relational Databases. What is T-SQL and PL/SQL and how is it different SQL You can think of SQL as a standard database language. It was initially developed by IBM and later ANSI (American National Standards Institute) made it a standard. So, SQL is an ANSI standard and based on it different database vendors like Microsoft, Oracle and many other organizations developed their own database query language. What is a relational database and why do we use it WORKING WITH DATABASES Creating, altering, and dropping a database In Part 1 of SQL Server, we have seen, using SSMS to connect to SQL Server. In this part we will learn creating, altering, and dropping a database. A SQL Server database can be created, altered, and dropped 1. Graphically using SQL Server Management Studio (SSMS) or 2. Using a Query To create the database graphically 1. Right Click on Databases folder in the Object explorer 2. Select New Database 3. In the New Database dialog box, enter the Database name and click OK. To Create the database using a query Create database DatabaseName To alter a database, once it is created Alter database DatabaseName Modify Name = NewDatabaseName Alternatively, you can also use system stored procedure Execute sp_renameDB ‘OldDatabaseName', 'NewDatabaseName' To Delete or Drop a database Drop Database Database That You Want To Drop Dropping a database, deletes the LDF and MDF files. Developer Machines 1,2,3 and 4 connects to the database server using SSMS The aim of this article is to create tblPerson and tblGender tables and establish primary key and foreign key constraints. In SQL Server, tables can be created graphically using SQL Server Management Studio (SSMS) or using a query. You Generated 2 files which is .MDF Files – data File ( Contain actual Data) .LDF Files - Transaction Log files (used to recover Database) The following statement creates tblGender table, with ID and Gender columns. The following statement creates tblGender table, with ID and Gender columns. ID column is the primary key column. The primary key is used to uniquely identify each row in a table. Primary key does not allow nulls. Create Table tblGender ( ID int Not Null Primary Key, Gender nvarchar(50) ) In tblPerson table, GenderID is the foreign key referencing ID column in tblGender table. Foreign key references can be added graphically using SSMS or using a query. To graphically add a foreign key reference 1. Right click tblPerson table and select Design 2. In the table design window, right click on GenderId column and select Relationships 3. In the Foreign Key Relationships window, click Add button 4. Now expand, in Tables and Column Specification row, by clicking the, + sign 5. Click on the elipses button, that is present in Tables and Column Specification row 6. From the Primary Key Table, dropdown list, select tblGender 7. Click on the row below, and select ID column 8. From the column on the right-hand side, select GenderId 9. Click OK and then click close. 10. Finally save the table. To create tblPerson table, graphically, using SQL Server Management Studio 1. Right click on Tables folder in Object explorer window 2. Select New Table 3. Fill Column Name, Data Type and Allow Nulls, as shown below and save the table as tblPerson. To add a foreign key reference using a query Alter table tblPerson add constraint tblPerson_GenderId_FK FOREIGN KEY (GenderId) references tblGender (ID) The general formula is here Alter table Foreign Key Table add constraint ForeignKeyTable_ForiegnKeyColumn_FK FOREIGN KEY(ForiegnKeyColumn) references PrimaryKey Table (PrimaryKeyColumn) FOREIGN KEYS used to enforce database integrity. In layman's terms, A foreign key in one table points to a primary key in another table. The foreign key constraint prevents invalid data form being inserted into the foreign key column. The values that you enter the foreign key column, must be one of the values contained in the table it points to. SELECT Select * from TblPerson Select * from TblGender GROUP ACTIVITY Each member of the Group will create a database that is relation to all other databases. For each dabatases they will created tables and Column. Assign Primary and Foreign key using designer query. Use Create, Alter Select and Insert into in creating databases. And prepare for our next lesson which is DEFAULT CONSTRAINT.