【Mysql】mysql 數據庫的增刪改查

# 進入數據庫
mysql -u[user] -p
mysql -u[user] -p[passwd]
# 舉例
mysql -uroot -p
mysql -uroot -pmysql



# 退出數據庫
exit
quit
# ctrl + d 也可以退出數據庫



# 顯示當前數據庫的時間
select now();

# 顯示當前數據庫版本
select version();

# 展示mysql裏面的數據庫
show databases;





# 創建數據庫
crate database 數據庫名稱 charset=utf8;
# 舉例
create database python charset=utf8;
# 查看創建數據庫的語句
show create database python;
# 刪除數據庫
drop database python;
# 選擇要用的數據庫
# use database
use python
# 查看正在使用的數據庫
select database();

# 查看當前數據庫裏面所有的表
show tables;


# 創建表
# create table 數據表名字(字段 類型 約束[,字段 類型 約束]);
# 舉例
create table class(id int,name varchar(30));
create table class_04(id int primary key not null auto_increment,name varchar(30));
create table students(
   id int unsigned primary key not null auto_increment,
   name varchar(30),
   age tinyint unsigned,
   high decimal(5,2),
   gender enum("男","女","保密") default "保密",
   cls_id int unsigned
)


# 查看錶格 desc 表格名
desc class


# 往表格裏面添加數據
insert into students values(0,"zhangsan","15","170.32","男",0);
# 部分插入
insert into students (name,gender) values ("xiaoqiao","2")



# 查詢數據
select * from students;
select * from students where name="xiaoli";
select * from students where id<9;
select * from students where id>6;
select age,gender from students;
select age as 年齡,gender as 性別 from students;


# 修改表-添加字段
alter table students add birthday datetime;


# 修改表-修改字段:重命名版
alter table students change birthday birth datetime not null;

# 修改表-修改字段 不重命名版
alter table  students modify birth date not null;

# 修改表-刪除字段
alter table students drop birthday;

# 刪除表
drop table students;

# 查看錶結構
show create table students;

# 修改已經添加到表格裏面的數據【修改整個表格裏面的所有數據】
update students set gender=1
# 修改已經添加到表格裏面的數據【指定某一條信息】
update students set gender=1 where id=3
# 修改已經添加到表格裏面的數據【指定某一條信息】
update students set age=22,gender=1 where name=zhangsan; 


# 刪除數據表裏面的數據  delete from 表名 where 條件
delete from students where name="zhangsan";

# 邏輯刪除
alter table students add is_delete bit default 0;
update students set is_delete=1 where id=11;
select * from students where is_delete=0;
 
 





















 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章