1. 程式人生 > 程式設計 >mysql基礎知識

mysql基礎知識

建庫

  1. 建立名為yusheng的資料庫 create database yusheng;

  2. 確認資料庫是否建立成功 show databases;

  3. 使用某個資料庫 use yusheng;

查庫

  1. 檢視資料庫版本
select version();
複製程式碼
  1. 檢視資料庫名字
select database();
複製程式碼
  1. 檢視當前使用者
select user();
複製程式碼
  1. 檢視資料庫狀態
show status\G
複製程式碼
  1. 檢視資料庫配置的變數
show variables\G
複製程式碼

建表

  1. 建立student表
create table if
not exists student( id int unsigned primary key auto_increment,name varchar(64) not null,birthday date default null )engine=innodb default charset=utf8; 複製程式碼
  1. 檢視student表是否建立成功 show tables;
  2. 檢視某個表的欄位屬性 desc student;
  3. 檢視建表語句 show create table student;
  4. 檢視錶的欄位屬性 show columns from student;
  5. 建立臨時表teacher,只對當前連線有效,關閉連線時自動刪除
create temporary table if not exists teacher(
    id int(10) unsigned primary key auto_increment,name varchar(32) not null
)engine=innodb default charset=utf8;
複製程式碼
  1. 建立student表的備份表student_bak
create table student_bak like student;(只複製表結構)
insert into student_bak select * from student;(只複製資料)
create table student_copy select * from student;(複製表結構和資料,但是表結構可能不完整,結合desc student_copy分析)
複製程式碼

落表

  1. 在student表中插入1條資料
insert into student(name,birthday) values('yusheng','2019-8-31');
複製程式碼
  1. 在student表中一次插入2條資料
insert into student(name,birthday) value('cliff','2019-8-31'),('ford','2019-8-31');
複製程式碼

查表

  1. 查詢student表所有資料(全表查詢)
select * from student;
複製程式碼
  1. 查詢名字以li開頭的所有學生(模糊查詢)
select * from student where name like 'li%';
複製程式碼
  1. 查詢名字叫lisi的學生(條件查詢)
select * from student where name = 'lisi';
複製程式碼
  1. 查詢生日分別是2019-8-31和2019-9-1的學生(聯合查詢)
select * from student where birthday = '2019-8-31' union select * from student where birthday = '2019-9-1';
複製程式碼
  1. 查詢名字以rd結尾的學生(正則查詢)
select * from student where name regexp 'rd$';
複製程式碼
  1. 按照生日統計學生的人數(分組查詢)
select birthday,count(*) from student group by birthday;
複製程式碼
  1. 同時找出所有學生的學生資訊與該學生的班級資訊(聯表查詢)
select birthday,count(*) from student group by birthday;(笛卡爾聯表)
select s.*,c.* from student s left join class c on s.class_id = c.id;(左聯)
select s.*,c.* from student s right join class c on s.class_id = c.id;(右聯)
select s.*,c.* from student s inner join class c on s.class_id = c.id;(內聯)
複製程式碼

修表

  1. 修改指定id學生的生日
update student set birthday = '2019-9-1' where id = 2;
複製程式碼
  1. 給student表新增欄位sex enum('man','woman','unknow') default 'unknow'
alter table student add column sex enum('man','woman','unknow') not 'unknow';
複製程式碼
  1. 刪除student表的性別欄位
alter table student drop column sex;
複製程式碼
  1. 修改student表address欄位為varchar(128)
alter table student modify column address varchar(128);
複製程式碼
  1. 修改student表sex欄位的預設值為woman
alter table student alter sex set default 'woman';
複製程式碼
  1. 修改表名
alter table student1 rename to student;
複製程式碼
  1. 調整student表列順序
alter table student modify birthday date default null after sex;
複製程式碼
  1. 修改儲存引擎:修改為myisam
alter table student engine=myisam;
複製程式碼
  1. 給班級名稱新增唯一約束
alter table class add unique name_constraint(name);
複製程式碼
  1. 新增檢查約束,學生人數大於等於40
alter table class add constraint student_num_constraint check(student_num >= 40);
複製程式碼
  1. 刪除學生人數大於等於40的約束
alter table class drop check student_num_constraint;
複製程式碼
  1. 給班主任名字新增索引
create index class_teacher_index on class(class_teacher);
複製程式碼

出表

  1. 刪除id為1的學生
delete from student where id = 1;
複製程式碼

刪表

drop table student;
複製程式碼