MANUAL DE SQL
   

CREACION DE BASES DE DATOS

        use master
        go
        if exists (select*from sysdatabases where name='viajes')
        drop database viajes
        go
        create
        database viajes
        go
        use viajes
        /*Creando la tabla Omnibus*/
        go
        create table omnibus
        (Placa char(7) not null,
        Marca varchar(50) not null,
        Modelo varchar(50) not null,
        Capacidad numeric(2) not null,
        Estado char(2) not null,
        constraint PK_Omnibus primary key (Placa),
        constraint Chr_Estado check(estado In('O','M','P','D')),
        constraint Chr_Capacidad check(Capacidad>=25),
        constraint Chr_Placa check(placa like '[a-z][a-z][0-9][0-9][0-9][0-9][0-9]'))
        go
        /*Insertando Datos en la tabla Omnibus*/
        go
        Insert Omnibus values('PQ29186','Mercedes','Avion',50,'O')
        Insert Omnibus values('PQ29186','Toyota','Barco',45,'P')
        Insert Omnibus values('RQ35689','Hiundai','Baltic',54,'D')
        Insert Omnibus values('En56457','Nissan','Mitsubishi',29,'M')
        Insert Omnibus values('PQ24785','Asia','Sapo',60,'o')
        Insert Omnibus values('PA29366','Ford','Avion',25,'M')
        select*from omnibus

        El siguinte ejemplo es la creacion de otra base de datos llamado Notas

        --crear la base de datos notas que contenga las tablas : alumnos,cursos,profesores,notas
        --un alumno puede tener muchas notas. Un profesor registra muchas notas,


        use master
        go
        if exists(select*from sysdatabases where name='Notas')
        drop database notas
        go
        create data
        base notas
        go
        use notas
        go
        if exists(select*from sysobjects where name='Alumno')
        drop table alumno
        go
        create table alumno

        (Id_Alu char(4) not null,
        cur char(4) null references cursos(Id_cur),
        id_not char(4) null references notas(Id_not),
        Nombre varchar(25) not null,
        Apellidos varchar(50) not null,
        sexo char(1) not null,
        Dni char(11) null,
        notas char(2) not null,
        constraint Alumn primary key(Id_Alu),
        constraint ch_ksexo check(sexo like '[m,f,M,F]'),
        constraint unico unique(Nombre,apellidos),
        constraint chknotas check(notas between 1 and 20),
        constraint pr_dfgd check(Id_alu like '[a-z][a-z][0-9][0-9]'))
        go
        if exists(select*from sysobjects where name='profesor')
        drop table profesor
        go
        create table profesor
        (Id_pro char(4) not null,
        id_not char(4) null references notas(Id_not),
        Nombre varchar(25) not null,
        Apellidos varchar(50) not null,
        sexo varchar(1) not null,
        Dni char(11) null,
        constraint p_kprofe primary key(Id_pro),
        constraint ch_exo check(sexo like '[m,f,M,F]'),
        constraint unicos unique(Nombre,apellidos))
        go
        if exists(select*from sysobjects where name='cursos')
        drop table cursos
        go
        create table cursos
        (Id_cur char(4) not null,
        Nombre varchar(25) not null,
        finicio varchar(15) not null,
        ftermino varchar(15) not null,
        constraint p_curso primary key(Id_cur))
        go
        if exists(select*from sysobjects where name='notas')
        drop table notas
        go
        create table notas
        (id_not char(4)not null primary key,
        n1 char(2) not null,
        n2 char(2) not null,
        n3 char(2) not null,
        Pparcial char(2) not null,
        Pfinal char(2) not null,
        constraint chn1 check(n1 between 1 and 20),
        constraint chn2 check(n2 between 1 and 20),
        constraint chn3 check(n3 between 1 and 20),
        constraint chPparcial check(Pparcial between 1 and 20),
        constraint pr_dar check(id_not like '[a-z][0-9][0-9][0-9]'),
        constraint chPfinal check(Pfinal between 1 and 20))
        go
        insert notas values('M001','12','14','14','15','13')
        insert notas values('M002','17','18','17','15','13')
        insert notas values('M003','15','15','19','15','13')
        insert notas values('M004','16','14','18','15','13')
        insert notas values('M005','14','18','19','15','13')
        insert notas values('M006','17','14','184','15','13')


        go
        select * from notas

        insert cursos values('0001','Angel','12/02/23','12/45/35')
        insert cursos values('0002','Marcos','12/02/23','12/45/35')
        insert cursos values('0003','Antonio','12/02/23','12/45/35')
        insert cursos values('0004','Carlos','12/02/23','12/45/35')
        go
        select * from cursos
        go
        insert Alumnos values('ac45','0001','Angel','Calderon,Fabian','m','45213568877','12')
        insert Alumnos values('ac45','0002','Marcos','Alzamora peres','m','45217896542','12')
        insert Alumnos values'ac45','0003','Antonio','Gutierres,Fabian','m','25478965412','12')
        insert Alumnos values'ac45','0004','Carlos','Angel alvarado','m','256314578964','12')

        go
        select * from alumno

         

        Inicio

        Bases De Datos-------Funciones------Procedimientos Almacenados------Consultas

        ©Copyrigth 2003 Todos los Derechos Reservados
        Maria Afan Medrano
        E-mail: mariaafan@hotmail.com
        Av.Iquitos 1391
        Lima- Peru