Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

EJEMPLO DE CREACIÓN DE BASE DE DATOS:

CREATE DATABASE bd_Music;

USE master;
GO
IF DB_ID ( N'bd_Music' ) IS NOT NULL
DROP DATABASE Music;
GO
CREATE DATABASE Music;
GO

EJEMPLO DE CREACIÓN DE TABLAS:

EJEMPLO CREACIÓN DE PRIMARY KEY – FOREING KEY

EJEMPLO CREACIÓN DE CONSTRAINTS


USE bd_Music;
if object_id ('Albums') is not null
drop table Albums;
if object_id ('Artists') is not null
drop table Artists;
if object_id ('Artists1') is not null
drop table Artists1;
if object_id ('Genres') is not null
drop table Genres;
if object_id ('Individual') is not null
drop table Individual;
if object_id ('Products') is not null
drop table Products;
GO

CREATE TABLE Artists (


ArtistId int IDENTITY(1,1) NOT NULL ,
ArtistName nvarchar(255) NOT NULL default 'Ana',
ActiveFrom date
CONSTRAINT ClP_Restriccion PRIMARY KEY(ArtistId),
CONSTRAINT ClP_Restriccion1 UNIQUE(ActiveFrom)
);
GO

CREATE TABLE Artists1 (


ArtistId int IDENTITY(1,1) NOT NULL ,
ArtistName nvarchar(255) NOT NULL default 'Ana',
ActiveFrom date
PRIMARY KEY(ArtistId),
UNIQUE(ActiveFrom)
);

GO

CREATE TABLE Albums (


AlbumId int IDENTITY(1,1) NOT NULL PRIMARY KEY,
AlbumName nvarchar(255) NOT NULL,
ReleaseDate date NOT NULL,
ArtistId int NOT NULL,
GenreId int NOT NULL
CONSTRAINT FK_Albums_Artists FOREIGN KEY (ArtistId)
REFERENCES dbo.Artists (ArtistId)
ON DELETE CASCADE /*No se puede poner Set Null*/
ON UPDATE NO ACTION /*Lo mismo que Restrict*/
);

CREATE TABLE Genres (


GenreId int IDENTITY(1,1) NOT NULL PRIMARY KEY,
Genre nvarchar(50) NOT NULL UNIQUE NONCLUSTERED
);

CREATE TABLE Individual (


IndividualId int IDENTITY(1,1) NOT NULL PRIMARY KEY,
IndividualName nvarchar(255) NOT NULL,
Salary money CHECK (Salary >= 25000 AND Salary <= 150000)
);

GO

CREATE TABLE products(


product_id INT IDENTITY PRIMARY KEY,
product_name VARCHAR(255) NOT NULL,
unit_price DEC(10,2) CHECK(unit_price > 0),
discounted_price DEC(10,2) CHECK(discounted_price > 0),
CHECK(discounted_price < unit_price)
);

EXEC sp_help 'products';

ALTER TABLE products


DROP CONSTRAINT positive_price;

/*
ALTER TABLE table_name
NOCHECK CONSTRAINT constraint_name;
*/

SELECT *
FRoM sys.key_constraints
WHERE type = 'PK';

Drop table products;

TRUNCATE TABLE products;

DROP TABLE IF EXISTS products;

DELETE FROM dbo.Albums

ALTER TABLE
if object_id('libros') is not null
drop table libros;

create table libros(


titulo varchar(30),
editorial varchar(15),
edicion datetime,
precio decimal(6,2)
);

go

insert into libros (titulo,editorial,precio)


values ('El aleph','Emece',25.50);

-- Agregamos el campo "cantidad" a la tabla "libros", de tipo tinyint,


-- que acepta valores nulos:
alter table libros
add cantidad tinyint;

exec sp_columns libros;

-- Agregamos un campo "codigo" a la tabla "libros", de tipo int con el


-- atributo "identity":
alter table libros
add codigo int identity;

-- Intentamos agregar un campo llamado "autor" de tipo varchar(30)


-- que NO acepte valores nulos (genera error):
alter table libros
add autor varchar(30) not null;

-- Agregar un campo llamado "autor" de tipo varchar(20) pero con


-- un valor por defecto:
alter table libros
add autor varchar(20) not null default 'Desconocido';

-- Eliminamos el campo "precio" de la tabla "libros":


alter table libros
drop column precio;

exec sp_columns libros;

-- Intentamos eliminar un campo con restricciones (genera error) :


alter table libros
drop column autor;

-- Eliminamos varios campos en una sola sentencia:


alter table libros
drop column editorial,edicion;

CREACIÓN DE REGLAS Y DEFAULT

if object_id ('clientes') is not null


drop table clientes;

if object_id ('VP_legajo_patron') is not null


drop default VP_legajo_patron;
if object_id ('RG_legajo_patron') is not null
drop rule RG_legajo_patron;
if object_id ('RG_legajo') is not null
drop rule RG_legajo;
if object_id ('VP_datodesconocido') is not null
drop default VP_datodesconocido;
if object_id ('VP_fechaactual') is not null
drop default VP_fechaactual;

create table clientes(


legajo char(4),
nombre varchar(30),
domicilio varchar(30),
ciudad varchar(15),
provincia varchar(20) default 'Cordoba',
fechaingreso datetime
);
GO
create rule RG_legajo_patron
as @valor like '[A-Z][A-Z][0-9][0-9]';
GO

exec sp_bindrule RG_legajo_patron,'clientes.legajo';


GO
create default VP_legajo_patron
as 'AA00';
GO
exec sp_bindefault VP_legajo_patron,'clientes.legajo';

GO
create default VP_datodesconocido as '??';
GO
exec sp_bindefault VP_datodesconocido,'clientes.domicilio';

exec sp_bindefault VP_datodesconocido,'clientes.ciudad';

/*Inserta valores a la tabla cliente*/


insert into clientes values('GF12','Ana Perez',default,default,'Cordoba','2001-
10-10');
select * from clientes;

exec sp_bindefault VP_datodesconocido,'clientes.provincia';


GO
create default VP_fechaactual
as getdate();
GO
exec sp_bindefault VP_fechaactual,'clientes.fechaingreso';

insert into clientes default values;


select * from clientes;

exec sp_bindefault VP_datodesconocido,'clientes.fechaingreso';

insert into clientes default values;


GO
create rule RG_legajo
as @valor like 'B%';
GO
exec sp_bindrule RG_legajo,'clientes.legajo';

insert into clientes values (default,'Luis Garcia','Colon


876','Cordoba','Cordoba','2001-10-10');

INSERTAR Y ELIMINAR REGISTROS


if object_id('agenda') is not null
drop table agenda;

create table agenda(


apellido varchar(30),
nombre varchar(20),
domicilio varchar(30),
telefono varchar(11)
);

insert into agenda (apellido,nombre,domicilio,telefono)


values('Alvarez','Alberto','Colon 123','4234567');
insert into agenda (apellido,nombre,domicilio,telefono)
values('Juarez','Juan','Avellaneda 135','4458787');
insert into agenda (apellido,nombre,domicilio,telefono)
values('Lopez','Maria','Urquiza 333','4545454');
insert into agenda (apellido,nombre,domicilio,telefono)
values('Lopez','Jose','Urquiza 333','4545454');
insert into agenda (apellido,nombre,domicilio,telefono)
values('Salas','Susana','Gral. Paz 1234','4123456');

delete from agenda


where nombre='Juan';

delete from agenda


where telefono='4545454';

select * from agenda;

delete from agenda;

select * from agenda;

You might also like