MySQL database entry (2) - basic database operations

1. Connect database

mysql -u root -p

2. Exit connection

exit/quit/ctrl+d

3. Display Database

show databases;

4 shows the current time database

select now();

5. Display the current database version

select version();

6. Create a database

create database new_database;
create database new_database2 charset=utf8;

6.1 View database created

show create database new_database;

6.2 Database

use new_database;

6.3 View database currently in use

select database();

6.4 View current database of all the tables

show tables;

6.5 to create a data table

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 View data table structure

desc students;

6.5.2 insert data into the table

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

The result is:

  • Insert part
-- insert into 表名(字段1,...) values (值1,...);
insert into students (name, gender) values ("小乔", 2)
  • Multiple-row inserts
-- insert into 表名(字段1,...) values (值1,...), (值1,...);
insert into students (name, gender) values ("大乔", 2), ("貂蝉", 2)

6.5.3 modify data in the table

-- 语法: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 query data in the table

  • Full field inquiry
-- select * from 表名 where 条件;
-- 例1:查询全部数据
select * from students;
-- 例2:根据条件查询部分数据
select * from students where id>2;
  • Query the specified field
-- select 字段1, 字段2,... from 表名;
select name,gender from students;
  • Specifies the field queries (also specify an alias for the field)
-- select 字段1 [as 别名], 字段2 [as 别名] from 表名 where 条件;
select name as 姓名,gender as 性别 from students;

6.5.5 delete data in the table

-- 物理删除
-- 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 modify the data table

6.6.1 Add field

alter table students add birthday datetime;

6.6.2 modify the field

alter table students modify birthday date;

6.6.3 Renaming Fields

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

6.6.4 Delete field

alter table students drop height;

6.7 Delete Data Sheet

drop table students;

You can show tables;look at the results after deleting the data table.

7. Delete database

drop database new_database;
Released two original articles · won praise 0 · Views 26

Guess you like

Origin blog.csdn.net/weixin_37780776/article/details/104817096