MySQLデータベースのエントリ(2) - 基本的なデータベース操作

1. Connectデータベース

mysql -u root -p

2.出口接続

exit/quit/ctrl+d

3.ディスプレイのデータベース

show databases;

図4は、現在時刻データベース

select now();

現在のデータベースのバージョンを表示します。5.

select version();

データベースを作成します6.

create database new_database;
create database new_database2 charset=utf8;

6.1 Viewデータベースを作成しました

show create database new_database;

6.2データベース

use new_database;

現在使用されている6.3 Viewデータベース

select database();

すべてのテーブルの6.4ビュー現在のデータベース

show tables;

データテーブルを作成するために、6.5

create table students(
	id int unsigned not null auto_increment primary key,
	name varchar(30),
	age tinyint unsigned,
	height decimal(5,2),
	gender enum("男", "女", "中性", "未知") default "未知",
	cls_id int unsigned
	);

6.5.1ビューのデータテーブル構造

desc students;

テーブルへのデータの挿入6.5.2

  • 全視野に
insert into students values(0, "老王", 18, "188.88", "男", 0);
-- 注意:枚举enum("男", "女", "中性", "未知")的下标从1开始,如使用下述SQL语句的插入数据
insert into students values(0, "老王", 18, "188.88", 2, 0);

結果は次のとおりです。

  • 一部を挿入
-- insert into 表名(字段1,...) values (值1,...);
insert into students (name, gender) values ("小乔", 2)
  • 複数行の挿入
-- insert into 表名(字段1,...) values (值1,...), (值1,...);
insert into students (name, gender) values ("大乔", 2), ("貂蝉", 2)

表中の6.5.3の変更データ

-- 语法:update 表名 set 字段1=值1,字段2=值2... where 条件;
-- 例1: 
update students set gender=1 where name="貂蝉";
-- 例2: 
update students set gender=1 where id=3;
-- 例3: 
update students set age=22,gender=1 where id=3;

表中の6.5.4クエリデータ

  • 全視野問い合わせ
-- select * from 表名 where 条件;
-- 例1:查询全部数据
select * from students;
-- 例2:根据条件查询部分数据
select * from students where id>2;
  • クエリの指定したフィールド
-- select 字段1, 字段2,... from 表名;
select name,gender from students;
  • 指定フィールドクエリ(また、フィールドのエイリアスを指定します)
-- select 字段1 [as 别名], 字段2 [as 别名] from 表名 where 条件;
select name as 姓名,gender as 性别 from students;

表中の6.5.5削除データ

-- 物理删除
-- delete from 表名 where 条件;
-- 例1:
delete from students;
-- 例2:
delete from students where name="大乔"
-- 逻辑删除
-- 添加一个字段,用来表示这条信息是否已经不能再使用了
-- 给students表添加一个is_delete字段,字段类型为bit
alter table students add is_delete bit default 0;
-- 使用is_delete字段,将符合条件的记录进行逻辑删除
update students set is_delete=1 where id=2;
-- 通过字段is_delete查询逻辑删除后的数据表
select * from students where is_delete=0;

6.6データテーブルを変更します

6.6.1追加フィールド

alter table students add birthday datetime;

フィールドを変更6.6.2

alter table students modify birthday date;

6.6.3名前の変更フィールド

alter table students change birthday birth date default "1990-01-01";

6.6.4 [削除]フィールド

alter table students drop height;

6.7削除データシート

drop table students;

あなたはできるshow tables;データテーブルを削除した後の結果を見てください。

7. [削除]データベース

drop database new_database;
リリース元の2件の記事 ウォンの賞賛0 ビュー26

おすすめ

転載: blog.csdn.net/weixin_37780776/article/details/104817096