How can I pass the sql server script to a functional script in mysql?

0
CREATE DATABASE chauquear 
GO
USE chauquear
GO

CREATE TABLE [dbo].[area_ch](
    [idarea] [int] IDENTITY(1,1) NOT NULL,
    [nombre_area] [varchar](45) primary key NOT NULL default 'Sin Area',
    [fecha] [datetime] NOT NULL DEFAULT (getdate()),
    [estado] [varchar](45) NOT NULL
    )


CREATE TABLE [dbo].[centro_cultivo_ch](
    [idcc] [int] IDENTITY(1,1) NOT NULL,
    [nombre_CentroCultivo] [varchar](45) primary key NOT NULL default 'Sin Centro Cultivo',
    [fecha] [datetime] NOT NULL DEFAULT (getdate()),
    [estado] [varchar](45) NOT NULL,
    [nombre_area] [varchar](45) foreign key (nombre_area)
    references area_ch(nombre_area) 
    on delete set default
    ON UPDATE CASCADE,
    [codigo_decreto] [varchar](45) NOT NULL,
    [codigo_concesion] [varchar](45) NOT NULL,
    [total_linea] [int] NOT NULL,
    [monto_inicial] [int] NOT NULL,
    [creado] [int] NOT NUll)


    CREATE TABLE [dbo].[cuadrante_ch](
    [idcuadrante] [int] IDENTITY(1,1) NOT NULL,
    [nombre_cuadrante] [varchar](45) primary key NOT NULL default 'Sin Cuadrante',
    [total_linea] [int] NOT NULL,
    [monto_inicial] [int] NOT NULL,
    [creado] [int] NOT NUll,
    [fecha] [datetime] NOT NULL,
    [nombre_area] [varchar](45) NOT NULL ,
    [nombre_CentroCultivo] [varchar](45)  foreign key (nombre_CentroCultivo)
    references centro_cultivo_ch(nombre_CentroCultivo) 
    on delete set default 
    ON UPDATE CASCADE,
    [estado] [varchar](45) NOT NULL
    )

CREATE TABLE [dbo].[linea_ch](    
    [idlinea] [int] IDENTITY(1,1) NOT NULL ,
    [nombre_linea] [varchar] (45) NOT NULL primary key default 'Sin Linea' ,
    [fecha] [datetime] NOT NULL DEFAULT (getdate()),
    [estado] [varchar](45) NOT NULL,
    [TipoMaterial] [varchar](45) NOT NULL,
    [Observacion] [varchar](140) NOT NULL,
    [nombre_area] [varchar](45) NOT NULL,
    [nombre_CentroCultivo] [varchar](45) NOT NULL,
    [nombre_cuadrante] [varchar](45) foreign key (nombre_cuadrante)
    references cuadrante_ch(nombre_cuadrante) 
    on delete set default 
    ON UPDATE CASCADE,
    [cantidad] int not null,
    total_cuelgas int not null,
    [stock_inicial] [int] NOT NULL,
    [creado] [int] NOT NUll
)

