一。數據庫打開
1.命令行
2.進入mysql
mysql -uroot -p密碼
3.退出
exit;
二。針對數據庫的操作
1.創建數據庫(有分號)
create database student;
2.使用數據庫
use student
3.刪除數據庫(有分號)
drop database student;
安全刪除
drop database if exists student;
4.查詢數據有哪些表
注意:必須先使用數據庫,才可以看數據庫有哪些表
show tables
三。針對數據庫中表的操作
1.創建表
(1)創建id為主鍵
create table student(
id int primary key,
name varchar(256),
sex varchar(2),
age int,
score int
);
(2)創建id為主鍵,方法2
create table student(
id int,
name varchar(256),
sex varchar(2),
age int,
score int,
primary key(id)
);
(3)創建多個主鍵,并且id設為自增auto_increment
create table student(
id int auto_increment,
name varchar(256),
sex varchar(2),
age int,
score int,
primary key(id,name,age)
);
2.刪除表
drop table student;
3.表的重新命名
alter table student rename to student1;
4.表的字段類型展示
desc student;
5.插入數據
(1)插入單條數據
insert into student (id,name,sex,age,score)value(1,"王五","男",25,60);
(2)插入多條數據
insert into student (id,name,sex,age,score)
values
(2,"張三","男",27,75),
(3,"李四","男",29,70);
三。表中數據類型(數值類型,日期、時間類型,字符串類型)
1.數值類型
(1)整數類型:tinyint, smallint ,mediumint, int, bigint
tinyint? ? ? ? ? ? ? 很小的整形,1個字節,有符號:-128->127 , ?? ?無符號:0->255
smallint? ? ? ? ? ?小的整形,?? ?2個字節,有符號:-32768->32767,?? ?無符號:0->65535
mediumint? ? ? 中等整形,?? ?3個字節,有符號:-8388608->8388607,無符號:0->16777215
int,?? ??? ?普通整形,?? ?4個字節,
bigint?? ??? ?大的整形,?? ?8個字節
設置成無符號字符? ??
age tinyint unsigned,
(2)浮點數類型
?浮點型和定點型 ?
?? ?浮點型和定點型都可以使用(M,N的方式來表示) , M:精度,總位數,N:標度,小數位數?
float?? ??? ?單精度浮點型?? ?4個字節
double?? ??? ?雙精度浮點型?? ?8個字節
decimal(M,N)定點型?? ??? ??? ?M+2字節?? ??? ?以串的方式存儲
2.日期、時間類型
DATETIME(類型)? ? YYYY-MM-DD HH:MM:SS(年:月:日 時:分:秒)? ? ? ??
1000-01-01 00:00:00 -> 9999-12-31 23:59:59?? ?8個字節(范圍)
DATE?? ??? ?YYYY-MM-DD?? ??? ??? ??? ?
1000-01-01 -> 9999-12-31?? ??? ??? ??? ??? ?3個字節
TIMESTAMP?? ?YYYY-MM-DD HH:MM:SS?? ??? ?
1970-01-01 00:00:00 -> 2038-01-19 03:14:07 ?? ?4個字節
TIME?? ??? ?HH:MM:SS?? ??? ??? ??? ?
-838:59:59->838:59:59?? ??? ??? ??? ??? ??? ?3個字節
YEAR?? ??? ?YYYY?? ??? ??? ??? ??? ?
1901->2155?? ??? ??? ??? ??? ??? ??? ??? ??? ?1個字節
3.字符串類型
?? ?char(M)?? ??? ?固定長度字符串?? ??? ??? ?M字節, 1<=M<=255
?? ?varchar(M)?? ?可變長字符串?? ??? ??? ?L+1字節,L<=M, ?1<=M<=255 (L:字符串實際長度)
?? ?text
?? ?tinytext?? ?小的字符串?? ??? ??? ??? ?L+1字節,L<2^8
?? ?mediumtext?? ?中等字符串?? ??? ??? ??? ?L+2字節,L<2^16
?? ?longtext?? ?長的文本?? ??? ??? ??? ?L+3個字節,L<2^24
????????char和varchar區別: char是固定長度,固定M個字節,而varchar是實際長度+1,實際長度不能超過M。
????????如果要求查詢速度:可以使用char, 盡量不要浪費空間。
????????varchar的查詢速度低于char,如果要求存儲空間,可以使用varchar。
四。查詢數據表
1.全部查詢
select * from student;
2.部分查詢
select id,name from student;
3.條件查詢(兩者等價)
select * from student where sex!="女";
select * from student where sex<>"女";