Comments (3)
課題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.
確認しました!OKです!
テーブル情報の確認について
DESCRIBE
だと文字モードやストレージエンジンが表示されないので、テーブル情報の確認にはSHOW CREATE TABLE tablename;
もよく使います。合わせて覚えておきましょう!
from training.
確認しました、OKです!
SQL文について
SQL文は;
を入力するまで解釈されないため、複数行に分けたり、インデントを入れることが可能です。
削除など、特に慎重な発行が求められる際は、予めメモ帳などに記述して整え、
確認後にコピペするという方法をとると、安全でログも見やすくなります。
from training.
Related Issues (20)
- サーバサイド - フレームワーク - フレームワークとは HOT 3
- サーバサイド - フレームワーク - Laravel - インストールと設定 HOT 3
- サーバサイド - フレームワーク - Laravel - routingとview作成
- サーバサイド - フレームワーク - Laravel - テンプレートエンジンのBladeを使う HOT 2
- サーバサイド - フレームワーク - Laravel - Controller作成 HOT 1
- サーバサイド - フレームワーク - Laravel - フォームのバリデーション、CSRF対策 HOT 2
- フロントエンド - JavaScript/jQuery - JavaScript/jQueryを理解する HOT 3
- フロントエンド - JavaScript/jQuery - Hello World!
- フロントエンド - JavaScript/jQuery - 関数を作る
- フロントエンド - JavaScript/jQuery - DOM操作
- フロントエンド - JavaScript/jQuery - イベント操作
- フロントエンド - JavaScript/jQuery - 条件分岐
- フロントエンド - JavaScript/jQuery - ajax通信
- サーバサイド - サーバサイドテスト - 自動ユニットテストの基礎 HOT 3
- サーバサイド - PHP - PHPを理解する HOT 6
- サーバサイド - PHP - PHPを書く HOT 11
- サーバサイド - PHP - フォーム作成
- フロントエンド - JavaScriptTraining HOT 1
- ruby - 環境構築 HOT 8
- ruby - 基本構文 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from training.