SQL基础语法

SQL基础语法

新建表

  其中,tablename是要建立表的名称;column_name1是建立表中列的名称,即字段名;datatype是这个字段的数据类型;column_constraint是字段的约束规则,如:主键约束、外键约束。

1
2
3
4
5
create table tablename(
column_name1 datatype [column_constraint],
column_name2 datatype [column_constraint],
...
)

如:

1
2
3
4
5
6
7
8
9
10
11
create table usertable(
user_id bigint IDENTITY (1,1) not null primary key,//IDENTITY (1,1)表示自动增加,增加值为1
user_name varchar(40),
user_password varchar(40),
user_true_name varchar(40),//用户的真实姓名
user_age int,
user_sex varchar(2),
user_address varchar(80),
user_telephone varchar(20),
user_time datetime default (gerdate())
)

删除表

1
drop table table_name

更改表

1
2
3
4
alter table table_name
[alter column column_name new_datatype [column_constraint]]
|[add column_name new_datatype [column_constraint]]
|[drop column_name new_datatype [column_constraint]]

查询语句


  在SQL语句中,查询语句select是最为常用的,其基本的语法格式如下。

1
2
3
4
5
6
select select_list [into new_table]
from table_source
[where search_condition]
[group by group_by_expression]
[having search_condition]
[order by order_expression [ASC|DESC]]

  其中,select_list是要查询的内容,如:字符名称的列表;[into new_table]是指可以把查询的结果放入一个新的表中;table_source是指表名,是查询数据的来源,如果是单表查询则为一个表的名称,如为多表查询则是表的名称列表;search_condition是查找的条件;group_by_expression是查询结果分组的条件;search_condition中分组后组内的条件;order_expression[ASC|DESC]是指排序的表达式以及方式,如:order_expression可以是一个字段名,则根据这个字段来排序,ASC表示升序,DESC表示降序。


比如:

  在userTable中要找出年龄在20~30岁之间的用户信息,并按ID号升序排序:

1
2
3
select * from userTable
where user_age>10 and user_age<30
order by user_id asc

1.=号

  如果数据类型是数值型则不必用引号(‘或(“括起来,如果是字符型,则必须用引号括起来,如:

1
2
select * from userTable
where user_age=20 and user_name='yourname'

  其功能是从user_Table表中查找出年龄是20,姓名是yourname的记录。


2.in与not in

  in用来确定给定的值是否与子查询或列表中的值相匹配,使用not in则对返回值相反。其语法格式如下:

1
2
3
4
5
test_expression [NOT] IN
(
subquery
|expression [,...n]
)

  其中test_expression是一个有效的表达式;subquery是包含某列结果集的子查询,必须与test_expression具有相同的类型;expression [,…n]是一个表达式列表,用来测试是否匹配,所有表达式必须和test_expression具有相同的类型。

  如:查询出年龄是20、22、25的用户信息

1
2
select * from userTable
where user_age in (20,22,25)

3.between…and与not between…and

  between…and用来选择列值在某个范围内的记录,not between…and则恰恰相反,用来选择列值不在某个范围内的记录,其语法如下:

1
test_expression [NOT] between begin_expression and end_expression

  如:查询出年龄在20~25(包括20岁和25岁)岁之间的用户信息

1
2
select * from userTable
where user_age between 19 and 26

4.like与no like

  确定给定的字符串是否与指定的模式匹配。模式可以包含常规字符和通配符字符,可使用字符串的任意片段匹配通配符。与使用”=”和”!=”字符串比较运算符相比,使用通配符可使LIKE运算符更加灵活。语法如下:

1
match_expression [NOT] like pattern

 

  其中,match_expression是任何字符串数据类型的有效表达式,pattern是在match_expression中的搜索模式,可有如下的通配符:”%”表示包含零个或更多字符的任意字符串;”_”表示任意单个字符。通配符在各种数据库可能会不一样。这里只介绍SQL Server的通配符。

  如:查询出真实姓名为姓”邓”的用户信息

1
2
select * from userTable
where user_true_name like '邓%'

5.in null与is not null

  is null确定一个给定的表达式是否为null,in not null与is null相反。其语法如下:

1
expression is [not] null

  其中expression是一个有效的表达式

  如:查询出真实姓名是空的用户信息

1
2
select * from usetTable
where user_true_name is null

6.and与or

  and运算用来连接两个布尔型表达式并当两个表达式都为true时返回false

  or运算用来将两个条件结合起来,满足其中之一即返回true

  and运算的优先级比or要高


插入语句

1
2
insert into table_name(column_name1,column_name2,...)
value(column_name1_values,column_name2_values,...)

  其中,table_name是将行添加到数据表中;(column_name1,column_name2,…)是要添加内容的列;(column_name1_values,column_name2_values,…)是将要插入的单个列中对应的值或表达式。

  如:要往用户表中插入一条用户信息记录

1
2
insert into table_name(user_name,user_password,user_true_name,user_age,user_sex,user_address,user_telephone)
value('zmd','987654','翟',18,'男','桂林','10086')

  其中表的user_id和add_time字段的值可以自动生成。


更新

1
2
3
4
5
update table_name set 		
column_name1=column_name1_values,
column_name2=column_name2_values,
...
[where 子句]

  其中,where子句同select语句中的where子句

  如:把用户表真实姓名姓邓的年龄全部改为27。

1
2
update userTable set user_age=27
where user_true_name like'邓%'

删除

1
delete from table_name [where 子句]

  where子句同select语句中的where子句

  如:删除用户表中姓邓的用户的所有记录

1
delete from userTable where user_true_name like'邓%'

查看所有数据库

1
show databases;
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×