【零基础学SQL】小白必读:SQL基础语句大全

1. 创建数据库

create database 数据库名;

create database mydatabase;

2. 使用数据库

use 数据库名;

use mydatabase;

3. 创建表

create table 表名 (

    列1 数据类型,

    列2 数据类型,

    ...

);

create table users (

    id int primary key auto_increment,

    username varchar(50) not null,

    email varchar(100) not null,

    created_at timestamp default current_timestamp

);

数值型:numeric

4. 插入数据

insert into 表名 (列1, 列2, ...)

values (值1, 值2, ...);

insert into users (username, email)

values ('john_doe', 'john@example.com');

5. 查询数据

select 列1, 列2, ...

from 表名

where 条件;

select * from users;

select username, email from users where id = 1;

6. 更新数据

update 表名

set 列1 = 值1, 列2 = 值2, ...

where 条件;

update users

set email = 'john.doe@example.com'

where id = 1;

7. 删除数据

delete from 表名

where 条件;

delete from users

where id = 1;

8. 删除表

drop table 表名;

drop table users;

9. 删除数据库

drop database 数据库名;

drop database mydatabase;

10. 连接表(join)

select 列1, 列2, ...

from 表1

join 表2 on 表1.列 = 表2.列;

select users.username, orders.order_date

from users

join orders on users.id = orders.user_id;

11. 排序(order by)

select 列1, 列2, ...

from 表名

order by 列1 asc|desc;

select * from users

order by created_at desc;

12. 分组(group by)

select 列1, count(*)

from 表名

group by 列1;

select username, count(*)

from users

group by username;

13. 过滤分组(having)

select 列1, count(*)

from 表名

group by 列1

having count(*) > 1;

select username, count(*)

from users

group by username

having count(*) > 1;

14. 限制结果(limit)

select 列1, 列2, ...

from 表名

limit 数量;

select * from users

limit 10;

15. 创建索引

create index 索引名

on 表名 (列1, 列2, ...);

create index idx_username

on users (username);

16. 删除索引

drop index 索引名

on 表名;

drop index idx_username

on users;

17. 事务处理

begin transaction;

--  语句

commit;

或者begin transaction;

--  语句

rollback;

18. 创建视图

create view 视图名 as

select 列1, 列2, ...

from 表名

where 条件;

create view active_users as

select * from users

where active = 1;

19. 删除视图

drop view 视图名;

drop view active_users;

20. 存储过程

create procedure 过程名 (参数1 数据类型, 参数2 数据类型, ...)

begin

    --  语句

end;

create procedure getuser (in user_id int)

begin

    select * from users where id = user_id;

end;

21. 调用存储过程

call 过程名(参数1, 参数2, ...);

call getuser(1);

22. 触发器

create trigger 触发器名

before|after insert|update|delete

on 表名

for each row

begin

    --  语句

end;

create trigger before_user_insert

before insert

on users

for each row

begin

    set new.created_at = now();

end;

23. 删除触发器

drop trigger 触发器名;

drop trigger before_user_insert;

24. 备份数据库

backup database 数据库名

to disk = '路径';

backup database mydatabase

to disk = 'c:\backup\mydatabase.bak';

25. 恢复数据库

restore database 数据库名

from disk = '路径';

restore database mydatabase

from disk = 'c:\backup\mydatabase.bak';

26. 创建用户

create user 用户名

identified by '密码';

create user 'newuser'@'localhost'

identified by 'password';

27. 授予权限

grant 权限 on 数据库名.表名

to 用户名@主机名;

grant all privileges on mydatabase.*

to 'newuser'@'localhost';

28. 撤销权限

revoke 权限 on 数据库名.表名

from 用户名@主机名;

revoke all privileges on mydatabase.*

from 'newuser'@'localhost';

29. 删除用户

drop user 用户名@主机名;

drop user 'newuser'@'localhost';

30. 修改表结构

alter table 表名

add 列名 数据类型;

alter table users

add age int;

31. 修改列

alter table 表名

modify 列名 新数据类型;

alter table users

modify age tinyint;

32. 删除列

alter table 表名

drop column 列名;

alter table users

drop column age;

33. 重命名表

alter table 旧表名

rename to 新表名;

alter table users

rename to customers;

34. 重命名列

alter table 表名

rename column 旧列名 to 新列名;

alter table users

rename column username to user_name;

35. 创建唯一约束

alter table 表名

add constraint 约束名 unique (列名);

alter table users

add constraint unique_email unique (email);

36. 删除唯一约束

alter table 表名

drop constraint 约束名;

alter table users

drop constraint unique_email;

37. 创建外键

alter table 表1

add constraint 外键名

foreign key (列1) references 表2(列2);

alter table orders

add constraint fk_user

foreign key (user_id) references users(id);

38. 删除外键

alter table 表名

drop constraint 外键名;

alter table orders

drop constraint fk_user;

39. 创建检查约束

alter table 表名

add constraint 约束名 check (条件);

alter table users

add constraint check_age check (age >= 18);

40. 删除检查约束

alter table 表名

drop constraint 约束名;

alter table users

drop constraint check_age;

41. 创建默认值

alter table 表名

alter column 列名 set default 默认值;

