sql的运用
一、数据库的操作流程
1.mysql -u root -p 进入数据库
2.show databases; 显示所有的仓库
3.create database 库名; 创建仓库
4.use 库名; 使用库
5.show tables; 查看数据库中所有的表
6.create table 表名(字段名1 字符类型1(字符长度1),字段名2 字符类型2(字符长度2)); 创建一个表
二、
1.desc 表名; 查看表结构
如:desc a1;
2.select * from 表名; 查看表中所有的数据 *代表所有信息
如:select * from a1;
3.插入数据
(1)插入所有的信息
备注:插入的数值,可以直接写;插入的是字符类型,加上单引号或双引号
格式:insert into 表名 values(值1,值2);
如:insert into a1 values(1,"1");
(2)插入部分信息
insert into 表名(字段名) values(值);
如:insert into a1(id) values(3);
(3)解决插入中文变成?号
建表语句后面接default charset=utf8;
如:create table a2(id int(10),sex varchar(20))default charset=utf8;
4.sql字符类型
(1)数值类型
int类型 大整数值(常用)
bugint类型 极大整数
fliat类型 浮点数
(2)字符类型
char类型 定长字符类型
varchar类型 变长字符类型
(3)时间类型
date类型 日期值 年月日
time类型 时间值 时分秒
year类型 年
datatime类型 年月日 时分秒
timestamp类型 混合日期 年月日 时分秒
5.删除
(1)drop table 表名; 删除表
如:drop table a3;
(2)delete from 表名; 删除表中数据
如:delete from a2;
(3)delete from 表名 where 条件; 删除表中指定条件的数据
如:delete from a2 where id=4;
(4)truncate 表名;
如:truncate a2;
三、对字段进行操作
1.通过add添加字段
格式:alter tabe 表名 add 字段名 字符类型(字符长度);
如:alter table a2 add name varchar(20);
2.通过change修改字段
格式:alter table 表名 change 原字段名 新字段名 字符类型(字符长度);
如:alter table a2 change name fs int(10);
3.通过drop删除字段
格式:alter table 表名 drop 字段名;
浙公网安备 33010602011771号