玖叶教程网

前端编程开发入门

SQL操作相关命令(sql 操作)

数据库操作

  • 查看当前所有数据库:
show databases;
  • 创建数据库:
create database 数据库名  DEFAULT  CHARSET utf8 COLLATE utf8_general_ci;
  • 删除数据库:
drop database 数据库名;
  • 进入数据库:
use 数据库;

表操作

  • 查看当前所有表:
进入 数据库 use 数据库, 		show tables;
  • 创建表
create table 表名(
					列名,类型,
          列名,类型) default charset=utf8;

创建表示例:

create table tb1(
	id int,
    name varchar(16)
)default charset=utf8;
create table tb2(
	id int,
    name varchar(16) not null,   -- 这一列数据 不允许为空
    email varchar(32) null,      -- 这一列数据 允许为空(默认)
    age int
)default charset=utf8;
create table tb3(
	id int,
    name varchar(16) not null,   -- 不允许为空
    email varchar(32) null,      -- 允许为空(默认)
    age int default 3            -- 插入数据时,如果不给age列设置值,默认值:3
)default charset=utf8;
create table tb4(
	id int primary key,			 -- 主键(不允许为空、不能重复)
    name varchar(16) not null,   -- 不允许为空
    email varchar(32) null,      -- 允许为空(默认)
    age int default 3            -- 插入数据时,如果不给age列设置值,默认值:3
)default charset=utf8;
create table tb5(
	id int not null auto_increment primary key,	-- 不允许为空 & 主键 & 自增
    name varchar(16) not null,   		-- 不允许为空
    email varchar(32) null,      		-- 允许为空(默认)
    age int default 3            		-- 插入数据时,如果不给age列设置值,默认值:3
)default charset=utf8;

注意:一个表里只能有一个自增列【自增列一般都是主键】

  • 删除表
drop table 表名;
  • 清空表
delete from 表名;
或  truncate table 表名; --->【速度快,但是无法撤销 回滚等】
  • 添加列
alter table 表名 add 列名 类型;
alter table 表名 add 列名 类型 DEFAULT 默认值;
alter table 表名 add 列名 类型 not null default 默认值;
alter table 表名 add 列名 类型 not null primary key auto_increment;
  • 删除列
alter table 表名 drop column 列名;
  • 修改列类型
alter table 表名 modify column 列名 类型;
  • 修改列 类型 + 名称
alter table 表名 change 原列名 新列名 新类型;
alter table  tb change id nid int not null;
alter table  tb change id id int not null default 5;
alter table  tb change id id int not null primary key auto_increment;

alter table  tb change id id int; -- 允许为空,删除默认值,删除自增。
  • 修改列 默认值
ALTER TABLE 表名 ALTER 列名 SET DEFAULT 1000;
  • 删除列 默认值
ALTER TABLE 表名 ALTER 列名 DROP DEFAULT;
  • 添加主键
alter table 表名 add primary key(列名);
  • 删除主键
alter table 表名 drop primary key;

数据行操作的相关SQL语句

  • 新增数据
insert into 表名 (列名, 列名, 列名)  values(对应列的值, 对应列的值, 对应列的值);
insert into tb1(name,password) values('武沛齐','123123');
insert into tb1(name,password) values('武沛齐','123123'),('alex','123');

insert into tb1 values('武沛齐','123123'),('alex','123'); -- 如果表中只有2列
  • 删除数据
delete from 表名;
delete from 表名 where 条件;
delete from tb1;
delete from tb1 where name="wupeiqi";
delete from tb1 where name="wupeiqi" and password="123";
delete from tb1 where id>9;
  • 修改数据
update 表名 set 列名=值;
update 表名 set 列名=值 where 条件;
update tb1 set name="wupeiqi";
update tb1 set name="wupeiqi" where id=1;

update tb1 set age=age+1;  -- 整型
update tb1 set age=age+1 where id=2;

update L3 set name=concat(name,"db");
update L3 set name=concat(name,"123")  where id=2;  -- concat一个函数,可以拼接字符串
  • 查询数据
select * from 表名;
select 列名,列名,列名 from 表名;
select 列名,列名 as 别名,列名 from 表名;
select * from 表名 where 条件;
select * from tb1;
select id,name,age from tb1;
select id,name as N,age, from tb1;
select id,name as N,age, 111 from tb1;

select * from tb1 where id = 1;
select * from tb1 where id > 1;
select * from tb1 where id != 1;
select * from tb1 where name="wupeiqi" and password="123";

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言