CREATE TABLE [dbo].[BodegaSiembraSemillaColector_ch](    
    [idBodegaSiembraSemillaColector] [int] IDENTITY(1,1) NOT NULL ,
    [Codigo_Bodega] [varchar] (45) NOT NULL,
    [nombre_Bodega] [varchar] (45) NOT NULL primary key ,
    [fecha] [datetime] NOT NULL DEFAULT (getdate()),
    [estado] [varchar](45) NOT NULL,
    [Observacion] [varchar](140) NOT NULL,
    [nombre_area] [varchar](45) NOT NULL,
    [nombre_CentroCultivo] [varchar](45) NOT NULL,
    [nombre_cuadrante] [varchar](45)NOT NULL,
    [nombre_linea] [varchar] (45) foreign key (nombre_linea)
    references linea_ch(nombre_linea) 
    on delete set default 
    ON UPDATE CASCADE,
    [Tipo_Semilla] [varchar] (45) NOT NULL,
    [cantidad_Semilla] int not null,
    total_cuelgas int not null,
    [stock_inicial] [int] NOT NULL,
    [creado] [int] NOT NUll,
    StockInicial_Semilla int NOT NULL,
    CreadoSemilla int not null  
)



    CREATE TABLE [dbo].[NombreSiembra_ch](
    [idsiembra_SemillaColector] [int] IDENTITY(1,1) NOT NULL,
    [FolioSiembraSemillaColector] [varchar](45) NOT NULL,
    [fecha] [datetime] NOT NULL DEFAULT (getdate()),
    [Nombre_SiembraSemillaColector] [varchar](45) NOT NULL primary key  default  'Sin Folio',
    [Presupuesto] int NOT NULL)

    CREATE TABLE [dbo].[siembra_semillacolector_ch](
    [idsiembra_SemillaColector] [int] IDENTITY(1,1) NOT NULL,
    [FolioSiembraSemillaColector] [varchar](45) NOT NULL  primary key  default  'Sin Folio',
    [nombre_area] [varchar](45) NOT NULL,
    [nombre_CentroCultivo] [varchar](45) NOT NULL,
    [nombre_cuadrante] [varchar](45) NOT NULL,
    [nombre_linea] [varchar] (45) foreign key (nombre_linea)
    references linea_ch(nombre_linea) 
    on delete set default 
    ON UPDATE CASCADE,
    [fecha] [datetime] NOT NULL DEFAULT (getdate()),
    [Nombre_SiembraSemillaColector] [varchar](45) foreign key (Nombre_SiembraSemillaColector)
    references NombreSiembra_ch(Nombre_SiembraSemillaColector) 
    on delete set default 
    ON UPDATE CASCADE,
    [origen_semilla] [varchar](45) NOT NULL,
    [numero_colectores] [int] NOT NULL,
    [numero_flotadores] [int] NOT NULL,
    [total_colectores] [int] NOT NULL,
    [talla_semilla] [varchar](45) NOT NULL,
    [peso_promedio] [int] NOT NULL,
    [destino] [int] NOT NULL,
    [Observacion] [varchar](100) NOT NULL,
    [transporte] [int] NOT NULL,
    [cantidad] int not null,
    total_cuelgas int not null
    )

CREATE TABLE [dbo].[kardex_cuadrante](
[id_kardexCuadrante] [int] IDENTITY(1,1) NOT NULL,
[nombre_area] [varchar](45) NOT NULL,
[nombre_CentroCultivo] [varchar](45) foreign key (nombre_CentroCultivo)
references centro_cultivo_ch(nombre_CentroCultivo) 
on delete CASCADE 
ON UPDATE CASCADE,
[nombre_cuadrante] [varchar](45) NOT NULL,
[monto_inicial] [int] NOT NULL,
[creado] [int] NOT NUll,
[total_linea] [int] NOT NULL,
[fecha] [datetime] NOT NULL
)

CREATE TABLE [dbo].[kardex_linea](
[id_kardexLinea] [int] IDENTITY(1,1) NOT NULL,
[nombre_area] [varchar](45) NOT NULL,
[nombre_CentroCultivo] [varchar](45) NOT NULL,
[nombre_cuadrante] [varchar](45) foreign key (nombre_cuadrante)
references cuadrante_ch(nombre_cuadrante) 
on delete CASCADE 
ON UPDATE CASCADE,
[nombre_linea] [varchar] (45) NOT NULL,
[total_linea] [int] NOT NULL,
[monto_inicial] [int] NOT NULL,
[creado] [int] NOT NUll,
[fecha] [datetime] NOT NULL,
)