alter table users

alter column created_at set default current_timestamp;

42. 删除默认值

alter table 表名

alter column 列名 drop default;

alter table users

alter column created_at drop default;

43. 创建序列

create sequence 序列名

start with 1

increment by 1;

create sequence user_id_seq

start with 1

increment by 1;

44. 使用序列

insert into 表名 (列1, 列2, ...)

values (nextval('序列名'), 值2, ...);

insert into users (id, username, email)

values (nextval('user_id_seq'), 'john_doe', 'john@example.com');

45. 删除序列

drop sequence 序列名;

drop sequence user_id_seq;

46. 创建函数

create function 函数名 (参数1 数据类型, 参数2 数据类型, ...)

returns 返回值数据类型

begin

    --  语句

    return 返回值;

end;

create function get_user_count()

returns int

begin

    declare user_count int;

    select count(*) into user_count from users;

    return user_count;

end;

47. 调用函数

select 函数名(参数1, 参数2, ...);

select get_user_count();

48. 删除函数

drop function 函数名;

drop function get_user_count;

49. 创建游标

declare 游标名 cursor for

select 列1, 列2, ...

from 表名

where 条件;

declare user_cursor cursor for

select id, username from users;

50. 使用游标

open 游标名;

fetch 游标名 into 变量1, 变量2, ...;

close 游标名;

open user_cursor;

fetch user_cursor into user_id, user_name;

close user_cursor;

51. 删除游标

deallocate 游标名;

deallocate user_cursor;

52. 创建临时表

create temporary table 表名 (

    列1 数据类型,

    列2 数据类型,

    ...

);

create temporary table temp_users (

    id int,

    username varchar(50)

);

53. 删除临时表

drop temporary table 表名;

drop temporary table temp_users;

54. 创建全局临时表

create global temporary table 表名 (

    列1 数据类型,

    列2 数据类型,

    ...

);

create global temporary table global_temp_users (

    id int,

    username varchar(50)

);

55. 删除全局临时表

drop global temporary table 表名;

drop global temporary table global_temp_users;

56. 创建物化视图

create materialized view 视图名 as

select 列1, 列2, ...

from 表名

where 条件;

create materialized view active_users as

select * from users

where active = 1;

57. 刷新物化视图

refresh materialized view 视图名;

refresh materialized view active_users;

58. 删除物化视图

drop materialized view 视图名;

drop materialized view active_users;

59. 创建同义词

create synonym 同义词名 for 表名;

create synonym emp for employees;

60. 删除同义词

drop synonym 同义词名;

drop synonym emp;

61. 创建角色

create role 角色名;

create role manager;

62. 授予角色

grant 角色名 to 用户名;

grant manager to 'john_doe';

63. 撤销角色

revoke 角色名 from 用户名;

revoke manager from 'john_doe';

64. 删除角色

drop role 角色名;

drop role manager;

65. 创建配置文件

create profile 配置文件名

limit 参数1 值1

参数2 值2

...;

create profile user_profile

limit failed_login_attempts 3

password_life_time 90;

66. 删除配置文件

drop profile 配置文件名;

drop profile user_profile;

67. 创建表空间

create tablespace 表空间名

datafile '路径'

size 大小;

create tablespace my_tablespace

datafile 'c:\oracle\oradata\mydb\my_tablespace.dbf'

size 100m;

68. 删除表空间

drop tablespace 表空间名;

drop tablespace my_tablespace;

69. 创建数据库链接

create database link 链接名

connect to 用户名 identified by '密码'

using '连接字符串';

create database link remote_db

connect to remote_user identified by 'password'

using 'remote_db';

70. 删除数据库链接

drop database link 链接名;

drop database link remote_db;

71. 创建上下文

create context 上下文名 using 包名;

create context user_context using user_package;

72. 删除上下文

drop context 上下文名;

drop context user_context;

73. 创建目录

create directory 目录名 as '路径';

create directory my_dir as 'c:\oracle\dir';

74. 删除目录

drop directory 目录名;

drop directory my_dir;

75. 创建库

create library 库名 as '路径';

create library my_lib as 'c:\oracle\lib\my_lib.dll';

76. 删除库

drop library 库名;

drop library my_lib;

77. 创建操作符

create operator 操作符名

binding (数据类型1, 数据类型2, ...)

return 返回值数据类型

using 函数名;

create operator my_operator

binding (int, int)

return int

using my_function;

78. 删除操作符

drop operator 操作符名;

drop operator my_operator;

79. 创建类型

create type 类型名 as object (

    属性1 数据类型,

    属性2 数据类型,

    ...

);

create type address_type as object (

    street varchar(100),

    city varchar(50),

    state varchar(2),

    zip varchar(10)

);

80. 删除类型

drop type 类型名;

drop type address_type;

81. 创建类型体

create type body 类型名 as

    member function 函数名 (参数1 数据类型, 参数2 数据类型, ...)

    return 返回值数据类型

    is

    begin

        --  语句

    end;

end;

create type body address_type as

    member function get_full_address()

    return varchar

    is

    begin

        return street || ', ' || city || ', ' || state || ' ' || zip;

    end;

end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Julyyyyyyyyyyy

感谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值