Skip to main content

Create Database and Tabe


Create Database
CREATE DATABASE  -----> syntax
*****you can use the CREATE statement only to create objects onthe local server


CREATE DATABASE
[ON [PRIMARY]
([NAME = <’logical file name’>,]
FILENAME = <’file name’>
[, SIZE = ]
[, MAXSIZE = size in kilobytes, megabytes, gigabytes, or terabytes>]
[, FILEGROWTH = ])]
[LOG ON
([NAME = <’logical file name’>,]
FILENAME = <’file name’>
[, SIZE = ]
[, MAXSIZE = size in kilobytes, megabytes, gigabytes, or terabytes>]
[, FILEGROWTH = ])]
[ COLLATE ]
[ FOR ATTACH [WITH ]| FOR ATTACH_REBUILD_LOG| WITH DB_CHAINING
ON|OFF | TRUSTWORTHY ON|OFF]
[AS SNAPSHOT OF ]
[;]

CREATE DATABASE Accounting
ON
(NAME = ‘Accounting’,
FILENAME = ‘c:\Program Files\Microsoft SQL Server\
MSSQL.1\mssql\data\AccountingData.mdf’,
SIZE = 10,
MAXSIZE = 50,
FILEGROWTH = 5)
LOG ON
(NAME = ‘AccountingLog’,
FILENAME = ‘c:\Program Files\Microsoft SQL Server\
MSSQL.1\mssql\data\AccountingLog.ldf’,
SIZE = 5MB,
MAXSIZE = 25MB,
FILEGROWTH = 5MB)
GO

Create Table:::::::::::::::::::::

CREATE TABLE [database_name.[owner].]table_name
(
[[DEFAULT ]
|[IDENTITY [(seed, increment) [NOT FOR REPLICATION]]]]
[ROWGUIDCOL]
[COLLATE ]
[NULL|NOT NULL]
[]
|[column_name AS computed_column_expression]
|[]
[,...n]
)
[ON {|DEFAULT}]
[TEXTIMAGE_ON {|DEFAULT}]


USE Accounting
CREATE TABLE Customers
(
CustomerNo int IDENTITY NOT NULL,
CustomerName varchar(30) NOT NULL,
Address1 varchar(30) NOT NULL,
Address2 varchar(30) NOT NULL,
City varchar(20) NOT NULL,
State char(2) NOT NULL,
Zip varchar(10) NOT NULL,
Contact varchar(25) NOT NULL,
Phone char(15) NOT NULL,
FedIDNo varchar(9) NOT NULL,
DateInSystem smalldatetime NOT NULL
)

table name::::::::::::::
--capitalize the first letter and use lowercase for the remaining letters.
--Limit the use of abbreviations.
--When building tables based on other tables (usually called linking or associate tables), you
should include the names of all of the parent tables in your new table name.
--When you have two words in the name, do not use any separators (run the words together)—

Default::::::::::::::
this is the value you want to be used for any rows that are inserted without a user-supplied value for this particular column.

Identity (almost like primary Key Constraints but wid one difference):::::::::::::::::::::::::::::::::
when you make a column an identity column, SQL Server automatically assigns a sequenced number to this column with
every row you insert. The number that SQL Server starts counting from is called the seed value, and the
amount that the value increases or decreases by with each row is called the increment.An identity column must be numeric, and, in practice, it is almost always implemented with an integer
or bigint datatype.
*******an IDENTITY column and a PRIMARY KEY are completely separate notions— in IDENTITY columns you can reset the seed value and count back up through values you’ve used before

Column Constraints::::::::::::::::::::::::::::
they are restrictions and rules that you place on individual columns about the data that can be inserted into that column. (for ex. if a column shd have the month(1 to 12) it should not allow 13 to be inserted.

Computed Columns::::::::::::::::::::::::::::::::::
its value is derived from other columns in the table by executing some .
syntax:
AS
EX: ExtendedPrice AS Price * Quantity

Table Constraints:::::::::::::::::::::::::::::::::::
table-level constraints include PRIMARY and FOREIGN KEY constraints, as well as CHECK constraints.

Creating a Table::::::::::::::::::::::::::::::::::::
Ex:
CREATE TABLE Customers

Comments

Popular posts from this blog

Insufficient access rights to perform the operation. (Exception from HRESULT: 0x80072098)

While accessing the active directory (AD) and authorization manager (AZMAN) , If you get “   Insufficient access rights to perform the operation. (Exception from HRESULT: 0x80072098)  “ message check the    account that is being used to get the LDAP query from AD .  ERROR DETAILS Exception Details:  System.Runtime.InteropServices.COMException: Insufficient access rights to perform the operation. (Exception from HRESULT: 0x80072098) Source Error: Line 154:    'Session("FullName") = System.Security.Principal.WindowsIdentity.GetCurrent.Name.ToString() Line 155: Line 156:    If Not User.IsInRole("Role1") Then Line 157:          Response.Redirect("./Login.aspx") Line 158:    End If  Stack Trace : .... SOLVE IT Steps to do check the app pool rights: Click on the website name that you are having problem with in IIS  In the right panel you will see 'Basic Settings'. Click It. Select the specific pool option and enter the name of the ac

Sql Server database Read_Only / Read_Write

The ALTER DATABASE command allows a database administrator to modify SQL Server databases and their files and filegroups. This includes permitting the changing of database configuration options. Why Read Only ? When you need to ensure that the data is a database is not modified by any users or automated processes, it is useful to set the database into a read-only mode. Once read-only, the data can be read normally but any attempts to create, updated or delete table rows is disallowed. This makes the read-only mode ideal when preparing for data migration, performing data integrity checking or when the data is only required for historical reporting purposes. Make Database Read Only USE  [master] GO ALTER DATABASE  [TESTDB]  SET  READ_ONLY  WITH  NO_WAIT GO Make Database Read/Write USE  [master] GO ALTER DATABASE  [TESTDB]  SET  READ_WRITE  WITH  NO_WAIT GO In case you get the following error message make the database single user: Msg 5070, Level 16, Stat

Do's and Don't SQL

Do's: Writing comments whenever something is not very obvious, as it won’t impact the performance.  (--) for single line  (/*…*/) to mark a section Use proper indentation Use Upper Case for all SQL keywords. SELECT, UPDATE, INSERT, WHERE, INNER JOIN, AND, OR, LIKE. Use BEGIN... END block for multiple statements in conditional code  Use Declare and Set in beginning of Stored procedure Create objects in same database where its relevant table exists otherwise it will reduce network performance. Use PRIMARY key in WHERE condition of UPDATE or DELETE statements as this will avoid error possibilities. If User table references Employee table than the column name used in reference should be UserID where User is table name and ID primary column of User table and UserID is reference column of Employee table. Use select column name instead of select * Use CTE (Common Table Expression); its scope is limited to the next statement in SQL query, instead of temporary tables and der