MySQLデータベース_データベースとデータテーブルの操作

MySQLデータベース

コマンドライン接続

  • 今日では、端末は、基本的な作業でデータベースに接続し、操作するために使用されない。一般に、このようなどのグラフィカルツールNavicat for MySQLDBeaver等が使用されるが、動作データベースのコマンド操作方法は熟練を要します。

  • ターミナルを介して接続します。コマンド:mysql -uroot / mysql -uroot -p (输入密码)

次の図に示すように、接続が成功した後

mysql05

サインアウト

quit 和 exit
或
ctrl+d

データベース操作

  • すべてのデータベースを表示
show databases;
  • データベースを使用する
use 数据库名;
  • 現在使用されているデータベースを表示する
select database();
  • データベースを作成する
create database 数据库名 charset=utf8;
举例:
create database python charset=utf8;
  • データベースを削除する
drop database 数据库名;
例:
drop database python;

データテーブルの操作

  • 現在のデータベース内のすべてのテーブルを表示する
show tables;
  • テーブル構造を表示する
desc 表名;
  • テーブルを作成する
    • auto_incrementは自動拡張を意味します
CREATE TABLE table_name(
    column1 datatype contrai,
    column2 datatype,
    column3 datatype,
    .....
    columnN datatype,
    PRIMARY KEY(one or more columns)
);
例:创建班级表

create table classes(
    id int unsigned auto_increment primary key not null,
    name varchar(10)
);
例:创建学生表

create table students(
    id int unsigned primary key auto_increment not null,
    name varchar(20) default '',
    age tinyint unsigned default 0,
    height decimal(5,2),
    gender enum('男','女','人妖','保密'),
    cls_id int unsigned default 0
)

  • テーブルの変更-フィールドの追加
alter table 表名 add 列名 类型;
例:
alter table students add birthday datetime;
  • テーブルの変更-フィールドの変更:バージョンの名前が変更されました
alter table 表名 change 原名 新名 类型及约束;
例:
alter table students change birthday birth datetime not null;
  • テーブルの変更-フィールドの変更:バージョンの名前を変更しないでください
alter table 表名 modify 列名 类型及约束;
例:
alter table students modify birth date not null;
  • テーブルの変更-フィールドの削除
alter table 表名 drop 列名;
例:
alter table students drop birthday;
  • テーブルを削除する
drop table 表名;
例:
drop table students;
  • テーブル作成ステートメントを表示する
show create table 表名;
例:
show create table classes;

おすすめ

転載: blog.csdn.net/weixin_42250835/article/details/90241265