數據庫的增刪改

--顯示當前時間:sql

select now();

--建立students表(id, name, age, high, gender, cls_id)數據庫

	create table students (
	    id int unsigned (默認:not null)  primary key  auto_increment(自增),
	    name varchar(20),
	    age tinyint unsigned default 0,
	    high decimal(5,2),
	    gender enum('男', '女', '中性', '保密') default '保密',
	    cls_id int unsigned
	); 

--查看錶結構:安全

	desc student;
	MariaDB [testdb]> desc students;
	+--------+-------------------------------------+------+-----+---------+----------------+
	| Field  | Type                                | Null | Key | Default | Extra          |
	+--------+-------------------------------------+------+-----+---------+----------------+
	| id     | tinyint(3) unsigned                 | NO   | PRI | NULL    | auto_increment |
	| name   | varchar(20)                         | NO   |     | NULL    |                |
	| age    | tinyint(3) unsigned                 | YES  |     | NULL    |                |
	| high   | decimal(5,2)                        | YES  |     | NULL    |                |
	| gender | enum('男','女','中性','保密')       | YES  |     | 中性    |                |
	| cls_id | tinyint(3) unsigned                 | YES  |     | NULL    |                |
	+--------+-------------------------------------+------+-----+---------+----------------+

字段的增刪改:

-- 修改表-添加字段
  --alter table 表名 add 列名 類型;spa

alter table students add birthday datetime; 

-- 修改表-修改字段:不重命名版
  -- alter table 表名 modify 列名 類型及約束;blog

alter table students modify birthday date;

-- 修改表-修改字段:重命名版
  -- alter table 表名 change 原名 新名 類型及約束;ci

alter table students change birthday birth date;

  

-- 修改表-刪除字段
  -- alter table 表名 drop 列名;rem

alter table students drop birth; 

-- 刪除表
  -- drop table 表名;it

drop table students;

庫的增刪改查:

#查看數據庫table

show databases;         

#查看建立數據庫狀態class

show create database testdb;        

#使用哪一個庫

use testdb;

#建立數據庫

create database test03            #默認是拉丁字符集
create database testdb character set utf8;

#刪除數據庫

drop database testdb;

#修改數據庫默認字符集

alter database 庫名 default character set utf8; #alter改變數據庫結構/表結構       

數據的增刪改:

增長:
--全列插入
  --insert into 表名 values (..)

eg:insert into students values (0(默認),'小明',19,188.999,'男', 1);
--主鍵字段默認 能夠用 0 null default 來站位

  

--部分插入(注意約束)
  --insert into 表名(字段,字段…) values (..)

eg:insert into students(id, name, age) values (0,'綠帽子',19); 

--部分插入(多條記錄)

insert into students(id, name, age) values (0,'綠帽子',19),(0,'小跳蚤',21);

  

修改:
  --update 表名 set 列1=值1, 列2=值2... where 條件(還能夠範圍eg:id>6);

update students set age=100 where id=1; 
update students set age=100,cls_id=77 where id=1;

eg:update students set name='小飛',age=23 where id=9(或name='黃飛鴻2');

  

刪除:
  -- delete from 表名 #清空表

-- 物理刪除
  -- delete from 表名 where 條件

delete from students where name='黃飛鴻';
delete from students where cls_id=88; 

  

-- 清空表數據

truncate students; #沒法恢復數據,而且清空自增
delete from students;	#在必定條件下能夠恢復數據,不會清楚自增

-- 邏輯刪除
  -- 用一條字段來表示 這條信息是否已經不能在使用了
  -- 給students表添加一個is_delete字段 bit 類型

alter table students add is_delete bit default 0;

  

其餘: 

--default charset=utf8 默認字符集(按照庫來的)
--auto_increment(自增)

MyISAM與InnoDB區別:
--兩種類型最主要的區別就是InnDB支持事物處理與外鍵和行級鎖
  MyISAM查詢機制高於InnoDB
  InnoDB安全級別高於MyISAM

相關文章
相關標籤/搜索