SQL Database Basic Commands
DATABASECreating a Database IntroductionTo create a database in SQL, use the following formula:CREATE DATABASE DatabaseNameHere is an example:CREATE DATABASE BethesdaCarRental;If you want the name of the database to be in different words, include them in square brackets. Here is an example:CREATE DATABASE [Bethesda Car Rental];If you want the name of the database to be in different words, include them in square brackets.To start from a sample code, open an empty Query window and display the Template Explorer. From the Template Explorer, expand Database. Drag Create Database and drop it in the Query window:-- =============================================-- Create database template-- =============================================USE masterGO-- Drop the database if it already existsIF EXISTS (SELECT name FROM sys.databasesWHERE name = N'')CREATE DATABASE GOTo visually create a database, open Microsoft SQL Server Management Studio. In the Object Explorer, expand the server name followed by the Databases node. Right-click Databases and click New Database...In the Name text box, type the desired name of the new database. Here is an example:Then specify the other properties of the new database:Deleting a Database IntroductionTo delete a database, you use the DROP DATABASE expression followed by the name of the database. The formula used is:DROP DATABASE DatabaseName;Here is an example: DROP DATABASE RealEstate1;GOTo start from a sample code, open an empty Query window and display the Template Explorer. From the Template Explorer, expand Database. Drag the Drop Database node and drop it in the Query window:-- =========================-- Drop Database Template-- =========================USE masterGOIF EXISTS (SELECT name FROM sys.databasesWHERE name = N'')DROP DATABASE GOTo delete a database in SQL Server Management Studio, in the Object Explorer, expand the Databases node, right-click the undesired database, and click Delete. A dialog box would prompt you to confirm your intentionIf you still want to delete the database, you can click OK. If you change your mind, you can click Cancel.Renaming a Database DescriptionTo change the name of a database, Transact-SQL provides sp_renamedb. The formula used would be:EXEC sp_renamedb 'ExistingName', 'NewName'The EXEC sp_renamedb expression is required.The ExistingName factor is the name of the database that you want to rename.The NewName factor is the name you want the database to have after renaming it.Here is an example of renaming a database:EXEC sp_renamedb 'RentalCars', 'BethesdaCarRentalGOTABLESTransact-SQL: Creating a Table IntroductionTo create a table, you can follow this formula:CREATE TABLE Country(Column1, Column2, Column3)or:CREATE TABLE Country(Column1,Column2,Column3);Each column is created as:ColumnNameDataTypeOptionsHere is an example: CREATE TABLE Customers (DrvLicNbrnvarchar(32), DateIssued DATE,DateExpired date,FullNamenvarchar(50),Address NVARCHAR(120),City NvarChar(40),State NVarChar(50),PostalCodenvarchar(20),HomePhonenvarchar(20),OrganDonor BIT);GOTo start from a sample code, open an empty Query window and display the Template Explorer. From the Template Explorer, expand Table. Drag Create Table and drop it in the Query window: -- =========================================-- Create table template-- =========================================USE GOIF OBJECT_ID('.', 'U') IS NOT NULL DROP TABLE .GOCREATE TABLE .(, , , CONSTRAINT PRIMARY KEY ())GOYou can then modify/customize this code.To visually create a table, in the Object Explorer, expand the the database and expand its Tables node. Right-click the Tables node and click New Table... Enter a name for each column and select its data type:Deleting a Table IntroductionTo delete a table using SQL, use the following formula: DROP TABLE TableNameThe DROP TABLE expression is required and it is followed by the name of the undesired table. Here is an example:DROP TABLE Students;GORenaming a Table IntroductionTo rename a table using code, execute the sp_rename stored procedure using the following formula:sp_renameExistingTableName, TableNewName;Here is an example:sp_rename 'StaffMembers', 'Employees';GOIn this case, the interpreter would look for a table named StaffMembers in the current or selected database. If it finds it, it would rename it Employees. If the table doesn't exist, you would receive an error.
Description
Learn how to start with Database.
What is SQL?
* SQL stands for Structured Query Language
* SQL lets you access and manipulate databases
* SQL is an ANSI (American National Standards Institute) standard
What Can SQL do?
* SQL can execute queries against a database
* SQL can retrieve data from a database
* SQL can insert records in a database
* SQL can update records in a database
* SQL can delete records from a database
* SQL can create new databases
* SQL can create new tables in a database
* SQL can create stored procedures in a database
* SQL can create views in a database
* SQL can set permissions on tables, procedures, and views
Presentation Transcript
Your Facebook Friends on WizIQ