登錄mysql:mysql -h localhost -u root -p
顯示所有數據庫:show databases;
進入mydb數據庫:use mydb;
顯示某個數據庫中的所有表,一般在use 數據庫名的操作之後用:show tables;
創建數據庫mydb2:create database mydb2 default character set 'utf8';
創建表:(在新建的mydb2數據庫下創建表)(auto_increment 是自增, primary key是主鍵)
create table student(
stuid int auto_increment primary key,
name varchar(20) not null,
age int,
score double not null
);
查看錶的結構 :desc[ribe] student;
指定字段名,插入記錄
insert into student(name,age,score)values('風清揚',25,95.5);
insert into student(stuid,name,age,score)values(4,'東方不敗',20,90);
insert into student(name,age,score)values('令狐沖',22,88);
不指定字段名,插入記錄: insert into student values(12,'李尋歡',29,93);
插入多條記錄:
insert into student(name,age,score)values('馬雲',50,60.5),('雷軍',45,65.3),('習近平',60,72.5);
insert into student values(19,'郭靖',22,60.2),(22,'黃蓉',20,69);
刪除學號爲19的記錄: delete from student where stuid=19;
將名字爲“馬雲”的記錄的成績變爲60分:update student set score=60 where name='馬雲';
查詢student表中成績在80到90之間的記錄:
select stuid,name,age,score from student where score>=80 and score<=90;
select * from student where score between 80 and 90;
select name as 學生姓名, score as 成績 from student where score>=80 and score<=90;
查詢所有學生的信息,按照成績升序的顯示查詢結果:select * from student order by score;
查詢所有學生的信息,按成績降序的顯示查詢結果:select * from student order by score desc;
查詢所有姓李的學生信息:select * from student where name like '李%';(%表示還有一個或多個字符)
查詢名字只有兩個字、並且最後一個字是'雲'的學生信息:select * from student where name like '_雲';
查詢成績在80到90之間的前3條學生的信息:select * from student where score>=80 and score<=90 limit 3;
查詢出第3條到第8條的學生記錄:select * from student limit 2,6;
查詢學生表中的總記錄數:select count(*) from student;
查詢學生中age不爲null的記錄數:select count(age) from student;