Git Product home page Git Product logo

Comments (3)

thori0908 avatar thori0908 commented on September 15, 2024

課題1

mysql> create database kadai14DB default character set utf8;
Query OK, 1 row affected (0.00 sec)

mysql> use kadai14DB;
Database changed

mysql> create table kadai1  (id int not null, title varchar(32), note text, created_at datetime not null ) engine=InnoDB;
Query OK, 0 rows affected (0.01 sec)

mysql> describe kadai1;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id         | int(11)     | NO   | PRI | NULL    |       |
| title      | varchar(32) | YES  |     | NULL    |       |
| note       | text        | YES  |     | NULL    |       |
| created_at | datetime    | NO   |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

Ref.
http://www.dbonline.jp/mysql/storage/index3.html
http://d.hatena.ne.jp/kariostro/20090813/1250174317

課題2

mysql> drop table kadai1;
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
Empty set (0.00 sec)

mysql> create table kadai1  (id int not null, title varchar(32), note text, created_at datetime not null, primary key(id) ) engine=InnoDB;
Query OK, 0 rows affected (0.01 sec)

mysql> describe kadai1;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id         | int(11)     | NO   | PRI | NULL    |       |
| title      | varchar(32) | YES  |     | NULL    |       |
| note       | text        | YES  |     | NULL    |       |
| created_at | datetime    | NO   |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

課題3

  • titleの次に以下のカラムを追加
    • カラム名:editor 型:VARCHAR(16) NULL可:YES
mysql> alter table kadai1 add editor varchar(16) after title;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> describe kadai1;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id         | int(11)     | NO   | PRI | NULL    |       |
| title      | varchar(32) | YES  |     | NULL    |       |
| editor     | varchar(16) | YES  |     | NULL    |       |
| note       | text        | YES  |     | NULL    |       |
| created_at | datetime    | NO   |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
  • titleのNULL可をNOに変更
mysql> alter table kadai1 modify title varchar(32) not null;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> describe kadai1;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id         | int(11)     | NO   | PRI | NULL    |       |
| title      | varchar(32) | NO   |     | NULL    |       |
| editor     | varchar(16) | YES  |     | NULL    |       |
| note       | text        | YES  |     | NULL    |       |
| created_at | datetime    | NO   |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
  • noteを削除
mysql> alter table kadai1 drop note; 
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> describe kadai1;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id         | int(11)     | NO   | PRI | NULL    |       |
| title      | varchar(32) | NO   |     | NULL    |       |
| editor     | varchar(16) | YES  |     | NULL    |       |
| created_at | datetime    | NO   |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

Ref.
http://www.dbonline.jp/mysql/table/index20.html
http://d.hatena.ne.jp/pipi_one/20100525/1274791619

課題4

mysql> create table kadai4 (id int not null, detail text, foreign key(id) references kadai1(id)) engine=InnoDB;
Query OK, 0 rows affected (0.01 sec)

mysql> describe kadai4;
+--------+---------+------+-----+---------+-------+
| Field  | Type    | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+-------+
| id     | int(11) | NO   | MUL | NULL    |       |
| detail | text    | YES  |     | NULL    |       |
+--------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

Ref.
http://www.limy.org/program/db/mysql/mysql_create_table.html

課題5

mysql> drop table kadai4;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
+---------------------+
| Tables_in_kadai14DB |
+---------------------+
| kadai1              |
+---------------------+
1 row in set (0.00 sec)

mysql> create table kadai4 (id int not null, detail text, foreign key(id) references kadai1(id)) engine=InnoDB;
Query OK, 0 rows affected (0.01 sec)

mysql> describe kadai4;
+--------+---------+------+-----+---------+-------+
| Field  | Type    | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+-------+
| id     | int(11) | NO   | MUL | NULL    |       |
| detail | text    | YES  |     | NULL    |       |
+--------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

Ref.
http://thinkit.co.jp/free/article/0608/1/1/

from training.

fr-itaya avatar fr-itaya commented on September 15, 2024

確認しました!OKです!

テーブル情報の確認について

DESCRIBEだと文字モードやストレージエンジンが表示されないので、テーブル情報の確認にはSHOW CREATE TABLE tablename;もよく使います。合わせて覚えておきましょう!

from training.

fr-matsuo avatar fr-matsuo commented on September 15, 2024

確認しました、OKです!

SQL文について

SQL文は;を入力するまで解釈されないため、複数行に分けたり、インデントを入れることが可能です。

削除など、特に慎重な発行が求められる際は、予めメモ帳などに記述して整え、
確認後にコピペするという方法をとると、安全でログも見やすくなります。

from training.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.