14
By R.N. Sangwan-www.rnsangwan.com To Learn or Teach visit www.theskillpedia.com Visit www.theskillpedia.com to learn or teach. By: www.rnsangwan.com 1

Sap ase learning session 7 creating and using databases

Embed Size (px)

Citation preview

Page 1: Sap ase learning session 7 creating and using databases

By R.N. Sangwan-www.rnsangwan.com

To Learn or Teach visit www.theskillpedia.com

Visit www.theskillpedia.com to learn or teach. By: www.rnsangwan.com 1

Page 2: Sap ase learning session 7 creating and using databases

• The Command used to create a database is :Create database

Syntaxcreate [inmemory] [temporary] database database_name

[use database_name as template]

[on {default | database_device} [= size]

[, database_device [= size]]...]

[log on database_device [= size]

[, database_device [= size]]...]

[with {dbid = number, default_location = "pathname", override}]

| [[,]durability = { no_recovery

| at_shutdown

| full} ]

}...

[for {load | proxy_update}]

Visit www.theskillpedia.com to learn or teach. By: www.rnsangwan.com 2

Page 3: Sap ase learning session 7 creating and using databases

• Temporary - indicates that you are creating a temporary database.

• Inmemory - required for in-memory databases.

• database_name - is the name of the new database.

• On - indicates a location and size for the database.

• Default - indicates that create database can put the new database on any default database devices, as

shown in sysdevices.status.

To specify a size for the database without specifying a location, use:

on default = size

To change a database device’s status to “default,” use

sp_diskdefault.

• database_device- is the logical name of the device on which to locate the database. A database can

occupy different amounts of space on each of several database devices.

• Size - is the amount of space to allocate to the database extension.

• log on - specifies the logical name of the device for the database logs.

Visit www.theskillpedia.com to learn or teach. By: www.rnsangwan.com 3

Page 4: Sap ase learning session 7 creating and using databases

• Adaptive Server can manage as many as 32,767 databases.

• The dbid should always be greater than zero and less than the

maximum dbid of 32,767.

• Adaptive Server can create only one database at a time.

• If two database creation requests collide, one user sees this message:

model database in use: cannot create new database

Visit www.theskillpedia.com to learn or teach. By: www.rnsangwan.com 4

Page 5: Sap ase learning session 7 creating and using databases

• To get a report on a database, execute sp_helpdb.

• For a report on the space used in a database, use sp_spaceused

Visit www.theskillpedia.com to learn or teach. By: www.rnsangwan.com 5

Page 6: Sap ase learning session 7 creating and using databases

• Creates a database named pubs10:• create database pubs10

• Creates a 4MB database named pubs11:• create database pubs11

• on default = 4

If you do not provide a unit specifier for size, the value provided for pubs is

presumed to be in megabytes.

Visit www.theskillpedia.com to learn or teach. By: www.rnsangwan.com 6

Page 7: Sap ase learning session 7 creating and using databases

• Use sp_changedbowner to change the ownership of a database. System administrators

create the user databases, then give ownership to another user.

• Sybase suggests that you transfer ownership before the user has been added to the

database, and before the user has begun creating objects in the database.

• The new owner must already have a login name(User) on Adaptive Server, but cannot be a

user of the database, or have an alias in the database.

• You may have to use sp_dropuser or sp_dropalias before you can change a database’s

ownership, and you may have to drop objects before you can drop the user.

The syntax is:

sp_changedbowner loginame [, true ]

• This example makes “albert” the owner of the current database and drops aliases of users

who could act as the old “dbo:”

sp_changedbowner albert

Visit www.theskillpedia.com to learn or teach. By: www.rnsangwan.com 7

Page 8: Sap ase learning session 7 creating and using databases

• Changes the owner of a user database.

• Syntaxsp_changedbowner loginame [, true ]

• Loginame is the login name of the new owner of the current database.

• True transfers aliases and their permissions to the new database owner. Values are “true” and

“TRUE”.

Visit www.theskillpedia.com to learn or teach. By: www.rnsangwan.com 8

Page 9: Sap ase learning session 7 creating and using databases

• Makes the user “albert” the owner of the current database:sp_changedbowner albert

Visit www.theskillpedia.com to learn or teach. By: www.rnsangwan.com 9

Page 10: Sap ase learning session 7 creating and using databases

• The new owner must not already be known as either a user or alias (that is, the new

owner must not already be listed in sysusers or sysalternates).

• Executing, with the single parameter loginame changes the database ownership

to loginame and drops aliases of users who could act as the old “dbo.”

• After executing, the new owner is known as the Database Owner inside the

database.

• It cannot transfer ownership of the system databases.

• The new owner must already have a login name in Adaptive Server, but

must not have a database user name or alias name in the database. To assign

database ownership to such a user, drop the user name or alias entry before

executing sp_changedbowner.

• A System Administrator must grant Permissions to the Database Owner, since the

user is no longer known inside the database under any other name.

Visit www.theskillpedia.com to learn or teach. By: www.rnsangwan.com 10

Page 11: Sap ase learning session 7 creating and using databases

• First you have to extend the disk spaces allocated by the database server . This is done

with the disk init command. For example :

1>use master

2>go

3> disk init name='<ASE device name>', physname='<path to file in the directory>', size

='xxxxM', directio='true‘

• Next you have to alter the database so the database makes use of the new space

ALTER database PUBS10 on <ASE device name> = 'xxxxM’

Visit www.theskillpedia.com to learn or teach. By: www.rnsangwan.com 11

Page 12: Sap ase learning session 7 creating and using databases

1> sp_helpdevice master

2> go• To expand the master database on the master device by 10MB, simply do the following:

alter database master on master = 10go

Extending database by 1280 pages (10.0 megabytes) on disk master

Visit www.theskillpedia.com to learn or teach. By: www.rnsangwan.com 12

Page 13: Sap ase learning session 7 creating and using databases

• Use the drop database command to remove a database from SQL Server. When you run this

command, it:

• Frees the storage space allocated for the database

• Deletes references to it from the system tables in the master database

• Only the Database Owner can drop a database. You must be in the master database to drop a

database. You cannot drop a database that is open for reading or writing by a user.

Syntax

drop database database_name [, database_name]...

• You can drop more than one database in a single statement.

• For example:

You must drop all the databases drop database newpubs, newdb

• on a database device before you can drop the database device.

• The command to drop a device is sp_dropdevice.

Visit www.theskillpedia.com to learn or teach. By: www.rnsangwan.com 13

Page 14: Sap ase learning session 7 creating and using databases

Visit www.theskillpedia.com to learn or teach. By: www.rnsangwan.com 14