SQL语言

SQL简介

  SQL(Structrue Query Language, 结构化查询语言), 被广泛用于大多数数据库中。
  SQL语言主要由4部分组成:
  1. 数据定义语言(DDL)
  2. 数据操纵语言(DML)
  3. 数据控制语言(DCL)
  4. 事务控制语言(TCL)

SQL语法

  在java中对数据库的操作主要调用SQL语言中的数据操纵语言,数据操纵语言中用的最多的就是“增删改查”四种操作。SQL语言不去分大小写。

创建数据表操作

格式:

create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..

例如:
  创建student数据表,包含ID,name, sex, age四个字段。将ID设置为主键。说一下主键的概念,主键是数据库数据表中每条记录的唯一标示。通过主键可以明确的区分数据表中的记录。一个数据表中不能有相同的主键。

CREATE TABLE TABLENAME(id int NOT NULL PRIMARY KEY auto_increment, name varchar(30) NOT NULL , sex int(1), age int(3) )

格式:

insert into table1(field1,field2) values(value1,value2)

例如:
  在数据表student中创建一条记录,姓名张三,性别1, 年龄23

INSERT INTO student(name, sex, age) values('张三',1,23)

格式:

delete from table1 where 范围

例如:
  在数据表student中删除姓名为张三的记录。

DELETE FROM student where name='张三'

格式:

update table1 set field1=value1 where 范围

例如:
  在数据表student中更改id =1的记录, 将其年龄改为23,性别改为1。

UPDATE student set age=23,sex=1 where id=1

格式:

select * from table1 where field1

例如:
  将数据表student中年龄为23 的数据去不显示出来。

select name,age from student where age=23

你可能感兴趣的:(sql)