Create Database, Create Table, Drop Database
Create Database, Create Table, Drop Database
Página 88.
1. In Object Explorer, connect to an instance of the SQL Server Database Engine and then expand that
instance.
2. Right-click Databases, and then select New Database.
3. In New Database, enter a database name.
4. To create the database by accepting all default values, select OK; otherwise, continue with the following
optional steps.
5. To change the owner name, select (...) to select another owner.
6. To change the default values of the primary data and transaction log files, in the Database files grid,
select the appropriate cell and enter the new value. For more information, see Add Data or Log Files to a
Database.
7. To change the collation of the database, select the Options page, and then select a collation from the
list.
8. To change the recovery model, select the Options page and select a recovery model from the list.
9. To change database options, select the Options page, and then modify the database options. For a
description of each option, see ALTER DATABASE SET Options (Transact-SQL).
10. To add a new filegroup, select the Filegroups page. Select Add and then enter the values for the
filegroup.
11. To add an extended property to the database, select the Extended Properties page.
USE master;
GO
CREATE DATABASE Sales
ON
( NAME = Sales_dat,
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\
saledat.mdf',
SIZE = 10,
MAXSIZE = 50,
FILEGROWTH = 5 )
LOG ON
( NAME = Sales_log,
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\
salelog.ldf',
SIZE = 5MB,
MAXSIZE = 25MB,
FILEGROWTH = 5MB );
GO
EXEC sp-databases;
ales;
Sales;
DROP DATABASE
DROP DATABASE Sales;
CREATE TABLE
Página 109.
Domicilio varchar(40),
CHECK (
emp_id IN ('1389', '0736', '0877', '1622', '1756')
OR emp_id LIKE '99[0-9][0-9]')
DROP TABLE
USE Master
GO
DROP TABLE Customers; DROP TABLE [MyDatabase].[dbo].[MyTable2];
GO
ALTER TABLE
ALTER TABLE Customers
ADD Email varchar(255);
-- Add a column with a constraint to enforce that nonnull data is in a valid telephone number format.
column_d VARCHAR(16) NULL CONSTRAINT column_d_chk CHECK
(column_d LIKE '[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]' OR column_d LIKE'([0-9][0-9][0-9]) [0-9][0-9][0-9]-
[0-9][0-9][0-9][0-9]'),
ALTER TABLE dbo.doc_exy ALTER COLUMN column_a DECIMAL (5, 2) ; --Cambiar tipo de datos de una columna
ALTER TABLE dbo.doc_exy ALTER COLUMN col_a varchar(25) ; --Cambiar el tamaño de una columna
ALTER TABLE T3
ALTER COLUMN C2 VARCHAR(50) ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = [CEK1], ENCRYPTION_TYPE = Randomized,
ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256') NULL; --Cifrado de una columna
Primary key
CREATE TABLE Names
(NameID INTEGER PRIMARY KEY,
FirstName VARCHAR (20), LastName VARCHAR (20),
DateOfBirth DATETIME);
Unique
CREATE TABLE Names
(NameID INTEGER, FirstName VARCHAR (20) UNIQUE,
LastName VARCHAR (20), DateOfBirth
DATETIME);
Foreign Key
CREATE TABLE Sales
(SalesID INTEGER, ProductID INTEGER,
Item TEXT REFERENCES Products (Item));
Check
CREATE TABLE SalesHistory
(SaleID int NOT NULL,
Product char (150) NULL,
SaleDate datetime NULL,
SalePrice money NULL CHECK (SalePrice > 4));
Default
CREATE TABLE SalesHistory
(SaleID int NOT NULL,
Product char (150) NULL DEFAULT ‘new product’,
SaleDate datetime NULL DEFAULT (getdate()),
SalePrice money NULL);
Identity
CREATE TABLE SalesHistory
(SaleID int IDENTITY (1,1) NOT NULL PRIMARY KEY,
Product char (150) NULL,
SaleDate datetime NULL,
SalePrice money NULL);
Create table
CREATE TABLE Peliculas
(Id_Pelicula smallint IDENTITY(1,1) PRIMARY KEY NOT NULL,
Nombre varchar(35) NOT NULL,
Director varchar(35),
Genero tinyint DEFAULT(1) NOT NULL,
Alquilada bit DEFAULT(0) NOT NULL)