(1)安装MySQL
下载地址:https://www.mysql.com/downloads/
开源软件版本:beta版本和stable版本
(2)入门语句
1、连接服务器 mysql -uroot -p
连上服务器首先面对的是库
当选了库之后,面对的是表
查看表的方法:
2、创建数据库
createdatabase 数据库名 charset 字符集utf8
3、删除一个数据库
dropbasedata
4、在数据库中,database 不可以改名,table和列可以改名(rename)
5、查看数据库的位置
6、简单表语句
1064语法错误
7、
8、删除表
drop tablestu;
9、在表中插入数据
insert intostu values
10、查看表中的数据
select * from newstu;
11、清空表中的数据 truecate
truncatenewstu;
与delect的区别:
truncate :相当于删除表在重建,操作后得到一个全新表(就得扔了)
delect:从删除所有的层面来操作(橡皮擦擦除)
如果清空的话,truncate更快一些
(3)乱码出现
1、出现乱码的情况
由于客户端使用的是gbk,所以需要告诉服务器,客户端使用的是gbk编码
set namesgbk;
(4)新建一个简单的表
薪资登记表
姓名 |
性别 |
公司 |
工资 |
饭补 |
create tableclass(
id int primary key auto_increment,
snamevarchar(10) not null default ' ',
genderchar(1) not null default ' ',
companyvarchar(20) not null default ' ',
salarydecimal(6,2) not null default 0.00,
fanbu smallint not null default 0
)enginemyisam charset utf8;
查看表结构:
desc class
1. 向那几列添加值
2. 分别添加什么值
(1,'张三','男','百度',8888.67,234)
insert intoclass (id,sname,gender,company,salary,fanbu) values (1,'张三','男','百度',8888.67,234)
id虽然没有插入,但是它是自增型,
饭补是0,因为在设置过程中,如果没有填默认是0
如果不声明插入的列,则意味插入所有的列
注意以下错误,在插入所有列的时候不能认为id是自增型的,就省略,这样将会报错!
增加多行
insert intoclass values (4,'刘备','v','拼多多',8514.23,311),(5,'张备','n','拼嘻嘻',8555.23,351);
insert intoclass values (sname,company,salary) ('曹操','江东',522.00), ('孙权','华大',53);
1 updat改哪张表
2 改那几列 gender,company
3 改成什么值 :n改为v,’千度‘
updateclass set gender = 'v' company='千度';
这样改后,所有的gender都变为v,公司都变为‘千度'
update classset fanbu = 5000 where id=6;
注意:
where 后加的是表达式,只要表达式值为真就发挥作用;
如:
删除是指删除整行
3. 从哪个表中删除
4. 删除那几行,不存在删除一列或者某几列
delete fromclass where salary >=5000;
5. 查哪张表
6. 查哪些列
7. 查哪行那列
selectsname,salary,fanbu from class where id=5;#查id=5的sname,salary,fanbu
select *from class