Mysplライブラリとテーブル構造の操作



Operation Myspl Library



データベースシステム

実際、私たちがよく言うデータベースは、データベースシステムと呼ばれるべきです。

1.1データベースのサーバーを起動します

# MAC system sudo /usr/local/mysql/support-files/mysql.server start # redhat/CentOS /usr/local/mysql/bin/mysql -u root -p

1.2データベースサーバーに接続します

# Unix-like operating system mysql -u root -p

-uユーザー名を指定します



-pは、次にパスワードを使用することを意味します

-Pはサーバーポートを指定します



-hはサーバーのIPを指定します

1.3データベースシステム管理者のパスワードを変更する

# One of the methods (under CentOS) there are more methods behind /usr/bin/mysqladmin -u root password'new password'

## 2。テーブルとライブラリの概念

データテーブル:データを保存するために使用されるテーブル



データベース:データテーブルを統合管理するためのコンテナ

3.図書館の運営

3.1ライブラリを表示して選択する

# Check how many databases there are in the database system show databases # Switch/select database use database name

3.2データベースを作成する

create database the name of the database you want to create # Create a database and specify the encoding and proofreading set create database `Database name` character set utf8 collate utf8_general_ci create database `Database name` character set utf8mb4 collate utf8mb4_general_ci

3.3既存のデータベースコードを変更する

alter database database name charset=new encoding

3.4データベースを削除する

drop database The name of the database to be dropped

4.データテーブル構造の操作

4.1データベース内のデータテーブルのリストを表示する

show tables

4.2データテーブルを作成する

# basic structure create table table name ( Field name 1 type (length), Field name 2 type (length), Field name 3 type (length), …… )

といった:

# Regularly create a data table CREATE TABLE `x2` ( `id` smallint(5), `name` varchar(4), `sex` tinyint(1), `mobile` char(11), `address` varchar(100), `add_time` datetime ) # Specify the encoding of the data table when creating the data table CREATE TABLE `x5` ( `id` smallint(5) DEFAULT NULL, `name` varchar(4) DEFAULT NULL, `sex` tinyint(1) DEFAULT NULL, `mobile` char(11) DEFAULT NULL, `address` varchar(100) DEFAULT NULL, `add_time` datetime DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8
4.2.1一般的なフィールドタイプ
フィールドタイプ 意義 表現範囲
tinyint 小さな整数 -128-127 0-255
smallint 小整数 -32768-32767 0-65535
ミディアムイント 中整数 -8388608-8388607; 0-16777215
int 整数 -2147493648-2147493647; 0-4294967295
bigint 大きい整数 -9223372036854775808-9223372036854775807; 0-18446744073709551615
浮く 単精度浮動小数点
ダブル 倍精度浮動小数点
char キャラクター
varchar キャラクター
テキスト テキスト 65535文字をサポートします。長さ+2バイトのストレージが必要
ミディアムテキスト ミディアムテキスト 16777215文字をサポートします。 +3バイトのストレージが必要
ロングテキスト 長いテキスト 4294967295文字をサポートします。 +4バイトのストレージが必要
日付時刻 日付タイプ
タイムスタンプ タイムスタンプの日付
4.2.2フィールドの制約
名前 意味 備考
nullではない 制限フィールド値をnullにすることはできません
デフォルト デフォルトに設定
主キー 主キーを設定する 最後に使用することもできますkey (field name)主キー
自動増加 自動成長を設定する 主キーフィールドのみを設定でき、タイプは整数です
署名なし フィールドを符号なしタイプに設定します
コメント メモ、指示、メモ

4.3データテーブルの列(フィールド)を変更する

列(フィールド)を追加する
alter table data table name add column name and type, constraint and other information # Add multiple columns at once alter table data table name add (column name 1 and type, constraint and other information, column name 2 and type, constraint and other information)
列(フィールド)を変更する
# The column name must already exist alter table data table name modify column name and type, constraint and other information # The old column name must exist, the new column name can be the same as the old column name, but not the same as other column names alter table data table name change old column name new column name and type, constraint and other information
列を削除
alter table data table name drop column name

4.4データテーブルのエンコーディングを変更する

alter table data table name charset=new encoding

4.5データテーブルを削除する

drop table table name

4.6テーブル作成ステートメントを表示する

show create table data table name