有同学需求,就整理一些基础入门的SQL语句;
SQL的注释:
--:表示单行注释
/*…*/:用于多行(块)注释
1.创建数据库:
create DATABASE istester
2.删除数据库:
drop DATABASE istester
3.创建新表:
-- 创建istester和idoxu表
CREATE TABLE istester (
id INT(10) NOT NULL UNIQUE PRIMARY KEY ,
name VARCHAR(20) NOT NULL ,
sex VARCHAR(4) ,
birth YEAR,
department VARCHAR(20) ,
address VARCHAR(50)
);
CREATE TABLE idoxu (
id INT(10) NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT ,
stu_id INT(10) NOT NULL ,
c_name VARCHAR(20) ,
grade INT(10)
);
根据已有的表创建新表: A:
create table istester2 like istester ;
B:
create table idoxu2 as select * from idoxu where 2=1;
4.删除表:
drop table istester2
5.增加一个列:
Alter table istester add column idoxu VARCHAR(20) NOT NULL
6.添加主键:
Alter table istester add primary key(idoxu)
删除主键:
Alter table istester drop primary key(idoxu)
7.几个简单的基本的sql语句
插入:
insert into istester(id,username,idoxu) values(1,idoxu,2020);
insert into istester(id,username,idoxu) values(2,idoxu2,2020);
insert into istester(id,username,idoxu) values(3,idoxu3,2020);
insert into istester(id,username,idoxu) values(4,idoxu4,2020);
选择:
select * from istester where id = 1
删除:
delete from istester where id = 1
更新:
update table1 set name=idoxu666 where id = 1
查找:
select * from istester where name like ‘%idoxu%’
排序:
select * from istester order by id desc
总数:
select count as totalcount from istester
求和:
select sum(id) as sumvalue from istester
平均:
select avg(id) as avgvalue from istester
最大:
select max(id) as maxvalue from istester
最小:
select min(id) as minvalue from istester
End 。