CREATE TABLE [dbo].[kardex_cuelgas](
[id_kardexLinea] [int] IDENTITY(1,1) NOT NULL,
[nombre_area] [varchar](45) NOT NULL,
[nombre_CentroCultivo] [varchar](45) NOT NULL,
[nombre_cuadrante] [varchar](45) NOT NULL,
[nombre_linea] [varchar] (45)  foreign key (nombre_linea)
references linea_ch(nombre_linea) 
on delete CASCADE 
ON UPDATE CASCADE,
[total_linea] [int] NOT NULL,
[monto_inicial] [int] NOT NULL,
[creado] [int] NOT NUll,
[fecha] [datetime] NOT NULL
)

    CREATE TABLE [dbo].[usuarios](
    [id_usuarios] [int] IDENTITY(1,1) NOT NULL primary key,
    [Rut] [nvarchar](10) NOT NULL,
    [Nombre] [nvarchar](50) NOT NULL,
    [Apellidos] [nvarchar](50) NOT NULL,
    [Email] [nvarchar](45) NOT NULL,
    [Cargo] [int] NOT NULL,
    [Direccion] [nvarchar](100) NOT NULL,
    [Fecha_Nacimiento] [date] NOT NULL,
    [Telefono] [int] NOT NULL,
    [Ciudad] [nvarchar](50) NOT NULL,
    [Usuario] [nvarchar](15) NOT NULL,
    [Contrasena] [nvarchar](50) NOT NULL)

    CREATE TABLE [dbo].[cargo](
    [idcargo] [int] IDENTITY(1,1) NOT NULL primary key,
    [Nombre_cargo] [varchar](45) NOT NULL)


    ALTER TABLE [dbo].[usuarios]  WITH NOCHECK ADD  CONSTRAINT [usuarios$usuarios_ibfk_1] FOREIGN KEY([Cargo])
REFERENCES [dbo].[cargo] ([idcargo])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
SET IDENTITY_INSERT [dbo].[cargo] ON
INSERT [dbo].[cargo] ([idcargo], [Nombre_cargo]) VALUES (1, N'Programador')
SET IDENTITY_INSERT [dbo].[cargo] OFF
SET IDENTITY_INSERT [dbo].[usuarios] ON 

INSERT [dbo].[usuarios] ([id_usuarios], [Rut], [Nombre], [Apellidos], [Email], [Cargo], [Direccion], [Fecha_Nacimiento], [Telefono], [Ciudad], [Usuario], [Contrasena]) VALUES (1, N'18734602', N'Cristian', N'Jonhson Alvarez', N'[email protected]', 1, N'Puerto Montt', CAST(N'1994-04-22' AS Date), 89587246, N'Puerto Montt', N'cris', N'admin')
SET IDENTITY_INSERT [dbo].[usuarios] OFF
    
asked by JKonG 27.07.2018 в 03:59
source

1 answer

0

I tell you the following:

1.- Your code in MySQL should be as follows

MySQL[social]> CREATE TABLE area_ch(
    -> idarea INT PRIMARY KEY  AUTO_INCREMENT NOT NULL,
    -> nombre_area VARCHAR(45) UNIQUE NOT NULL,
    -> INDEX(nombre_area),
    -> fecha DATETIME NOT NULL DEFAULT NOW(),
    -> estado VARCHAR(45) NOT NULL);

idarea should be your PRIMARY KEY() , since it is the id with which each record is saved and it would later be useful to use it as a foreign key in another table with which you can relate

with AUTO_INCREMENT the increase of an initial value is made

By doing a DESCRIBE of the newly created table we will get a view like this

MySQL [social]> DESCRIBE area_ch;
+-------------+-------------+------+-----+---------------------+----------------+
| Field       | Type        | Null | Key | Default             | Extra          |
+-------------+-------------+------+-----+---------------------+----------------+
| idarea      | int(11)     | NO   | PRI | NULL                | auto_increment |
| nombre_area | varchar(45) | NO   | UNI | NULL                |                |
| fecha       | datetime    | NO   |     | current_timestamp() |                |
| estado      | varchar(45) | NO   |     | NULL                |                |
+-------------+-------------+------+-----+---------------------+----------------+

I commented to you that nombre_area should not be your PK because that is going to be a unique value that allows me to identify each registered register; in the same way this unique record is the one that will allow me to use it as FK in another table where you need to have related data

nombre_area can instead have the attribute of UNIQUE so that said value does not repeat itself store it as INDEX to use it in an optimized way when searching

In addition to using nombre_area as primary key, an attribute type that can not be repeated would limit the information input itself over time

At least in MySQL you can not have a column as primary key that is auto_increment and another column that is primary key; because auto increments can only be present once as PRIMARY KEY

    
answered by 27.07.2018 в 04:27