Yii для чайников \Yii for dummies (создание базы))

 для наглядности решил использовать mysql workbench . для ленивых (актуально 20.02.2015)
подключение к локальной базе.



я создал такую базу

получаем
---------
-- MySQL Script generated by MySQL Workbench
-- 02/20/15 20:38:33
-- Model: New Model    Version: 1.0
-- MySQL Workbench Forward Engineering

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `mydb` ;

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `mydb`.`tbl_department`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`tbl_department` ;

CREATE TABLE IF NOT EXISTS `mydb`.`tbl_department` (
  `d_id` INT NOT NULL AUTO_INCREMENT,
  `d_name` VARCHAR(45) NULL,
  `d_parent` INT NULL,
  `d_coment` TEXT(0) NULL,
  PRIMARY KEY (`d_id`),
  UNIQUE INDEX `d_id_UNIQUE` (`d_id` ASC),
  UNIQUE INDEX `d_name_UNIQUE` (`d_name` ASC))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`tbl_user`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`tbl_user` ;

CREATE TABLE IF NOT EXISTS `mydb`.`tbl_user` (
  `u_id` INT NOT NULL AUTO_INCREMENT,
  `u_name` VARCHAR(45) NOT NULL,
  `u_role` VARCHAR(45) NULL,
  `tbl_department_d_id` INT NOT NULL,
  PRIMARY KEY (`u_id`, `tbl_department_d_id`),
  INDEX `fk_tbl_user_tbl_department1_idx` (`tbl_department_d_id` ASC),
  CONSTRAINT `fk_tbl_user_tbl_department1`
    FOREIGN KEY (`tbl_department_d_id`)
    REFERENCES `mydb`.`tbl_department` (`d_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`tbl_motor`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`tbl_motor` ;

CREATE TABLE IF NOT EXISTS `mydb`.`tbl_motor` (
  `m_id` INT NOT NULL AUTO_INCREMENT,
  `m_name` VARCHAR(45) NOT NULL,
  `m_coment` TEXT(0) NULL,
  `m_date` TIMESTAMP(0) NULL,
  `m_timestamp` TIMESTAMP(0) NULL,
  `tbl_motorcol` VARCHAR(45) NULL,
  `tbl_user_u_id` INT NOT NULL,
  `tbl_department_d_id` INT NOT NULL,
  PRIMARY KEY (`m_id`, `tbl_user_u_id`, `tbl_department_d_id`),
  UNIQUE INDEX `m_id_UNIQUE` (`m_id` ASC),
  INDEX `fk_tbl_motor_tbl_user_idx` (`tbl_user_u_id` ASC),
  INDEX `fk_tbl_motor_tbl_department1_idx` (`tbl_department_d_id` ASC),
  CONSTRAINT `fk_tbl_motor_tbl_user`
    FOREIGN KEY (`tbl_user_u_id`)
    REFERENCES `mydb`.`tbl_user` (`u_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_tbl_motor_tbl_department1`
    FOREIGN KEY (`tbl_department_d_id`)
    REFERENCES `mydb`.`tbl_department` (`d_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
 
---- то что выдал benchmark
 
 
--
-- Current Database: `yii`
--

/*!40000 DROP DATABASE IF EXISTS `%s` */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `yii` /*!40100 DEFAULT CHARACTER SET utf8 */;

USE `yii`;

-- MySQL dump 10.13  Distrib 5.6.22, for Win32 (x86)
--
-- Host: localhost    Database: yii
-- ------------------------------------------------------
-- Server version 5.5.25

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;


--
-- Table structure for table `tbl_department`
--

DROP TABLE IF EXISTS `tbl_department`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `tbl_department` (
  `d_id` int(11) NOT NULL AUTO_INCREMENT,
  `d_name` varchar(45) DEFAULT NULL,
  `d_parent` int(11) DEFAULT NULL,
  `d_coment` text,
  PRIMARY KEY (`d_id`),
  UNIQUE KEY `d_id_UNIQUE` (`d_id`),
  UNIQUE KEY `d_name_UNIQUE` (`d_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `tbl_department`
--

LOCK TABLES `tbl_department` WRITE;
/*!40000 ALTER TABLE `tbl_department` DISABLE KEYS */;
/*!40000 ALTER TABLE `tbl_department` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `tbl_motor`
--

DROP TABLE IF EXISTS `tbl_motor`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `tbl_motor` (
  `m_id` int(11) NOT NULL AUTO_INCREMENT,
  `m_name` varchar(45) NOT NULL,
  `m_coment` text,
  `m_date` timestamp NULL DEFAULT NULL,
  `m_timestamp` timestamp NULL DEFAULT NULL,
  `fk_user` int(11) DEFAULT NULL,
  `fk_dep` int(11) DEFAULT NULL,
  PRIMARY KEY (`m_id`),
  UNIQUE KEY `m_id_UNIQUE` (`m_id`),
  KEY `fk_user` (`fk_user`),
  KEY `fk_dep` (`fk_dep`),
  CONSTRAINT `tbl_motor_fk1` FOREIGN KEY (`fk_user`) 
REFERENCES `tbl_user` (`u_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `tbl_motor_fk2` FOREIGN KEY (`fk_dep`)  
REFERENCES `tbl_department` (`d_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `tbl_motor`
--

LOCK TABLES `tbl_motor` WRITE;
/*!40000 ALTER TABLE `tbl_motor` DISABLE KEYS */;
/*!40000 ALTER TABLE `tbl_motor` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `tbl_user`
--

DROP TABLE IF EXISTS `tbl_user`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `tbl_user` (
  `u_id` int(11) NOT NULL AUTO_INCREMENT,
  `u_name` varchar(45) NOT NULL,
  `u_role` varchar(45) DEFAULT NULL,
  `tbl_department_d_id` int(11) NOT NULL,
  PRIMARY KEY (`u_id`,`tbl_department_d_id`),
  KEY `fk_tbl_user_tbl_department1_idx` (`tbl_department_d_id`),
  KEY `u_id` (`u_id`),
  CONSTRAINT `fk_tbl_user_tbl_department1` FOREIGN KEY (`tbl_department_d_id`) 
REFERENCES `tbl_department` (`d_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `tbl_user`
--

LOCK TABLES `tbl_user` WRITE;
/*!40000 ALTER TABLE `tbl_user` DISABLE KEYS */;
/*!40000 ALTER TABLE `tbl_user` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2015-02-21  7:43:12
 
реальный дамп
---------
немного объясню - есть некий прибор (таблица мотор). нужно сохранять имя последнего изменившего запись(ссылка на таблицу юзера) и указывать цех где назодится мотор (ссылка на таблицу цехов). также каждый юзер относится к определенному цеху
------


не буду лукавить. видимо первую базу придется создать через phpmyadmin,  я создал yii и подключился через ems mysql

`mydb` нужно в этом случае заменить в npp на yii (именно так без скобок).
 
PK - Primary Key
NN - Not Null
BIN - Binary
UN - Unsigned
UQ - Create/remove Unique Key
ZF - Zero-Filled
AI - Auto Incremenent

Комментарии

Популярные сообщения из этого блога

Пишем логи на C# (.NET). Легкий способ.

Учебник yii2