当前位置:网站首页>Summary of basic knowledge of Oracle database SQL statements I: Data Definition Language (DDL)

Summary of basic knowledge of Oracle database SQL statements I: Data Definition Language (DDL)

2022-06-22 08:06:00 EA development - green shirt code customer

ORACLE database SQL Basics

1、 Create user 
CREATE USER jack             -- Create user 
INDENTIFIED BY 123456        -- Set the password 
ACCOUNT UNLOCK               -- User unlock 
2、 Authorize roles 
GRANT CONNENT,   -- Connect to the server session permissions 
	  RESOURCE,  -- Create table 、 stored procedure 、 trigger 、 Index and other permissions 
	  DBA         -- System permissions , Have permission to create users 
TO jack
3、 Recycling permissions 
REVOKE CONNENT FROM jack
4、 Modify the user 
ALTER USER jack IDENTIFIED BY 654321
ALTER USER jack LOCK|UNLOCK
 Data definition language (DDL)
CREATE command 、ALTER command 、DROP command 
 Create a student list 
CREATE TABLE  student_1(
sno         varchar2(10),
sname       varchar2(20),
sage        number(3),
sbirthday   date
)
 Replicated table structure 
CREATE TABLE student_2 AS 
SELECT * FROM student_1 WHERE 1=2;
 Copy table 
CREATE TABLE student_3 AS
SELECT * FROM student_1;
 Create result set table 
CREATE TABLE student_2 AS 
SELECT * FROM student_1 WHERE sage<=20;
 Delete table 
DROP TABLE student_1;             -- You can roll back 
TRUNCATE TABLE student_2;         -- Cannot roll back 
DELETE FROM student_3 WHERE 1=1;  -- Delete data conditionally 
 Add columns 
ALTER TABLE student_1 ADD ssex varchar2(2);
 Change data type 
ALTER TABLE student_1 MODIFY sno number;
 Change column names 
ALTER TABLE student_1 RENAME COLUMN sno TO id;
 Delete column 
ALTER TABLE student_1 DROP COLUMN ssex;
 Modify the name of the table 
ALTER TABLE student_1 RENAME TO student_0;
 Add table comments 
COMMENT ON TABLE student_0 IS ' Student list ';
 Add table field comments 
COMMENT ON COLUMN student_0.id IS ' Student ID';
 See table notes 
 SELECT * FROM user_tab_comments 
 WHERE TABLE_NAME='student_0';
 View table field comments 
SELECT * FROM user_col_comments
WHERE TABLE_NAME='student_0';
 Table constraints 
1PRIMARY KEY: Primary key constraint 
2FOREIGN KEY: Foreign key constraints 
3CHECK  : Check constraint 
4UNIQUE : Unique constraint 
5NOT NULL: Non empty constraint 
 Create constraints 
ALTER TABLE  Table name  ADD CONSTRAINT  Constraint name   Constraint content 
 Add primary key 
ALTER TABLE student_0 ADD CONSTRAINT myconstraint PRIMARY KEY(id)
 Add a foreign key constraint 
ALTER TABLE student_0 ADD CONSTRAINT myforeingkey FOREIGN KEY (cno) REFERENCES course (cno)
 add to check constraint 
ALTER TABLE student_0 ADD CONStRAINT mycheck CHECK (ssex=' male ' or ssex=' Woman ')
 Add unique constraints 
ALTER TABLE student_2 ADD CONSTRAINT myunique UNIQUE(id)
 Add non empty constraints 
ALTER TABLE student_2 ADD CONSTRAINT mynotnull NOT NULL(id)
 Delete constraints 
ALTER TABLE student_2 DROP CONSTRAINT myunique
 Create session level temporary tables 
CREATE GLOBAL TEMPORARY TABLE table_name(col1,col2...)ON COMMIT PRESERVE ROWS;
 Create transaction level temporary tables 
CREATE GLOBOLTEMPORARY TABLE table_name(col1,col2...)ON COMMIT DETELE ROWS;

The role of temporary tables : Break down complex logic , Use temporary tables to store intermediate results , To facilitate the following logical processing . Some temporary data may need to be stored during program execution , These data need to be used during the whole program session, and so on .

Session level temporary tables are truncated only when the session ends , And the transaction level temporary tables do not matter COMMIT、ROLLBACK、 Or the session ends , The data in the temporary table will be truncated and emptied (TRUNCATE TABLE)

原网站

版权声明
本文为[EA development - green shirt code customer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220530130571.html